Overplotting 2个SpatialPolygonsDataFrames与spplot(Ov

2019-09-24 02:35发布

我有一堆我已经在县一级绘制的数据的, 无国界的 。 我想在国界补充。 我有一个状态shape文件(多边形),但spplot似乎没有任何办法在上图的顶部添加。 有没有办法做到这一点无需重写面板功能取两个SPDFs(这似乎很专业的东西可能是一个问题,其他人)?

这里有一个重复的例子:

library(sp)
Srs1 = Polygons(list(Polygon(cbind(c(2,4,4,1,2),c(2,3,5,4,2)))), "s1")
Srs2 = Polygons(list(Polygon(cbind(c(5,4,2,5),c(2,3,2,2)))), "s2")

county <- SpatialPolygonsDataFrame( SpatialPolygons(list(Srs1,Srs2)), 
                                  data.frame( z=1:2, row.names=c("s1","s2") ) )

SrsA <- Polygons(list(Polygon(cbind(c(3,5,5,1,3),c(3,4,6,5,3)))),"sA")
state <- SpatialPolygonsDataFrame( SpatialPolygons(list(SrsA)),
                                  data.frame( z=1,row.names="sA" ))

spplot( county, zcol="z",col=NA )
spplot( state, add=TRUE ) # Note the add=TRUE does nothing here, but that's the spirit of what I want to accomplish

Answer 1:

要overplot使用spplot功能,您可以使用sp.layout说法。 例如,创建相应的版面项目清单,如

spCounty <- list("sp.polygons", county, col = NA)
spState <- list("sp.polygons", state)

然后绘制,通过上面的列表项的列表中sp.layout参数:

# spplot(county, zcol = "z", col = NA, sp.layout = list(spCounty, spState))
# actually, you only need to pass the second layout item to sp.layout
spplot(county, zcol = "z", col = NA, sp.layout = spState)

如果两个空间data.frames不完全重叠,x和y的限制可能不正确。 您可以通过提取适当的限制,如果有必要纠正这种bbox(obj)

例如,

theMin <- pmin(bbox(county)[,1], bbox(state)[,1])
theMax <- pmax(bbox(county)[,2], bbox(state)[,2])

spplot(county, zcol = "z", col = NA, sp.layout = spState,
  ylim = c(theMin[2], theMax[2]), xlim = c(theMin[1], theMax[1]))



文章来源: Overplotting two SpatialPolygonsDataFrames with spplot
标签: r spatial