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