我想:
- 从SpatialPointDataFrame中在R中创建泰森多边形(Voronoi tesselation) OK
- 获取SpatialPolygonDataFrame OK
- 通过原始SpatialPointDataFrame中的值对其进行着色 如何实现?
目前为止,我已经按照以下链接创建和更新了Voronoi tesselation,并进行了更新:https://gis.stackexchange.com/questions/190917/r-voronoi-tesselation-from-long-lat-data.
我知道可以借助库“dismo”来对它进行着色:https://gis.stackexchange.com/questions/136542/r-function-for-thiessen-polygons
然而,使用上述Voronoi函数时,在我的voronoipolygons中只有一个变量:“dummy”。然而,我希望根据变量“z”对我的多边形进行着色,这个变量不再包含在我的.voro多边形中。
voronoipolygons = function(layer) {
require(deldir)
crds = layer@coords
z = deldir(crds[,1], crds[,2])
w = tile.list(z)
polys = vector(mode='list', length=length(w))
require(sp)
for (i in seq(along=polys)) {
pcrds = cbind(w[[i]]$x, w[[i]]$y)
pcrds = rbind(pcrds, pcrds[1,])
polys[[i]] = Polygons(list(Polygon(pcrds)), ID=as.character(i))
}
SP = SpatialPolygons(polys)
voronoi = SpatialPolygonsDataFrame(SP, data=data.frame(dummy = seq(length(SP)),
row.names=sapply(slot(SP, 'polygons'),
function(x) slot(x, 'ID'))))
}
我的问题是:如何通过
"z"
变量对我的.voro
多边形进行着色,或/和如何直接将其包含在上述的voronoipolygons()
函数中?我不能只是将"z"
变量添加到.voro@data
中,因为值的顺序被改变了。我的R技巧还不够强大..非常感谢!虚拟数据:
x <- c(32.5, 32.1, 33.5, 32.2, 33.0)
y <- c(-2.2, -3.3, -2.3, -2.9, -3.0)
z <- c(1, 2, 5, 8, 4)
# make df
df<-as.data.frame(cbind(x,y,z))
coordinates(df)<- ~ x + y #make SPDF
df.voro <- voronoipolygons(df) # calculated VORONOI
require('dismo')
spplot(df.voro, "dummy") # colorize Polygons
# add z variable to newly created data
df.voro@data$z<-df$z ## !!! can't use this, because this change order of values in df !!!
spplot(df.voro, "z")