Plotly (r):无法将正确的颜色应用于 3D 散点并同时显示图例

Plotly (r): Unable to apply correct colors to 3D scatter and show legend at the same time

答案已给出:

我正在尝试创建一个包含两个回归平面的 3D 散点图。我将在最后包含一个适当的可重现示例。
主剧情命令:

    p <- 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, 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) %>%

  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)) %>%

  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))))

这会生成一个颜色正确的图(为我的数据框中的每个数据点定义),但缺少图例。 我之前尝试只遵循文档,它建议使用(与我的变量)color=~treat, colors=c('color1', 'color2', 'color3').
然而,这在绘图时被完全忽略,结果总是红色、蓝色和绿色的点。但是,这会产生一个适当的图例。我还尝试将我的颜色定义为 cols1<-c('color1', 'color2', 'color3'),然后调用 colors=cols1。相同的结果(红色、蓝色、绿色散点)。

我正在寻找一种方法来 1) 调整散点的颜色和 2) 仍然有图例。

提前致谢!

可重现代码:https://pastebin.com/UJBrrTPs

编辑:经过更多测试后,我发现改变轨迹的顺序可以保持表面的正确颜色,但错误的散点颜色会改变。

cols1 <- c("rgb(68, 1, 84)", "rgb(40, 125, 142)", "rgb(253, 231, 37)")

p <-   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))))

这给出了以下情节(注意与评论中发布的颜色不同但仍然错误的颜色):

我也了解到这个已解决的问题:https://github.com/ropensci/plotly/issues/790

我希望这能以某种方式帮助查明问题。

24.07.2018:plotly 已更新至 4.8.0。问题依然存在,但是现在散点全白了。

这是您想要的吗?

你非常接近,我只是做了一些小的调整让它工作,我想你可能会想要。

## Map each value of `treat` to the desired color
Custom_Color_Mappings <- c("AP" = "rgb(40, 125, 142)",
                           "C" =  "rgb(253, 231, 37)",
                           "PO"  = " rgb(68, 1, 84)")

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, 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) %>%
  add_trace(inherit=F, data=.df, x=~z, y=~y, z=~x,
            mode="markers",
            type="scatter3d",
            color=~treat, ## Base color off of `treat`
            colors = Custom_Color_Mappings, ## Map colors as defined above
            marker = list(opacity=0.6, ## Note that the `color` and `colors` arguments are outside of the `marker` definition
                          symbol=105,
                          size=7)) %>%
  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))))

结果如下所示:

我想这就是您要找的东西?

library(plotly)

p <- plot_ly() %>%
  add_surface(
    x=~x1.seq, y=~x2.seq, z=~z, 
    colorscale = list(c(0,1),c("rgb(253, 231, 37)","rgb(253, 231, 37)")),
    opacity=0.35, showlegend=FALSE, showscale = FALSE
  ) %>%
  add_surface(
    x=~x1.seq, y=~x2.seq, z=~z2, 
    colorscale = list(c(0,1),c("rgb(40, 125, 142)","rgb(40, 125, 142)")),
    opacity=0.35, showlegend=FALSE, 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)))
  )


for (i in unique(.df$treat)) {
  d <- .df[.df$treat %in% i, ]
  p <- add_markers(
    p, data=d, x=~z, y=~y, z=~x, text=~treat, name=~treat,
    marker = list(opacity=0.6, symbol=105, size=7, color=~color_map)
  )
}

p

https://i.stack.imgur.com/pTQj4.png