【问题标题】:County lines in ggplot map and plot colorsggplot 地图中的县线和绘图颜色
【发布时间】:2021-11-02 02:46:38
【问题描述】:

我正在尝试在 R 中创建我的数据地图。创建地图时,没有显示轮廓,也没有显示点的填充。弗吉尼亚的地图是使用 Rs map_data 函数制作的。当地图拉起时,它只是黑色的,没有县线。我尝试将多边形“颜色”更改为“col”和“fill”。不用找了。此外,地图上的数据点也是黑色的——没有颜色,尽管我有我的代码。我的代码有错误吗?

这是我的数据集示例

urban_continuum p.shannon p.simpson latitude longitude
A_Rural 4.609 0.9334 37.7659 -75.7578
C_Suburb 6.420 0.9973 38.0241 -78.5535
D_City 5.961 0.9959 38.8183 -77.0820
B_Town 5.033 0.9923 37.7879 -80.0086

这是我的代码:

Virginia=map_data("state", region = "virginia")

VA.pdiv=ggplot()+
  geom_polygon(data=Virginia, aes(x=long, y=lat, group=group,
               color="white"))+
  geom_point(data = VA.county1, aes(x=longitude, y=latitude,
                                    fill=p.shannon,
                                    size=p.simpson,
                                    shape=urban_continuum))+
  scale_fill_gradient(low = "blue", high = "orange")+
  labs(x="Longitude", y="Latitude",
       fill="Shannon Diversity Index of VA Plants",
       size="Simpson Diversity Index of VA Plants",
       title = "Plant Diversity in Virginia")+
  coord_map()
VA.pdiv

当我尝试其他人的建议时,我得到了颜色点,但仍然没有县线。我将删除 p.simpson 或 p.shannon 以帮助澄清,但现在我只是解决县线问题。 这是我的代码:

require(ggplot2)
library(ggplot2)

#pull up our base map
Virginia= map_data("county", region="Virginia")

ggplot()+
  geom_polygon(data = Virginia, 
               aes(x = long, 
                   y = lat, 
                   group = group)) +
  geom_point(data = VA.county1, 
             aes(x = longitude, 
                 y = latitude,
                 color = p.shannon,
                 size=p.simpson,
                 shape = urban_continuum)) +
  scale_color_gradient(low = "blue", high = "orange") +
  labs(x = "Longitude", 
       y = "Latitude",
       color = "Shannon Diversity Index of VA Plants",
       size = "Simpson Diversity Index of VA Plants",
       shape= "Urban Continuum",
       title = "Plant Diversity in Virginia") +
  coord_map()

这是结果图:

【问题讨论】:

  • 您能否为VA.county1 添加一些数据以使代码可重现?谢谢
  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。
  • 我很抱歉。我添加了一些示例数据,以及一些澄清细节。

标签: r maps


【解决方案1】:

如果您想要州内的县线,您可能希望在map_data 中使用“县”而不是“州”。

对于geom_polygon,不需要color,除非您想为不同的县指定不同的颜色(在这种情况下,您可以考虑使用fill,因为我建议您在积分中使用color)。

对于geom_point,我切换到color并更改为scale_color_gradient以匹配。

如果这更接近您的想法,请告诉我。

library(ggplot2)

Virginia <- map_data("county", region = "virginia")

ggplot() +
  geom_polygon(data = Virginia, 
               aes(x = long, 
                   y = lat, 
                   group = group)) +
  geom_point(data = VA.county1, 
             aes(x = longitude, 
                 y = latitude,
                 color = p.shannon,
                 size = p.simpson,
                 shape = urban_continuum)) +
  scale_color_gradient(low = "blue", high = "orange") +
  labs(x = "Longitude", 
       y = "Latitude",
       fill = "Shannon Diversity Index of VA Plants",
       size = "Simpson Diversity Index of VA Plants",
       title = "Plant Diversity in Virginia") +
  coord_map()

地图

数据

VA.county1 <- structure(list(urban_continuum = c("A_Rural", "C_Suburb", "D_City", 
"B_Town"), p.shannon = c(4.609, 6.42, 5.961, 5.033), p.simpson = c(0.9334, 
0.9973, 0.9959, 0.9923), latitude = c(37.7659, 38.0241, 38.8183, 
37.7879), longitude = c(-75.7578, -78.5535, -77.082, -80.0086
)), class = "data.frame", row.names = c(NA, -4L))

【讨论】:

  • 这个解决方案对积分有效,非常感谢!奇怪的是,对于县线,我仍然无法让它发挥作用。
  • 你的结局是什么?没有像上面这样的县线?您是否使用完全相同的代码?您在map_data 中使用“县”而不是“州”吗?
  • 没有像上面这样的县线。完全相同的代码 - 我将您的代码复制并粘贴到我的系统中。我将编辑/更新我的问题以显示我所做的。
  • @madam_fledershrew 如果你不添加任何图层,只做ggplot() + geom_polygon(data = Virginia, aes(x = long, y = lat, group = group)) + coord_map() 你会得到什么?你使用的是什么版本的ggplot2?您是否也尝试过清除环境并重新启动 R?
  • 你能用加利福尼亚县here复制这个例子吗?
猜你喜欢
  • 2012-03-15
  • 1970-01-01
  • 1970-01-01
  • 2021-06-26
  • 2019-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-05
相关资源
最近更新 更多