Search code examples
rshapefiler-sf

How to merge two polygon elements into one?


I have the shapefile below containing information on Moroccan provinces. The shapefile looks as follows:

> library(sf)
> library(ggplot2)
> 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" ...
> 

Now I want to take two provinces, "Es Semara" and "Tarfaya" (shown below):

> library(patchwork)
> E <- ggplot() + geom_sf(data = morocco_shp) + geom_sf(data = morocco_shp[morocco_shp$NAME=="Es Semara",], fill = "grey10") + theme_bw()
> T <- ggplot() + geom_sf(data = morocco_shp) + geom_sf(data = morocco_shp[morocco_shp$NAME=="Tarfaya",], fill = "grey10") + theme_bw()
> E + T

enter image description here

And merge them into one province, which I would call ET, in such a way that when I run the following command:

ggplot() + geom_sf(data = morocco_shp) + geom_sf(data = morocco_shp[morocco_shp$NAME=="ET",], fill = "grey10") + theme_bw()

It gives me the following map:

enter image description here

Can someone please help do this? Thank you in advance.


Solution

  • You can create a new variable that indicates which polygons should be merged and which ones not with mutate. Then, you can group by this new variable and use summarise to merge the desired polygons. Since you did not share your data, here is an example using the nc data from sf. The merged polygons are located in the center of the plot.

    library(sf)
    library(dplyr)
    
    nc <- st_read(system.file("shape/nc.shp", package="sf"))
    
    # Original data
    plot(nc["NAME"])
    

    original polygons

    resul <- nc |>
      mutate(group = if_else(NAME %in% c("Randolph", "Chatham"), "group", NAME)) |>
      group_by(group) |>
      summarise()
    
    plot(resul)
    

    Merged polygons