3D surface with a 2D projection using R

Hello everyone,

I need to plot a 3D surface with 2D projections like the one below using R.

It features a 3D density plot, something easy to do in R using plotly, for example. The 2D surfaces on the other hand I've had no luck so far in my search for how to draw them. The best I've found is this example, but it uses Python instead of R.

I have also found that package RSM (Response Surface Methods) may have the tools to draw this graph, but I've studied the package documentation and looked for online examples and so far I have not been able to find anything close to this graph in quality.

Base R function persp looks like it could offer some answer too, but I've had no success using it to draw the 2D projections so far. Package plot3D may also offer clues to the solution.

Thanks in advance for any help on this.

As long as you're producing the surface from a matrix of values, you could use plotly to do something like this:

# 2D kernel density estimation
kd <- with(MASS::geyser, MASS::kde2d(duration, waiting, n = 50))
x <- kd$x
y <- kd$y
# this was rows correspond to y and columns corresponds to x
# (this is the way plotly prefers)
z <- t(kd$z)

plot_ly(x = x, y = y, z = z, type = "surface", size = I(5)) %>%
  # condition on the 10th x value
  add_paths(x = x[10], y = y, z = z[, 10], color = I("red"), linetype = I("dash")) %>%
  # project it to the 'background'
  add_paths(x = min(x), y = y, z = z[, 10], color = I("black")) %>%
  # condition on the 15th y value
  add_paths(x = x, y = y[15], z = z[15, ], color = I("red"), linetype = I("dash")) %>%
  # project it to the 'background'
  add_paths(x = x, y = min(y), z = z[15, ], color = I("black"))

Although this is produced by "coolbutuseless", you may find something useful in here: