I have two shapefiles representing neighboring counties. I would like to clean up their common boundary in my ggplot in R. I know there exists an easy way to do this in ArcGIS, but I'd prefer to stick to using R. I found the spdep package and tried to use it:
ob <- SpatialPolygons(ob@polygons, proj4string=ob@proj4string)
ob2<- SpatialPolygons(ob2@polygons, proj4string=ob2@proj4string)
poly2nb(pl=list(ob,ob2))
But this resulted in the following error:
Error: extends(class(pl), "SpatialPolygons") is not TRUE
We don't have your file but here's something similar:
# devtools::install_github("walkerke/tigris")
library(sp)
library(Matrix)
library(spdep)
library(tigris)
me <- counties("maine", detailed=FALSE)
nh <- counties("nh", detailed=FALSE)
neighbors <- poly2nb(as.SpatialPolygons.PolygonsList(c(me@polygons, nh@polygons)))
neighbors
## Neighbour list object:
## Number of regions: 26
## Number of nonzero links: 114
## Percentage nonzero weights: 16.86391
## Average number of links: 4.384615
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With