I am trying to use plotly (4.7.1) with R 3.4.1 to plot two 3D surfaces on the same plot, with two different color scales (variations of blue for one and variations of purple for the other one), and color values derived from z values. When I plot surfaces separately, it works pretty fine, but when I plot both surfaces on the same plot by using add_trace or add_surface, the second surface takes the color of the first one.
Here is an example, using example code from https://plot.ly/r/3d-surface-plots/#new-to-plotly
library(plotly)
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)
z1 <- z - 1
z2 <- z + 1
cols1 <- c(rgb(255/255,112/255,183/255,1),rgb(128/255,0/255,64/255,1))
cols2 <- c(rgb(107/255,184/255,214/255,1),rgb(0/255,90/255,124/255,1))
p1 <- plot_ly(showscale = TRUE) %>%
add_surface(z = ~z1, cmin = min(z1), cmax = max(z2), color = ~z1, colors = cols1) %>%
layout(scene = list(zaxis = list(range = c(min(z1),max(z2)))))
p2 <- plot_ly(showscale = TRUE) %>%
add_surface(z = ~z2, cmin = min(z1), cmax = max(z2), color = ~z2, colors = cols2) %>%
layout(scene = list(zaxis = list(range = c(min(z1),max(z2)))))
p3 <- plot_ly(showscale = TRUE) %>%
add_surface(z = ~z1, cmin = min(z1), cmax = max(z2), color = ~z1, colors = cols1) %>%
add_surface(z = ~z2, cmin = min(z1), cmax = max(z2), color = ~z2, colors = cols2) %>%
layout(scene = list(zaxis = list(range = c(min(z1),max(z2)))))
p1
p2
p3
I tried inherit=F
into second add_surface
but it didn’t change anything. I also looked at https://stackoverflow.com/questions/46150158/plotly-different-colours-for-different-surfaces, but the answer does not apply to my case as I don’t want uniformely colored plots but colors depending on z values.
I didn’t find any answers elsewhere, but I am pretty new to plotly so I hope the answer is not obvious.
I somehow managed to get two different scales by using the colorscale
argument in the second add_surface
: the first surface’s colorscale was the default, and the second was different, but not from the colors I defined.
p4 <- plot_ly(showscale = TRUE) %>%
add_surface(z = ~z1, cmin = min(z1), cmax = max(z2), color = ~z1, reversescale=T) %>%
add_surface(z = ~z2, cmin = min(z1), cmax = max(z2), color = ~z2, colorscale = list(c(min(z1),"rgb(107,184,214)"),c(max(z2),"rgb(0,90,124)"))) %>%
layout(scene = list(zaxis = list(range = c(min(z1),max(z2)))))
p4
I guess it is somehow manageable because of this last example, but I guess I am doing something wrong here. Any help would be very much appreciated! I also asked the question on Stack Overflow, and I was suggested to open a ticket on Plotly Github page, but I preferred to ask it here first in case someone has an easy solution. Thanks!