Show Sidebar Hide Sidebar

3D Subplots in R

How to create 3D subplots in R with Plotly.

New to Plotly?

Plotly's R library is free and open source!
Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode.
We also have a quick-reference cheatsheet (new!) to help you get started!

Version Check

Version 4 of Plotly's R package is now available!
Check out this post for more information on breaking changes and new features available in this version.

library(plotly)
packageVersion('plotly')
## [1] '4.5.6.9000'

3D Subplot

library(plotly)

# custom grid style
axx <- list(
  gridcolor='rgb(255, 255, 255)',
  zerolinecolor='rgb(255, 255, 255)',
  showbackground=TRUE,
  backgroundcolor='rgb(230, 230,230)'
)

# individual plots
p1 <- plot_ly(z = ~volcano, scene='scene1') %>%
  add_surface(showscale=FALSE)

p2 <- plot_ly(z = ~volcano, scene='scene2') %>%
  add_surface(showscale=FALSE)

p3 <- plot_ly(z = ~volcano, scene='scene3') %>%
  add_surface(showscale=FALSE)

p4 <- plot_ly(z = ~volcano, scene='scene4') %>%
  add_surface(showscale=FALSE)

# subplot and define scene
p <- subplot(p1, p2, p3, p4) %>%
  layout(title = "3D Subplots",
         scene = list(domain=list(x=c(0,0.5),y=c(0.5,1)),
                      xaxis=axx, yaxis=axx, zaxis=axx,
                      aspectmode='cube'),
         scene2 = list(domain=list(x=c(0.5,1),y=c(0.5,1)),
                       xaxis=axx, yaxis=axx, zaxis=axx,
                       aspectmode='cube'),
         scene3 = list(domain=list(x=c(0,0.5),y=c(0,0.5)),
                       xaxis=axx, yaxis=axx, zaxis=axx,
                       aspectmode='cube'),
         scene4 = list(domain=list(x=c(0.5,1),y=c(0,0.5)),
                       xaxis=axx, yaxis=axx, zaxis=axx,
                       aspectmode='cube'))

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = plotly_POST(p, filename="subplot3d/basic")
chart_link

Reference

See https://plot.ly/r/reference/#layout-scene-xaxis for more information and options!

Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.