plotly.R
plotly.R copied to clipboard
Legend is missing for type "mesh3d" unless some surface trace is added
plotly.js added support for more trace legends but, for mesh3d, there is no legend below unless showlegend = TRUE for the mesh and a surface is added.
GH issue for more trace legends merge for above
library(plotly)
library(rmarchingcubes)
R <- 2
f1 <- function(x, y, z){
x^2 + y^2 + z^2 - R^2
}
f2 <- function(x, y, z){
(x-2)^2 + (y-2)^2 + (z-2)^2 - R^2
}
g <- function(x, y, z) {
pmax(f1(x, y, z), f2(x, y, z))
}
n <- 100
x_ <- y_ <- z_ <- seq(-R, R, length.out = n)
Grid <- expand.grid(X = x_, Y = y_, Z = z_)
voxel <- with(Grid, array(g(X, Y, Z), dim = c(n, n, n)))
surf <- contour3d(voxel, level = 0, x_, y_, z_)
vertices <- surf$vertices
indices <- surf$triangles - 1L
plot_ly(
x = vertices[, 1], y = vertices[, 2], z = vertices[, 3],
i = indices[, 1], j = indices[, 2], k = indices[, 3],
type = "mesh3d",
name = "hello world",
showlegend = TRUE
) %>% layout(scene = list(aspectmode = "data"))
z <- c(
c(8.83,8.89,8.81,8.87,8.9,8.87),
c(8.89,8.94,8.85,8.94,8.96,8.92),
c(8.84,8.9,8.82,8.92,8.93,8.91),
c(8.79,8.85,8.79,8.9,8.94,8.92),
c(8.79,8.88,8.81,8.9,8.95,8.92),
c(8.8,8.82,8.78,8.91,8.94,8.92),
c(8.75,8.78,8.77,8.91,8.95,8.92),
c(8.8,8.8,8.77,8.91,8.95,8.94),
c(8.74,8.81,8.76,8.93,8.98,8.99),
c(8.89,8.99,8.92,9.1,9.13,9.11),
c(8.97,8.97,8.91,9.09,9.11,9.11),
c(9.04,9.08,9.05,9.25,9.28,9.27),
c(9,9.01,9,9.2,9.23,9.2),
c(8.99,8.99,8.98,9.18,9.2,9.19),
c(8.93,8.97,8.97,9.18,9.2,9.18)
)
dim(z) <- c(15,6)
plot_ly(
x = vertices[, 1], y = vertices[, 2], z = vertices[, 3],
i = indices[, 1], j = indices[, 2], k = indices[, 3],
type = "mesh3d",
name = "hello",
showscale = FALSE,
showlegend = TRUE
) %>%
plotly::layout(scene = list(aspectmode = "data")) %>%
add_surface(z = ~z, name = "placeholder")