✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚾️ It's finally Baseball season! Root for the home team... & Register for our Sports Analytics Webinar!

Unable to apply correct colors to 3D scatter and show legend at the same time

Edit: Answer from SO https://stackoverflow.com/a/51503530/10071318

I created a detailed version of this question over at Stackoverflow, so here is the short version:

I’m trying to create a 3D scatter with two regression planes, but no matter what I try I can’t manage to have the scatter in the correct color AND have a legend for it at the same time.
Reproducible code here.

Currently my setting is:
layer1: surface
layer2: surface
layer:3 scatter

plot_ly(x=~x1.seq, y=~x2.seq, z=~z, colorscale = list(c(0,1),c("rgb(253, 231, 37)","rgb(253, 231, 37)")),
         type="surface", opacity=0.35, showlegend=F, showscale = FALSE) %>%
add_trace(inherit=F, data=.df, x=~z, y=~y, z=~x, color=~treat, colors=cols1, mode="markers", type="scatter3d", 
        marker = list(opacity=0.6, symbol=105, size=7)) %>%
add_trace(inherit=F, x=~x1.seq, y=~x2.seq, z=~z2, colorscale = list(c(0,1),c("rgb(40, 125, 142)","rgb(40, 125, 142)")),
      type="surface", opacity=0.35, showlegend=F, showscale = FALSE) %>%
layout(legend = list(x = 0.1, y = 0.9), scene = list(
aspectmode = "manual", aspectratio = list(x=1, y=1, z=1),
xaxis = list(title = "Litter size", range = c(2,7)),
yaxis = list(title = "Day", range = c(0,20)),
zaxis = list(title = "Weight", range = c(0.5,15))))`

If I try to set like this, the colors are completely ignored and set to some default. I managed to get the correct colors by applying the correct value to each point in my data frame, but then I get no legend (see reproducible code).
If I change the order to surface - scatter - surface I get different default colors.

Does anybody has an idea what else I could try?

The problem originally appeared in 4.7.1 but is still present in 4.8.0.
However, now the scatter is completely white.

Edit: answer from SO https://stackoverflow.com/a/51503530/10071318