9

我有一堆我在县级绘制的数据,没有国界。我想添加州界。我有一个状态 shapefile(多边形),但spplot似乎没有任何方法可以添加到以前的地图之上。有没有办法在不重写面板函数来获取两个 SPDF 的情况下做到这一点(这对于其他人可能遇到的问题似乎非常专业)?

这是一个可重现的示例:

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
4

1 回答 1

12

要使用该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.frame 不完全重叠,则 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]))

在此处输入图像描述

于 2012-10-13T15:39:52.353 回答