0

How can you smooth the polygons of a map produced with ggplot and sf?

I have used the sf package to extract the polygons from a shapefile


geomunicipios <- st_read("ruta/archivo.shp")
Reading layer `archivo' from data source 
  `ruta\archivo.shp' 
  using driver `ESRI Shapefile'
Simple feature collection with 45 features and 10 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -2.344411 ymin: 37.37375 xmax: -0.647983 ymax: 38.75509
Geodetic CRS:  WGS 84

And ggplot2 to plot the map:


rmurcia <- ggplot(data = geomunicipios) + 
 geom_sf(aes(fill=columna),color="#FFFFFF",size=1)

José Carlos
  • 213
  • 1
  • 12
  • 1
    [See here](https://stackoverflow.com/q/5963269/5325862) on making a reproducible example that is easier for folks to help with. Without your shapefile or anything else that recreates the issue, we can't run any of your code, and you haven't posted any of the output, so it's unclear what you're working with and what's not working – camille Dec 21 '21 at 16:20
  • 1
    An answer is there: https://gis.stackexchange.com/questions/243569/simplify-polygons-of-sf-object – manro Dec 21 '21 at 21:03
  • The download can be done from: http://centrodedescargas.cnig.es/CentroDescargas/index.jsp - Información geográfica de referencia - Límites municipales, provinciales y autonómicos - Descargar - lineas_limite.zip. And the path in the uncompressed folder: SIGLIM_Publico_INSPIRE - SHP_ETRS89 - recintos_municipales_inspire_peninbal_etrs89 - recintos_municipales_inspire_peninbal_etrs89.shp – José Carlos Jan 04 '22 at 10:45

1 Answers1

1

To perform the smoothing of the polygons I have analyzed three alternatives:

  • i. package "smoothr": geosmunicipios <- smooth(geomunicipios, method = "ksmooth", smoothness = 12)

  • ii. package "rmapshaper": geosmunicipios <- ms_simplify(geomunicipios, keep = 0.02500, weighting = 12)

  • iii. package "sf": geosmunicipios <- st_simplify(geomunicipios, dTolerance = 50, preserveTopology = TRUE)

You have to try different values of the parameters to adjust to the needs and obtain the desired result.

To reproduce the case, the download can be done from: centrodedescargas.cnig.es/CentroDescargas/index.jsp

And follow the links: Información geográfica de referencia - Límites municipales, provinciales y autonómicos - Descargar: lineas_limite.zip.

And the path in the uncompressed folder: SIGLIM_Publico_INSPIRE - SHP_ETRS89 - recintos_municipales_inspire_peninbal_etrs89 - recintos_municipales_inspire_peninbal_etrs89.shp

Finally, for this case I have chosen to use rmapshaper, it produces a satisfactory result with a reduced size of the .pdf file, where I include the graphic.

José Carlos
  • 213
  • 1
  • 12