Search code examples
rplotcolorsvoronoi

R: How to color voronoi tesselation by data value?


I want to

  1. create Voronoi tesselation in R from SpatialPointDataFrame OK
  2. obtain SpatialPolygonDataFrame OK
  3. colorize it by values in my original SpatialPointDataFrame HOW???

as far: I've created and updated Voronoi tesselation, following: and updated here: https://gis.stackexchange.com/questions/190917/r-voronoi-tesselation-from-long-lat-data.

I know that I can color it by library("dismo") : https://gis.stackexchange.com/questions/136542/r-function-for-thiessen-polygons

However, using Voronoi function above, in my voronoipolygons I have only one variable: "dummy". However, I want to color my polygon by variable "z" - which is not more included in my .voro polygon.

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'))))
}

My question is: how to colorize my .voro polygons by "z" variable, or/and how to directly include it in voronoipolygons() function above? I can't just add "z" variable into .voro@data, because the order of values is altered. My R skill are not so strength yet.. Thank you a lot!

Dummy data:

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

Solution

  • I've got it !! How to modify the Voronoi function

    I need to firstly read my.variable from my data.frame: my.variable = layer@data[,1] and then add it to my SP object as: 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'))))
    }
    

    create voronoi tesselation polygon by modified Voronoi function:

    df.voro2 <- voronoipolygons2(df)  
    

    check how my voronoi2 data looks like

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

    and how they are different from voronoi1 data

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

    display both spplots on one 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 ;)

    enter image description here