【问题标题】:Plotly (r): Unable to apply correct colors to 3D scatter and show legend at the same timePlotly (r):无法将正确的颜色应用于 3D 散点图并同时显示图例
【发布时间】:2018-12-20 21:49:48
【问题描述】:

已给出答案:https://stackoverflow.com/a/51503530/10071318

我正在尝试创建一个包含两个回归平面的 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&lt;-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:情节更新到 4.8.0。问题仍然存在,但是现在散点图看起来完全是白色的。

【问题讨论】:

    标签: r r-plotly


    【解决方案1】:

    这就是你所追求的吗?

    你非常接近,我只是做了一些小的调整,让它按照我认为你可能想要的方式工作。

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

    结果如下所示:

    【讨论】:

    • 我尝试了类似的方法。但正如您在绘图中看到的那样,散点图的颜色是错误的,与我之前遇到的蓝色、红色和绿色相同。三种定义的颜色中的两种与回归平面相同。颜色应该像这样imgur.com/a/8wjcPBL
    【解决方案2】:

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

    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

    【讨论】:

    • 完美!这解决了这个问题。知道为什么将颜色定义为 colors=c('color1', 'color2', 'color3') 不起作用?
    • colors 用于定义“全局”色阶的范围。可能是全局色阶覆盖了表面的色阶
    猜你喜欢
    • 1970-01-01
    • 2021-04-17
    • 1970-01-01
    • 1970-01-01
    • 2020-04-02
    • 1970-01-01
    • 1970-01-01
    • 2018-06-24
    • 1970-01-01
    相关资源
    最近更新 更多