I am trying to change markers' colors and symbols according to a categorical variable in r-plotly. But it is not working. Also, I would like to draw "contour lines" around the "walls" of the 3d panel, similar to what I have in the attached figure. Any help is welcome.
# Packages
library(plotly)
library(misc3d)
# Fake data
norm_vec <- function(x) sqrt(sum(x ^ 2))
X3d_norm <- data.frame(T3 = runif(100), T6 = runif(100), P4 = runif(100))
norms <- apply(X3d_norm, 1, norm_vec)
X3d_norm <- X3d_norm / norms
X3d_norm$cluster <- factor(sample(1:2, 100, replace = T), ordered = T)
# Getting octant data
f <- function(x, y, z){x ^ 2 + y ^ 2 + z ^ 2}
R <- 1
x <- y <- z <- seq(0, R, length.out = 100)
g <- expand.grid(x = x, y = y, z = z)
voxel <- array(with(g, f(x, y, z)),dim = c(100, 100, 100))
cont <- computeContour3d(voxel, level = R ^ 2, x = x, y = y, z = z)
idx <- matrix(0:(nrow(cont) - 1), ncol = 3,byrow = TRUE)
# Colors and symbols
pal <- c("#E69F00", "#009E73")
pal <- setNames(pal, c(1, 2))
symb <- c("circle", "square")
symb <- setNames(symb, c(1, 2))
# Plot
plot_ly() %>%
add_mesh(
x = cont[, 1], y = cont[, 2], z = cont[, 3],
i = idx[, 1], j = idx[, 2], k = idx[, 3],
opacity = 0.15
) %>%
add_markers(
data = X3d_norm,
x = ~T3, y = ~P4, z = ~T6,
color = ~cluster, colors = pal,
symbol = ~cluster, symbols = symb
) %>%
layout(
legend = list(title = list(text = 'Clusters')),
scene = list(
xaxis = list(
title = "T3", nticks = 4, tickvals = c(0.20, 0.40, 0.60, 0.80),
tickfont = list(size = 16), titlefont = list(size = 24)),
yaxis = list(
title = "P4", nticks = 4, tickvals = c(0.20, 0.40, 0.60, 0.80),
tickfont = list(size = 16), titlefont = list(size = 24)),
zaxis = list(
title = "T6", nticks = 4, tickvals = c(0.20, 0.40, 0.60, 0.80),
tickfont = list(size = 16), titlefont = list(size = 24))
)
)
