Show Sidebar Hide Sidebar

3D Axes in R

How to format axes of 3d plots 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.9.1'

Range of Axes

library(plotly)

axx <- list(
  nticks = 4,
  range = c(-25,75)
)

axy <- list(
  nticks = 4,
  range = c(-25,75)
)

axz <- list(
  nticks = 4,
  range = c(0,50)
)

x <- 70*(runif(70, 0, 1))
y <- 55*(runif(70, 0, 1))
z <- 40*(runif(70, 0, 1))

p <- plot_ly(x = ~x, y = ~y, z = ~z, type = 'mesh3d') %>%
  layout(scene = list(xaxis=axx,yaxis=axy,zaxis=axz))

p

Fixed Ratio Axes

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,

                      # select the type of aspectmode
                      aspectmode='cube'),
         scene2 = list(domain=list(x=c(0.5,1),y=c(0.5,1)),
                       xaxis=axx, yaxis=axx, zaxis=axx,
                       aspectmode='data'),
         scene3 = list(domain=list(x=c(0,0.5),y=c(0,0.5)),
                       xaxis=axx, yaxis=axx, zaxis=axx,
                       aspectmode='auto'),
         scene4 = list(domain=list(x=c(0.5,1),y=c(0,0.5)),
                       xaxis=axx, yaxis=axx, zaxis=axx,
                       aspectmode='manual',

                       # set your manual fixed aspect ratio
                       aspectratio = list(x=1, y=1, z=2)))

p

Set Axes Title

library(plotly)

axx <- list(
  title = "X AXIS TITLE"
)

axy <- list(
  title = "Y AXIS TITLE"
)

axz <- list(
  title = "Z AXIS TITLE"
)

x <- 70*(runif(70, 0, 1))
y <- 55*(runif(70, 0, 1))
z <- 40*(runif(70, 0, 1))

p <- plot_ly(x = ~x, y = ~y, z = ~z, type = 'mesh3d') %>%
  layout(scene = list(xaxis=axx,yaxis=axy,zaxis=axz))

p

Ticks Formatting

library(plotly)

axx <- list(
  ticketmode = 'array',
  ticktext = c("Huey", "Dewey", "Louie"),
  tickvals = c(0,25,50),
  range = c(-25,75)
)

axy <- list(
  nticks = 4,
  range = c(-25,75)
)

axz <- list(
  nticks = 4,
  range = c(0,50)
)

x <- 70*(runif(70, 0, 1))
y <- 55*(runif(70, 0, 1))
z <- 40*(runif(70, 0, 1))

p <- plot_ly(x = ~x, y = ~y, z = ~z, type = 'mesh3d') %>%
  layout(scene = list(xaxis=axx,yaxis=axy,zaxis=axz))

p

Background and Grid Color

library(plotly)

axx <- list(
  backgroundcolor="rgb(200, 200, 230",
  gridcolor="rgb(255,255,255)",
  showbackground=TRUE,
  zerolinecolor="rgb(255,255,255"
)

axy <- list(
  backgroundcolor="rgb(230, 200,230)",
  gridcolor="rgb(255,255,255)",
  showbackground=TRUE,
  zerolinecolor="rgb(255,255,255"
)

axz <- list(
  backgroundcolor="rgb(230, 230,200)",
  gridcolor="rgb(255,255,255)",
  showbackground=TRUE,
  zerolinecolor="rgb(255,255,255"
)

x <- 70*(runif(70, 0, 1))
y <- 55*(runif(70, 0, 1))
z <- 40*(runif(70, 0, 1))

p <- plot_ly(x = ~x, y = ~y, z = ~z, type = 'mesh3d') %>%
  layout(scene = list(xaxis=axx,yaxis=axy,zaxis=axz))

p

Reference

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