首页 > 解决方案 > ggplot sf_geom 在多个区域之间创建自定义边框

问题描述

我想在我的地图的多个子部分之间创建边界,如下所示:

https://stackoverflow.com/a/49523256/9829458

但是,它在我的 R 中不起作用。只绘制了这张地图的外部边框。当我在我的数据集上使用此代码时,我遇到了同样的问题......

我的数据:

city_code  Name   Long Lat    Groups
   <chr> <chr>     <dbl>    <dbl> <dbl>
 1 34001 ABEI…   724751. 6262333.     9
 2 34002 ADIS…   734961. 6270688.    10
 3 34003 AGDE    739245. 6245728.     7
 4 34004 AGEL    688135. 6249905.     4
 5 34005 AGON…   758530. 6311345.    20
 6 34006 AIGNE   683215. 6247000.     4
 7 34007 AIGU…   685638. 6249976.     4
 8 34008 LES …   705573. 6274482.     6
 9 34009 ALIG…   727555. 6263258.     9
10 34010 ANIA…   747789. 6287511.    18

我的地图:

read_sf("Map.shp")  %>%  
   mutate(Groups = as.factor(Groups)) %>% 
   mutate(Groups = factor(Groups, levels = c(paste0(1:23)))) %>%
   ggplot() + 
      geom_sf(aes(fill = Groups)) + 
      theme_bw()

所以,就我而言,我想在我的地图上绘制“组”边界,同时看到城市边界(并保存fill = Groups颜色)。

标签: rggplot2sf

解决方案


解决方案在原始帖子中进行了编辑:

read_sf("Map.shp")  %>%  
mutate(Groups = as.factor(Groups)) %>% 
        mutate(Groups = factor(Groups, levels = c(paste0(1:23)))) %>%
      ggplot() +
        geom_sf(aes(fill = Groups),  size = 0.4) +
        geom_sf(fill = "transparent", color = "Black", size = 1, data = . %>% group_by(Groups) %>% summarise()) +
        theme_bw() 

推荐阅读