Search code examples
rrglrastervistopographyplot3d

Displaying multiple 3d plots in the same window using plot3D {rasterVis} in R


I am interested in making two 3d topographic plots that display side by side in the same Xquartz device window. Displaying two 3d plots in the same window is straightforward using the rgl package - there are plenty of examples in the documentation using mfrow3d() and other methods.

However, I need to use the plot3D() function from the rasterVis package. The reason I am using plot3D() from rasterVis, rather than something like surface3d() from rgl, is that I need to use the drape argument in plot3D() to display values from a raster as colors on a 3d topographic map (and this raster has different values than the one that creates the z axis on the plot). If anyone has tips on something analogous to drape within an rgl function, I would also be interested in that!

When I try to use mfrow3d() along with the plot3D() function, it brings up a series of blank device windows instead of displaying the two plots side by side in the same window.

Here is some code to make a topographic map using plot3D, from the rasterVis documentation:

data(volcano)
r <- raster(volcano)
extent(r) <- c(0, 610, 0, 870)
levelplot(r, col.regions=terrain.colors)
plot3D(r)

And here is where I try to use mfrow3d to plot 2 identical volcano plots side by side, one in blue and one in red, which I adapted from the rgl documentation:

volcanos <- list(r, r)
col <- c("blue", "red")
open3d()
mfrow3d(1,2)
for (i in 1:2) {
next3d()   
plot3D(volcanos[[i]], col=col[i])
}

Is what I am trying to do even possible with plot3D from rasterVis?


Solution

  • The current version of rasterVis::plot3D opens a new device with each call. I have modified its code to test if there is an active device, and open a new one only if needed. With this commit your example works as expected. You should install the development version of rasterVis with devtools::install_github('oscarperpinan/rasterVis').