title: Plotly (r): Unable to apply correct colors to 3D scatter and show legend at the same time
body: <p>Answer has been given: <a href="https://stackoverflow.com/a/51503530/10071318">https://stackoverflow.com/a/51503530/10071318</a></p>
  
  <p>I'm trying to create a 3D scatter with plotly including two regression planes. I'll include a proper reproducible example at the end.<br>
  Main plot command:</p>
  
  <pre><code>    p &lt;- 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) %&gt;%
  
    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) %&gt;%
  
    add_trace(inherit=F, data=.df, x=~z, y=~y, z=~x, mode="markers", type="scatter3d", 
  
              marker = list(opacity=0.6, symbol=105, size=7, color=~color_map)) %&gt;%
  
    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))))
  </code></pre>
  
  <p>This produces a plot with the correct colors (defined for each data point in my data frame), but lacks a legend.
  I tried previously to just follow the documentation, which suggests using (with my variables)  <code>color=~treat, colors=c('color1', 'color2', 'color3')</code>.<br>
  This however is completely ignored while plotting and results always in red, blue and green dots. However, this produces a proper legend. I also tried to define my colors as <code>cols1&lt;-c('color1', 'color2', 'color3')</code> and then calling <code>colors=cols1</code>. Same result (red, blue, green scatter).</p>
  
  <p>I'm looking for a way to 1) adjust the colors of the scatter and 2) still have a legend.</p>
  
  <p>Thanks in advance!</p>
  
  <p>Reproducible code: <a href="https://pastebin.com/UJBrrTPs" rel="nofollow noreferrer">https://pastebin.com/UJBrrTPs</a></p>
  
  <p>Edit: After some more testing I found out that changing the order of the trace maintains the correct colors for the surfaces, but the wrong colors for the scatter are changed up.</p>
  
  <pre><code>cols1 &lt;- c("rgb(68, 1, 84)", "rgb(40, 125, 142)", "rgb(253, 231, 37)")
  
  p &lt;-   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) %&gt;%
    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)) %&gt;%
    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) %&gt;%
    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))))
  </code></pre>
  
  <p>This gives the following plot (note the different, but still wrong colors, compared to the one posted in the comment):<br>
  <a href="https://i.stack.imgur.com/Cxn4s.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Cxn4s.png" alt="enter image description here"></a></p>
  
  <p>I was also made aware of this resolved issue: <a href="https://github.com/ropensci/plotly/issues/790" rel="nofollow noreferrer">https://github.com/ropensci/plotly/issues/790</a></p>
  
  <p>I hope this somehow helps to pinpoint the problem.</p>
  
  <p>24.07.2018: plotly got updated to 4.8.0. The problem still exists, however now the scatter appears completely white.</p>