使用spplot过度绘制两个SpatialPolygonsDataFrame

时间:2012-10-13 13:33:40

标签: r spatial

我有一堆数据,我在县一级绘制了without borders。我想加入州界。我有一个状态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

1 个答案:

答案 0 :(得分:10)

要使用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]))

enter image description here