【问题标题】:Recentering state abbreviation on ggplot2在 ggplot2 上重新定位状态缩写
【发布时间】:2017-02-16 03:58:03
【问题描述】:

所以我修改了形状配置文件以添加夏威夷和阿拉斯加,因此我的数据框使用的长纬度坐标与下面显示的 maps 包 (map_data("state")) 提供的库存州坐标不同

states <- data.frame(state.center, state.abb)
states
           x       y state.abb
1   -86.7509 32.5901        AL
2  -127.2500 49.2500        AK
3  -111.6250 34.2192        AZ
4   -92.2992 34.7336        AR
5  -119.7730 36.5341        CA

我的数据框的坐标如下。

当我使用下面的代码绘制它时,我得到一张没有任何州缩写标签的地图。

p <- function(data, brks, title) {
  ggp <- ggplot() + 
    geom_polygon(data = data, aes(x = long, y = lat, group = group, 
                                  fill = IMSUB), color = "black", size = 0.15) + 
    scale_fill_gradient2(limits=c(-1,1), breaks = c(-1, 0, 1), labels=c("Trump", 0, "Clinton"), low = "red", mid = "white",
                         high = "blue", midpoint = 0, space = "Lab",
                         na.value = "grey50", guide = "colourbar") + 
    theme_nothing(legend = TRUE) + labs(title = title, fill = "") + 
    geom_text(data = states, aes(x = x, y = y, label = state.abb), size = 3)
  return(ggp)
}

我觉得我可以用us50 中的坐标替换states 数据框中的坐标,但我不确定如何正确替换它们。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    你试过aes()里面的尺寸吗?

     geom_text(data=state, aes(x y, label = state.abb, size=3)
    

    如果没有,这个其他方法可能会起作用,只需替换 geom_text 行:

    with(states, annotate(geom="text", x = x y=y, label = state.abb, size = 3)) 
    

    它应该使用状态应用注释,作为带有 x 和 y 的文本作为你的 lat 和 long..这应该可以工作。

    【讨论】:

    • 再仔细看,你的坐标系看起来确实不一样......虽然 ggplot 可以绘制任何一个系统,但它们仍然需要在同一个系统中......看看如果你会发生什么让它们在同一个坐标系中。您可以为此使用包“sp”...
    猜你喜欢
    • 1970-01-01
    • 2010-12-28
    • 1970-01-01
    • 1970-01-01
    • 2013-01-24
    • 1970-01-01
    • 2019-10-25
    • 1970-01-01
    • 2015-04-22
    相关资源
    最近更新 更多