ggplot 地图和地块颜色中的县界线

County lines in ggplot map and plot colors

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

这是我的数据集的示例

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()

这是结果图:

如果您想要州内的县线,您可能希望在 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))