简体   繁体   中英

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

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

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:

在此处输入图片说明

there is now the convenience show2d function available to produce the desired 2D projections

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

show2d({
  par(mar=c(0,0,0,0))
  plot(x = df$x, y = df$y, 
       col = "black")
})

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM