Reputation: 135
I use the following code to create a a 3d scatterplot in plotly:
library(plotly)
A <- c(50,20,0)
B <- c(50,0,30)
C <- c(50,0,0)
D <- c(50,20,30)
E <- c(0,0,30)
F <- c(0,20,0)
G <- c(0,0,0)
H <- c(0,20,30)
classes <- c("A","B","C","D",
"E","F","G","H")
conceptual <- rbind(data.frame(),A,B,C,D,
E,F,G,H)
colnames(conceptual) <- c("X","Y","Z")
conceptual$labels <- classes
scene = list(camera = list(eye = list(x = 2.5, y = -1.5, z = 1.25)))
p <- plot_ly(conceptual, x = ~X, y = ~Y, z = ~Z, text = ~labels) %>%
add_markers() %>%
add_text() %>%
layout(scene=scene,showlegend = FALSE)
p
And get this plot: plot
My question is, how can I add a line that will show the outer side of the box on the axis? something like that: required plot
Upvotes: 2
Views: 241
Reputation: 24262
library(plotly)
A <- c(50,20,0); B <- c(50,0,30); C <- c(50,0,0); D <- c(50,20,30)
E <- c(0,0,30); F <- c(0,20,0); G <- c(0,0,0); H <- c(0,20,30)
classes <- c("A","B","C","D","E","F","G","H")
conceptual <- rbind(data.frame(),A,B,C,D,E,F,G,H)
colnames(conceptual) <- c("X","Y","Z")
conceptual$labels <- classes
l1 <- subset(conceptual, labels %in% c("E","B"))
l2 <- subset(conceptual, labels %in% c("D","B"))
l3 <- subset(conceptual, labels %in% c("C","B"))
scene = list(camera = list(eye = list(x = 2.5, y = -1.5, z = 1.25)))
p <- plot_ly(conceptual, x = ~X, y = ~Y, z = ~Z, text = ~labels) %>%
add_markers() %>%
add_text() %>%
add_trace(x=~X, y=~Y, z=~Z, data=l1,
type='scatter3d', mode='lines',
line = list(color = "black", width = 4, dash='dash')) %>%
add_trace(x=~X, y=~Y, z=~Z, data=l2,
type='scatter3d', mode='lines',
line = list(color = "black", width = 4, dash='dash')) %>%
add_trace(x=~X, y=~Y, z=~Z, data=l3,
type='scatter3d', mode='lines',
line = list(color = "black", width = 4, dash='dash')) %>%
layout(scene=scene,showlegend = FALSE)
p
Upvotes: 1