Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Snap polygons together in R ggplot

enter image description hereI 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

like image 514
ZAC Avatar asked Nov 26 '25 06:11

ZAC


1 Answers

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
like image 102
hrbrmstr Avatar answered Nov 28 '25 18:11

hrbrmstr