【问题标题】:How do I map categorical variables to color the outline of points in a 3D scatter plot in R Plotly?如何映射分类变量以在 R Plotly 中为 3D 散点图中的点轮廓着色?
【发布时间】:2020-06-25 02:08:30
【问题描述】:

此代码制作了 Fisher iris 数据集的简单 3d 散点图,并添加了一个额外的分类变量:


library(plotly)
roots <- factor(round(runif(n = dim(iris)[2],min = -.499,max = 2.499)))
my_iris <- cbind(data.frame(roots), iris)


plot_ly() %>%
  add_trace(data = my_iris, type = 'scatter3d', mode = "markers",
            x = ~Sepal.Length,
            y = ~Petal.Length,
            z = ~Sepal.Width, 
            color = ~Species,
            colors = c("red","blue","green")

  )


通过查看此帮助页面:https://plot.ly/r/marker-style/

我发现您可以像这样为这些点添加轮廓:

plot_ly() %>%
  add_trace(data = my_iris, type = 'scatter3d', mode = "markers",
            x = ~Sepal.Length,
            y = ~Petal.Length,
            z = ~Sepal.Width, 
            color = ~Species,
            colors = c("#00FA9A34","#B22222dd","#00BFFFee"),
            marker = list(

              line = list(
                color = "#aabbffdd",
                width = 2
              )

            )
  )

查看此站点https://plot.ly/r/reference/#scatter3d 使我们认为线条是 scatter3d 标记的属性,而这些标记又具有颜色和宽度属性。

现在我尝试根据我的新 roots 变量将颜色映射到轮廓,

plot_ly() %>%
  add_trace(data = my_iris, type = 'scatter3d', mode = "markers",
            x = ~Sepal.Length,
            y = ~Petal.Length,
            z = ~Sepal.Width,
            color = ~Species,
            colors = c("#00FA9A34","#B22222dd","#00BFFF66"),
            marker = list(

              line = list(
                color = ~roots,
                colors = c("#000000ff","#f00f3355","#dd22ccbb"),
                width = 2
              )

            )
  )

而且它不太有效:我使用的第一个 hex+alpha 值应该映射到完全不透明的黑色,但这不是我得到的颜色之一,我希望看到描述输出的图例条目。

所以我的问题是:有没有办法进行这种美学映射?也许我应该使用add_markers而不是使用add_trace?有没有办法在 Plotly R 的二维散点图中做到这一点?还希望获得有关如何正确学习 R 的 Plotly 的提示,因为我在上面链接到的文档页面有点不透明,而且似乎比 ggplot2 学习的好资源更少。

【问题讨论】:

    标签: r plot plotly data-visualization r-plotly


    【解决方案1】:

    在我看来,plotly 不允许指定标记轮廓的颜色。

    对于标签,您可以更改悬停文本。 或者使用注解:

    也许这并不能解决您的问题,但我希望这对您有所帮助。

    # Yours data
    
    library(dplyr)
    library(plotly)
    roots <- as.character(round(runif(n = dim(iris)[2],min = -.499,max = 2.499)))
    my_iris <- cbind(data.frame(roots), iris)
    
    
    # Changing plot
    my_iris %>%
    plot_ly(type = 'scatter3d', mode = "markers") %>%
      add_trace(x = ~Sepal.Length,
                y = ~Petal.Length,
                z = ~Sepal.Width,
                color = ~Species,
                colors = c("#00FA9A34","#B22222dd","#00BFFF66"),
                marker = list(
                  line = list(
                    color = ~roots,
                    colors = c("#000000","#E35F13","#E3138B"),
                    width = 6
                  )
                ),
                hoverinfo = 'text',
                text = ~paste('</br> Species: ', Species,
                              '</br> Petal Length: ', Petal.Length,
                              '</br> Petal Width: ', Petal.Width,
                              '</br> Roots: ',roots)
                )
    

    输出:

    【讨论】:

      【解决方案2】:

      我找到了一个我仍然认为不合适的解决方案:将您用来为标记轮廓着色的变量设置为您自己想要使用的颜色:

      library(plotly)
      roots <- round(runif(n = dim(iris)[1],min = -.499,max = 2.499))
      roots_colors <- vector(mode="character", length=length(roots))
      #setting  the value of roots colors to be the colors we want
      roots_colors[roots == 0] <- "#000000ff"
      roots_colors[roots == 1] <-  "#ff0000ff"
      roots_colors[roots == 2] <-  "#0000ffff"
      
      my_iris <- cbind(data.frame(roots_colors), iris)
      
      plot_ly() %>%
        add_trace(data = my_iris, type = 'scatter3d', mode = "markers",
                  x = ~Sepal.Length,
                  y = ~Petal.Length,
                  z = ~Sepal.Width,
                  color = ~Species,
                  colors = c("#00FA9A34","#B22222dd","#00BFFF66"),
                  marker = list(
      
                    line = list(
                      color = ~roots_colors,
                      width = 2
                    )
      
                  )
        )
      

      我们现在有了我们想要的适当颜色,但没有描述它们的图例。现在我想知道如何使图例反映这些颜色。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-07-08
        • 1970-01-01
        • 2012-05-11
        • 1970-01-01
        • 1970-01-01
        • 2018-06-24
        • 2016-11-20
        • 1970-01-01
        相关资源
        最近更新 更多