3D Surface Lighting in R
How to add lighting effects in 3D R Plots.
Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Try Plotly Studio now.
Ambient
library(plotly)
fig1 <- plot_ly(z = ~volcano, scene='scene1', lighting = list(ambient = 0.2))
fig1 <- fig1 %>%
add_surface(showscale=FALSE)
fig2 <- plot_ly(z = ~volcano, scene='scene2', lighting = list(ambient = 0.9))
fig2 <- fig2 %>%
add_surface(showscale=FALSE)
fig <- subplot(fig1, fig2)
fig <- fig %>%
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'))
fig
Roughness
library(plotly)
fig1 <- plot_ly(z = ~volcano, scene='scene1', lighting = list(roughness = 0.1))
fig1 <- fig1 %>%
add_surface(showscale=FALSE)
fig2 <- plot_ly(z = ~volcano, scene='scene2', lighting = list(roughness = 0.9))
fig2 <- fig2 %>%
add_surface(showscale=FALSE)
fig <- subplot(fig1, fig2)
fig <- fig %>%
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'))
fig
Diffuse
library(plotly)
fig1 <- plot_ly(z = ~volcano, scene='scene1', lighting = list(diffuse = 0.1))
fig1 <- fig1 %>%
add_surface(showscale=FALSE)
fig2 <- plot_ly(z = ~volcano, scene='scene2', lighting = list(diffuse = 0.9))
fig2 <- fig2 %>%
add_surface(showscale=FALSE)
fig <- subplot(fig1, fig2)
fig <- fig %>%
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'))
fig
Specular
library(plotly)
fig1 <- plot_ly(z = ~volcano, scene='scene1', lighting = list(specular = 0.1))
fig1 <- fig1 %>%
add_surface(showscale=FALSE)
fig2 <- plot_ly(z = ~volcano, scene='scene2', lighting = list(specular = 1.9))
fig2 <- fig2 %>%
add_surface(showscale=FALSE)
fig <- subplot(fig1, fig2)
fig <- fig %>%
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'))
fig
Fresnel
library(plotly)
fig1 <- plot_ly(z = ~volcano, scene='scene1', lighting = list(fresnel = 0.1))
fig1 <- fig1 %>%
add_surface(showscale=FALSE)
fig2 <- plot_ly(z = ~volcano, scene='scene2', lighting = list(fresnel = 4.5))
fig2 <- fig2 %>%
add_surface(showscale=FALSE)
fig <- subplot(fig1, fig2)
fig <- fig %>%
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'))
fig
Reference
See https://plotly.com/r/reference/#surface-lighting for more information and options!
What About Dash?
Dash for R is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.
Learn about how to install Dash for R at https://dashr.plot.ly/installation.
Everywhere in this page that you see fig
, you can display the same figure in a Dash for R application by passing it to the figure
argument of the Graph
component from the built-in dashCoreComponents
package like this:
library(plotly)
fig <- plot_ly()
# fig <- fig %>% add_trace( ... )
# fig <- fig %>% layout( ... )
library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)
app <- Dash$new()
app$layout(
htmlDiv(
list(
dccGraph(figure=fig)
)
)
)
app$run_server(debug=TRUE, dev_tools_hot_reload=FALSE)