3D Mesh Plots in R
How to make interactive 3D mesh plots in R.
Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Sign up for early access now.
Basic 3D Mesh Plot
library(plotly)
x <- runif(50, 0, 1)
y <- runif(50, 0, 1)
z <- runif(50, 0, 1)
fig <- plot_ly(x = ~x, y = ~y, z = ~z, type = 'mesh3d')
fig
Click to copy
Tetrahedron Mesh Plot
library(plotly)
fig <- plot_ly(type = 'mesh3d',
x = c(0, 1, 2, 0),
y = c(0, 0, 1, 2),
z = c(0, 2, 0, 1),
i = c(0, 0, 0, 1),
j = c(1, 2, 3, 2),
k = c(2, 3, 1, 3),
intensity = c(0, 0.33, 0.66, 1),
color = c(0, 0.33, 0.66, 1),
colors = colorRamp(c("red", "green", "blue"))
)
fig
Click to copy
Cube Mesh Plot
library(plotly)
fig <- plot_ly(type = 'mesh3d',
x = c(0, 0, 1, 1, 0, 0, 1, 1),
y = c(0, 1, 1, 0, 0, 1, 1, 0),
z = c(0, 0, 0, 0, 1, 1, 1, 1),
i = c(7, 0, 0, 0, 4, 4, 6, 6, 4, 0, 3, 2),
j = c(3, 4, 1, 2, 5, 6, 5, 2, 0, 1, 6, 3),
k = c(0, 7, 2, 3, 6, 7, 1, 1, 5, 5, 7, 6),
intensity = seq(0, 1, length = 8),
color = seq(0, 1, length = 8),
colors = colorRamp(rainbow(8))
)
fig
Click to copy
Reference
See https://plotly.com/r/reference/#mesh3d for more information and chart attribute 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)
Click to copy