I have a model and I want to use the surf3D function in R, and produce a plot similar to the following (the image is the example from "ggRandomForests: Random Forests for Regression").

Example data available at


rfsrc_data <- rfsrc(rtp~., data=data)

From this point, I understand that I need to create a partial surface and then use the surf3D similarly to the example from ggRandomForests: Random Forests for Regression, however, I am lost with the partial surfaces.

surf3D(x=srf$x, y=srf$y, z=srf$z, col=topo.colors(10),
 colkey=FALSE, border = "black", bty="b2",
 shade = 0.5, expand = 0.5,
 lighting = TRUE, lphi = -50,
 xlab="Lower Status", ylab="Average Rooms", zlab="Median Value"

enter image description here


1 Answer 1


You need to create a bivariate partial dependency plot first. I believe the function you are after to create the object to pass to plot3D::surf3D is ggRandomForests::partial.rfsrc, ggRandomForests::gg_partial_coplot or ggRandomForests::gg_partial and you can actually call the plot object using the plot generic but, probably not a 3D object.

I would honestly recommend that, unless there is something specific that you are after in the ggRandomForests package, the pdp package is far more flexible for exploring partial dependency and can support randomForest or ranger objects, along with a large variety of other model object types. For bivariate partial dependency and associated 3D plots, see the section on Multi-predictor PDP's in the linked vignette.

Here is a quick worked example of creating a bivariate partial dependency plot, with pdp, using your data and random forests from the ranger package.


rtp <- read.csv("https://www.dropbox.com/s/hho5sgwjhlk4185/data.csv?raw=1")

( mdl <- ranger::ranger(rtp~., data=rtp) )

bvpd <- pdp::partial(mdl, pred.var = c("pp", "mv"), 
                     grid.resolution = 40)                
  pdp::plotPartial(bvpd, levelplot = FALSE, zlab = "rtp", drape = TRUE,
                   colorkey = FALSE, screen = list(z = -20, x = -60),
                   palette = c("viridis", "magma", "inferno",
                   "plasma", "cividis"))    

Bivariate partial plot of pp and mv

  • Thank you @Jeffrey Evans! I did not know that I could use pdp for 3d surfaces! Thank you, very beautiful!
    – tibi
    Feb 24, 2021 at 7:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.