Show Sidebar Hide Sidebar

3D Surface Lighting in R in R

How to add lighting effects in 3D R Plots.

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

Ambient

p1 <- plot_ly(z = ~volcano, scene='scene1', lighting = list(ambient = 0.2)) %>%
  add_surface(showscale=FALSE)

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

p <- subplot(p1, p2) %>%
  layout(title = "Ambient Lighting",
         grid = list(rows = 1, columns = 2,
                     pattern = 'independent'),
         scene = list(domain=list(column = 0),
                      aspectmode='cube'),
         scene2 = list(domain=list(column = 1),
                       aspectmode='cube'))

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="3d-surface-ambient")
chart_link

Roughness

p1 <- plot_ly(z = ~volcano, scene='scene1', lighting = list(roughness = 0.1)) %>%
  add_surface(showscale=FALSE)

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

p <- subplot(p1, p2) %>%
  layout(title = "Roughness",
         grid = list(rows = 1, columns = 2,
                     pattern = 'independent'),
         scene = list(domain=list(column = 0),
                      aspectmode='cube'),
         scene2 = list(domain=list(column = 1),
                       aspectmode='cube'))

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="3d-surface-roughness")
chart_link

Diffuse

p1 <- plot_ly(z = ~volcano, scene='scene1', lighting = list(diffuse = 0.1)) %>%
  add_surface(showscale=FALSE)

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

p <- subplot(p1, p2) %>%
  layout(title = "Diffuse Reflection",
         grid = list(rows = 1, columns = 2,
                     pattern = 'independent'),
         scene = list(domain=list(column = 0),
                      aspectmode='cube'),
         scene2 = list(domain=list(column = 1),
                       aspectmode='cube'))

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="3d-surface-diffuse")
chart_link

Specular

p1 <- plot_ly(z = ~volcano, scene='scene1', lighting = list(specular = 0.1)) %>%
  add_surface(showscale=FALSE)

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

p <- subplot(p1, p2) %>%
  layout(title = "Specular Reflection",
         grid = list(rows = 1, columns = 2,
                     pattern = 'independent'),
         scene = list(domain=list(column = 0),
                      aspectmode='cube'),
         scene2 = list(domain=list(column = 1),
                       aspectmode='cube'))

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="3d-surface-specular")
chart_link

Fresnel

p1 <- plot_ly(z = ~volcano, scene='scene1', lighting = list(fresnel = 0.1)) %>%
  add_surface(showscale=FALSE)

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

p <- subplot(p1, p2) %>%
  layout(title = "Fresnel",
         grid = list(rows = 1, columns = 2,
                     pattern = 'independent'),
         scene = list(domain=list(column = 0),
                      aspectmode='cube'),
         scene2 = list(domain=list(column = 1),
                       aspectmode='cube'))

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="3d-surface-fresnel")
chart_link

Reference

See https://plot.ly/r/reference/#surface-lighting for more information and options!

Still need help?
Contact Us

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