Search code examples
rdata-visualizationrgl

In R (rgl), how to print shadows of points in plot3d?


In R, using package rgl, I'd like to add the shadows of the points in plot3d(), just like in the image below.

plot3d with shadows

I've added the bottom grid using grid3d(), but still have no clue on how to add the shadows. If I plot the same points changing the 3rd axis value to its minimum value (-100 in the image), the plot area automatically increases, leaving a gap between the points and the grid. Is there a better way to do that?

I think it was obvious from the question, but here is a sample code:

library(rgl)
df <- data.frame(x=rnorm(100),
                 y=rnorm(100),
                 z=rnorm(100))
plot3d(df)
grid3d('z')

Solution

  • The idea of setting z to the minimal value fails because rgl makes the bounding region slightly bigger. But you can grab the z value from the grid, and use that. You can also tell rgl not to expand the bounding box to include the new points. This code does both things:

    library(rgl)
    df <- data.frame(x=rnorm(100),
                     y=rnorm(100),
                     z=rnorm(100))
    plot3d(df)
    id <- grid3d('z')                            # Get id values for grid
    gridz <- rgl.attrib(id[1], "vertices")[1,3]  # Use the first z value
    save <- par3d(ignoreExtent = TRUE)           # Ignore points for bbox
    with(df, points3d(x, y, gridz, col = "gray"))# Plot the "shadows"
    par3d(save)                                  # Restore bbox status
    

    Here's what I get:

    enter image description here