我有两个shapefile代表相邻县。我想在R的ggplot中清理它们的公共边界。我知道在ArcGIS中存在一种简单的方法,但是我更喜欢坚持使用R。我找到了spdep包并尝试使用它:
ob <- SpatialPolygons(ob@polygons, proj4string=ob@proj4string)
ob2<- SpatialPolygons(ob2@polygons, proj4string=ob2@proj4string)
poly2nb(pl=list(ob,ob2))
但这导致以下错误:
错误:extends(class(pl),“ SpatialPolygons”)不是TRUE
最佳答案
我们没有您的文件,但是这里有些类似:
# 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