R语言 如何在shapefileMap中高亮显示区域?

hpxqektj  于 2023-06-03  发布在  其他
关注(0)|答案(1)|浏览(143)

我有下面的shp文件包含在摩洛哥各省的信息。数据如下所示:

> library(sf)
> library(haven)
> library(ggplot2)
> library(tidyverse)
> morocco_shp <- read_sf("decoupage/Provinces_2015.shp")
> str(morocco_shp)
sf [75 × 22] (S3: sf/tbl_df/tbl/data.frame)
 $ OBJECTID_1: int [1:75] 1 2 3 4 5 6 7 8 9 10 ...
 $ NAME      : chr [1:75] "Assa Zag" "Guelmim" "Tan Tan" "Tata" ...
 $ pop_maroca: num [1:75] 44120 187613 1060302 117809 596891 ...
 $ pop_etrang: num [1:75] 4 195 5299 32 3708 ...
 $ pop_total_: num [1:75] 44124 187808 1065601 117841 600599 ...
 $ menages_14: num [1:75] 44124 187808 1065601 117841 600599 ...
 $ class_pop : int [1:75] 3 3 3 3 2 2 2 3 2 3 ...
 $ SRF       : num [1:75] 22741 11002 9498 26216 2430 ...
 $ DENSITE_14: num [1:75] 1.94 17.07 112.19 4.5 247.19 ...
 $ RuleID    : int [1:75] 1 2 5 1 5 4 5 3 5 2 ...
 $ Shape_Leng: num [1:75] 6.72 6.47 4.16 10.61 2.93 ...
 $ Shape_Area: num [1:75] 2.088 1.018 0.872 2.442 0.229 ...
 $ menage_urb: num [1:75] 3931 30332 253831 7992 124107 ...
 $ pop_urb   : num [1:75] 27333 139246 1005041 40820 508155 ...
 $ etrange_ur: num [1:75] 2 144 5259 24 3631 ...
 $ marocain_u: num [1:75] 27331 139102 999782 40796 504524 ...
 $ menage_rur: num [1:75] 1277 9882 12907 14367 19645 ...
 $ pop_rur   : num [1:75] 16791 48562 60560 77021 92444 ...
 $ etranger_r: num [1:75] 2 51 40 8 77 128 9 57 191 91 ...
 $ marocain_r: num [1:75] 16789 48511 60520 77013 92367 ...
 $ prcnt_urb : num [1:75] 61.9 74.1 94.3 34.6 84.6 ...
 $ geometry  :sfc_POLYGON of length 75; first list element: List of 1
  ..$ : num [1:348, 1:2] -8.67 -8.74 -8.76 -8.79 -8.8 ...
  ..- attr(*, "class")= chr [1:3] "XY" "POLYGON" "sfg"
 - attr(*, "sf_column")= chr "geometry"
 - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA NA NA NA ...
  ..- attr(*, "names")= chr [1:21] "OBJECTID_1" "NAME" "pop_maroca" "pop_etrang" ...

将数据绘制为以下Map:

> morocco_shp %>% ggplot() + geom_sf() + theme_bw()

现在我的问题是我想在Map中突出显示一个省,例如Tan Tan(见下图),我该怎么做?先谢谢你?

wkyowqbh

wkyowqbh1#

最简单的方法(虽然可能不是最有效的方法)是将另一个图层添加到ggplot,并将shapefile过滤到您想要突出显示的区域。我无法访问您发布的摩洛哥shapefile,但这里有一个来自sfnc数据集的示例。

library(sf)
library(ggplot2)

nc_shp<-sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)

ggplot()+
#Add base map
  geom_sf(data = nc_shp)+
#Add additional layer with just the region of interest.
  geom_sf(data = nc_shp[nc_shp$NAME=="Mecklenburg",],fill = "grey10")+
  theme_bw()

您也可以通过自定义填充(如scale_fill_manual)来实现这一点,但这似乎比它的价值更麻烦。

相关问题