Search code examples
r3drasterrastervis

add flat polygon to rasterVis


I would like to create a figure that combines 3D bathymetric grided data (sea) with a 2D polygon (land).

For example, in such a plot (http://chenangliu.info/en/wp-content/uploads/2014/02/wireframe.jpg) I would like to add a flat surface to the purple area, as well as points, text,...

I noticed the package rasterVis, which perfectly draws my bathymetry raster. However, I don't know how I could possibly add land, just as a regular 2D polygon. I tried working around this problem by setting grid values for land area to zero, but the result is not perfect because the boundaries are unsharp, there are lakes,... that should shouldn't be in 3D.

So this works fine:

library(rasterVis)
pal<-colorRampPalette(c("darkblue","lightblue","green"))
    #----this doesn't help
     bathy[bathy>0,]=0
    #----
plot3D(bathy,col=pal)

This doesn't:

polygon3d(europa)  # error: Error: n > 2 is not TRUE

The last function doesn't work [idem for polygon() ], just as my other attempts to do this. I suppose this isn't too hard to do, but I can't finy any examples of this. So any hints would be welcome.


Solution

  • You have to use polygon3d according to its specifications. Since you do not provide data to reproduce your code, I use the example from the help page of plot3D:

    library(raster)
    library(rasterVis)
    library(rgl)
    
    data(volcano)
    r <- raster(volcano)
    extent(r) <- c(0, 610, 0, 870)
    
    plot3D(r)
    

    Now you can add a 3D polygon with the functions defined in the package rgl:

    x <- c(30, 400, 400, 30)
    y <- c(30, 30, 500, 500)
    
    polygon3d(x, y, z=rep(100, 4), col = 'red')
    polygon3d(x, y, z=rep(120, 4), col = 'blue')
    polygon3d(x, y, z=rep(140, 4), col = 'black')