R 和地图:如何知道 shapefile 中每个细分的名称

R and map: how to know the name of each subdivision in a shapefile

我正在使用 ggplot 和“sf”包绘制尼泊尔及其 3983 个村庄的地图。 village_level 地图数据是一个 .shp 文件(类似于可以在此处找到的文件 https://data.humdata.org/dataset/administrative-bounadries-of-nepal)。我用

nepal_village <- st_read("nepal_village.shp")

village_plot <- ggplot() +
        geom_sf(data = nepal_village) + 
        ggtitle("AOI Boundary Plot") + 
        coord_sf()

village_plot

我想用每个村庄的人口对地图进行颜色编码。人口存储在另一个文件中,一列是村庄名称,另一列是人口。是否可以将人口文件与地图文件合并?如何获取.shp文件中各个村庄的名称?

您应该可以通过 dplyr 加入数据集

join <- dplyr::left_join(x = nepal_village , y = village_pop, by = 'shared column name')

我没有太多地使用 ggplot 进行映射,但是使用 tmap 可以将其可视化如下。 我确定也有一种方法可以在 ggplot 中将其可视化。

village_plot <- tm_shape(join) + 
    tm_polygon(col= "villagePOP") +
    tm_layout(title = "AOI Boundary Plot") 

在您的 .shp (shapefile) 中,您有一列包含村庄的名称。 将形状文件读取到对象(在示例中为奥地利)

austria_shapes <- read_sf("data/gadm36_AUT_shp/gadm36_AUT_1.shp") # in data folder

然后通过村庄名称与您的第二个 table 一起反对:data_joined。 然后:

ggplot() +
  geom_sf(data = data_joined, aes(fill = n),
          color = "grey", size = 0.15) +
  geom_sf_label(data = data_joined, aes(label = ..your_label_vector.. )) +
  theme_void() + # position matters due to legend bottom
  scale_fill_gradient2(low = 'white', high = 'red') +
  theme(legend.position = "bottom",
        legend.spacing.y = grid::unit(-0.45, "cm"),
        legend.spacing.x = unit(1.0, 'cm'),
        plot.title = element_text(size=22,hjust = 0.5) # title of plot
  ) + # 50 cm equals to 5 cm because of issue #2398
  labs(fill = "whatever you want") +
  geom_point(data = shape_df, 
             aes(x = long, y = lat, group = NULL,fill = NULL, 
                 size = n), color = "green",alpha = I(6/10), 
             show.legend = FALSE) + 
  ggtitle('whatever you want more')