R:如何通过数据值对 voronoi 镶嵌进行着色?

R: How to color voronoi tesselation by data value?

我要

  1. 从 SpatialPointDataFrame 在 R 中创建 Voronoi 细分 OK
  2. 获取SpatialPolygonDataFrame成功
  3. 如何根据我原来的 SpatialPointDataFrame 中的值给它上色???

到目前为止: 我创建并更新了 Voronoi 镶嵌,如下所示: 并在此处更新: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 多边形着色,or/and 如何将其直接包含在上面的 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")

我知道了!!如何修改Voronoi函数

我需要先从我的 data.frame 中读取 my.variable:my.variable = layer@data[,1] 然后将其添加到我的 SP 对象中:y.data = my.variable

voronoipolygons2 = function(layer) {
  require(deldir)
  crds = layer@coords
  z = deldir(crds[,1], crds[,2])
  w = tile.list(z)
  my.variable = layer@data[,1]   ## HERE
  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)), 
                                                         my.data = my.variable, # HERE add new column to my voronoi data 
                                                         row.names=sapply(slot(SP, 'polygons'), 
                                                                          function(x) slot(x, 'ID'))))
}

通过修改后的 Voronoi 函数创建 voronoi 细分多边形:

df.voro2 <- voronoipolygons2(df)  

检查我的 voronoi2 数据是什么样的

> df.voro2@data
  dummy my.data
1     1       1
2     2       2
3     3       5
4     4       8
5     5       4

以及它们与 voronoi1 数据的不同之处

> df.voro@data
  dummy
1     1
2     2
3     3
4     4
5     5

在一个上显示两个 spplots sheet

require(gridExtra)
grid.arrange(spplot(df.voro, "dummy", xlab = "x", ylab = "y", main = "original" ),
             spplot(df.voro2, "my.data", xlab = "x", ylab = "y", main = "z value applied !;-)"))

TRADAAA ;)