2016-04-28 120 views
3

我想R:如何通过数据值对voronoi tesselation进行着色?

  1. 从SpatialPointDataFrame确定R中创建的Voronoi镶嵌
  2. 获得SpatialPolygonDataFrame OK
  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

然而,使用上述沃罗诺伊功能,在我voronoipolygons我只有一个变量:“假的”。但是,我想通过变量“z”来给我的多边形着色 - 这不包含在我的.voro多边形中。

voronoipolygons = function(layer) { 
    require(deldir) 
    crds = [email protected] 
    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"变量添加到[email protected],因为值的顺序已更改。我的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 
[email protected]$z<-df$z ## !!! can't use this, because this change order of values in df !!! 
spplot(df.voro, "z") 

回答

3

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

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

voronoipolygons2 = function(layer) { 
    require(deldir) 
    crds = [email protected] 
    z = deldir(crds[,1], crds[,2]) 
    w = tile.list(z) 
    my.variable = [email protected][,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')))) 
} 

通过修改维诺函数创建沃罗诺伊镶嵌多边形:

df.voro2 <- voronoipolygons2(df) 

检查我voronoi2数据看起来像

> [email protected] 
    dummy my.data 
1  1  1 
2  2  2 
3  3  5 
4  4  8 
5  5  4 

以及它们是如何从voronoi1数据不同

> [email protected] 
    dummy 
1  1 
2  2 
3  3 
4  4 
5  5 

显示两者在一张纸上

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 spplots)

enter image description here

相关问题