I'm trying to identify the common borders of two different polygons using the sf_intersection()
function from the sf
package.
I tried this simple approach in my data, which comes from a shapefile, but it's not working exactly as I expected.
My data is the shapefile names "zones" from this repository, and this is what I've tried:
library(sf)
library(ggplot2)
zones <- st_read('./Data/zones.shp')
zones$id <- seq(nrow(zones))
borders <- st_intersection(zones, zones)
borders <- borders[borders$id != borders$id.1, ]
ggplot() +
geom_sf(data = zones, color='red', fill=NA) +
geom_sf(data = borders, color = 'navy')
The final plot yields this result:
If you look carefully, you'll note that there are some portions of the inner line of the polygons that are not part of the line in borders
(they are red and not blue).
I don't know why this is happening. Any hint or advice will be much appreciated. Thanks!