3D Cone Plots in R
How to make 3D cone plots with Plotly.
New to Plotly?
Plotly is a free and open-source graphing library for R. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.
Basic 3D Cone
library(plotly)
fig <- plot_ly(
type= "cone",
x= 1, y= 1, z= 1,
u= 1, v= 1, w= 0
)
fig <- fig %>%
layout(
scene= list(
camera= list(
eye= list(x= -0.76, y= 1.8, z= 0.92)
)
)
)
fig
Mulitple 3D Cones
library(plotly)
fig <- plot_ly(
type="cone",
x= c(1, 2, 3),
y= c(1, 2, 3),
z= c(1, 2, 3),
u= c(1, 0, 0),
v= c(0, 3, 0),
w= c(0, 0, 2),
sizemode= "absolute",
sizeref= 2,
anchor= "tip",
colorbar= list(
x= 0,
xanchor= "right",
side= "left"
)
)
fig <- fig %>%
layout(
scene= list(
domain= list(x= c(0, 1)),
camera= list(
eye= list(x= -1.57, y= 1.36, z= 0.58)
)
)
)
fig
3D Cone Lighting
library(plotly)
fig <- plot_ly(
type="cone",
y= c(1, 2, 3),
z= c(1, 1, 1),
u= c(1, 2, 3),
v= c(1, 1, 2),
w= c(4, 4, 1),
showscale= F,
hoverinfo= "u+v+w+name"
)
fig <- fig %>%
add_trace(
name= "base",
x= c(1, 1, 1)
)
fig <- fig %>%
add_trace(
name= "opacity=0.3",
x= c(2, 2, 2),
opacity= 0.3
)
fig <- fig %>%
add_trace(
name= "lighting.ambient=0.3",
x= c(3, 3, 3),
lighting= list(ambient= 0.3)
)
fig <- fig %>%
add_trace(
name= "lighting.diffuse=0.3",
x= c(4, 4, 4),
lighting= list(diffuse= 0.3)
)
fig <- fig %>%
add_trace(
name= "lighting.specular=2",
x= c(5, 5, 5),
lighting= list(specular= 2)
)
fig <- fig %>%
add_trace(
name= "lighting.roughness=1",
x= c(6, 6, 6),
lighting= list(roughness= 1)
)
fig <- fig %>%
add_trace(
name= "lighting.fresnel=2",
x= c(7, 7, 7),
lighting= list(fresnel= 2)
)
fig <- fig %>%
add_trace(
name= "lighting.position x=0,y=0,z=1e5",
x= c(8, 8, 8),
lightposition= list(x= 0, y= 0, z= 1e5)
)
fig <- fig %>%
layout(
scene= list(
aspectmode= "data",
camera= list(
eye= list(x= 0.05, y= -2.6, z= 2)
)
),
margin= list(t= 0, b= 0, l= 0, r= 0)
)
fig
3D Cone of Wind Dataset
library(plotly)
library(rjson)
dat <- fromJSON(file='https://raw.githubusercontent.com/plotly/plotly.js/master/test/image/mocks/gl3d_cone-wind.json')
fig <- plot_ly(
type="cone",
x= dat$data[[1]]$x,
y= dat$data[[1]]$y,
z= dat$data[[1]]$z,
u= dat$data[[1]]$u,
v= dat$data[[1]]$v,
w= dat$data[[1]]$w,
text="-> wind <-",
hoverinfo="u+v+w+text",
marker = list(
colorscale = "Viridis",
cmin=0,
cmax=100
)
)
fig <- fig %>%
layout(
scene= list(
aspectratio= list(x= -1.57, y= 1.36, z= 0.58)
)
)
fig
3D Cone Vortex
library(plotly)
library(rjson)
df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/vortex.csv')
fig <- plot_ly(
df,
type="cone",
x= ~x,
y= ~y,
z= ~z,
u= ~u,
v= ~v,
w= ~w,
sizemode= 'absolute',
sizeref= 40
)
fig <- fig %>%
layout(
scene= list(
aspectratio= list(x= 1, y= 1, z= 0.8),
camera = list(eye = list(x= 1.2, y= 1.2, z= 0.6))
)
)
fig
Reference
See https://plotly.com/r/reference/ 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)