Search code examples
rshapefile

How to highlight a region in a shapefile map?


I have the following shp file containing information on provinces in Morocco. The data looks as follows:

> 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" ...

Plotting the data results in the following map:

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

enter image description here

Now my question is: I want to highlight one province in the map, say Tan Tan for example (see map below), how can I do that? Thank you in advance? enter image description here


Solution

  • The simplest way to do it, though perhaps not the most efficient, is to add another layer to the ggplot with the shapefile filtered just to the region that you want to highlight. I don't have access to the Morocco shapefile that you posted, but here's an example from the nc dataset that comes from sf.

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

    NC Map with one region highlighted

    You may also be able to do this by playing around with a custom fill (like with scale_fill_manual), but that seems like more trouble than it's worth.