Show Sidebar Hide Sidebar

3D Subplots in R

How to create 3D subplots in R with Plotly.

New to Plotly?

Plotly is a free and open-source graphing library for R. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

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

p

Reference

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