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.5.6.9000'

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

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

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

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

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

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

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

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = plotly_POST(p, filename="axes3d/bg-grid")
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.