rplot3drgl

How to plot log transformed data in rgl 3d plot?


I do a 3d plot with the rgl package and I want to use a log transformed z axes. What I do is:

# Dummy data.
x <- seq(1, 2, length = 10); y <- seq(1, 3, length = 10)
z <- outer(x, y, function(x, y) x + exp(y)); z <- log(z)
# Plot.
rgl::persp3d(x, y, z, col= "white")
rgl::surface3d(x, y, z, front = "lines")

Log Plot

The 3d object looks as I expect. But I want the z labels not to say the log values, instead I want it to show the raw values, i.e. where the 3 is there should be a 20 (since exp(3) is approx. 20). So I am looking for some equivalent to ggplot2::scale_y_continuous(trans= "log10") as discussed here. How to do this with the rgl package?


Solution

  • One way is to tell persp3d() to skip the axes, then use calls like axes3d(), box3d(), axis3d() or bbox3d()to draw them where you want them. For example,

    library(rgl)
    
    x <- seq(1, 2, length = 10); y <- seq(1, 3, length = 10)
    z <- outer(x, y, function(x, y) x + exp(y)); z <- log10(z)
    # Plot.
    open3d()
    persp3d(x, y, z, col= "white", polygon_offset = 1)
    surface3d(x, y, z, front = "lines")
    
    zticks <- pretty(z)
    zlabels <- signif(10^zticks, 2)
    
    axes3d(zat = zticks, zlab = zlabels)
    

    The help page for the latest version (1.3.14) shows a slightly simpler way using axes3d().