【问题标题】:custom colors in R PlotlyR Plotly中的自定义颜色
【发布时间】:2016-06-06 16:51:14
【问题描述】:

我目前是 Plotly 的初学者,有一个我似乎无法解决的问题。我有我的堆积条形图,但我不知道如何为每个单独的类别着色。我目前正在使用 R。

这是我当前的堆积条形图:

我当前的代码是:

p = plot_ly(x, x = day, y = count, type = "bar", group = status) %>% layout(barmode = "stack", showlegend = T)

我已尝试使用 "color = " 参数和标记,但没有正确着色我的图表。

【问题讨论】:

  • "我尝试使用 "color =" 参数和标记,但没有正确地为我的图表着色"...你尝试了什么,你的意思是什么"没有正确地为我的图表着色”。而且我们无法测试您的代码,我们无权访问您的数据集

标签: r plotly r-plotly


【解决方案1】:

这里有一些可能会有所帮助的东西。请注意,由于某种原因,方法 1 会导致图例条目为黑色。所以我也建议一种解决方法。

library(plotly)
library(dplyr)
library(data.table)

mtcars$color <- factor(mtcars$gear, labels = c("blue", "red", "green"))

# Method 1
# Legend entries are all black
plot_ly(mtcars, x = as.factor(cyl), y = mpg, group = gear, type = "bar", marker = list(color = color), name = "test") %>% 
  layout(barmode = "stack", showlegend = T)

# Method 2
# Workaround
dt <- data.table(mtcars)

p <- dt[gear == 3,] %>% 
  plot_ly(x = factor(cyl), y = mpg, name = "Gear = 3", type = "bar", marker = list(color = "blue"))

p <- dt[gear == 4,] %>% 
  add_trace(x = factor(cyl), y = mpg, name = "Gear = 4", type = "bar", marker = list(color = "red"))

p <- dt[gear == 5,] %>% 
  add_trace(x = factor(cyl), y = mpg, name = "Gear = 5", type = "bar", marker = list(color = "green"))

p <- layout(p, barmode = "stack")

p

【讨论】:

    【解决方案2】:

    您需要为color 参数指定一个因子,然后为colors 参数指定一个颜色向量。

    这是一个简单的解决方案。绘图前请注意数据框所需的排序。

    require(dplyr)
    require(plotly)
    
    set.seed(42)
    
    df <- data.frame(x = rep(LETTERS[1:5], 3), 
                     y = rexp(15, rate = 0.5),
                     z = c(rep("Adam", 5), rep("Arthur", 5), rep("Ford", 5)))
    df <- arrange(df, desc(z))
    
    plot_ly(df, 
            x = x, 
            y = y, 
            color = z, 
            colors = c("grey50", "blue", "red"), 
            type = "bar") %>% 
        layout(barmode = "stack")
    

    数据框的顺序很奇怪。我原以为plot_ly 会使用关卡的顺序,但事实并非如此。

    编辑: 此示例使用plotly 3.x.x。如果您使用plotly 4.x.x 或更高版本,此代码可能无法按原样运行。更多详情请看这里:https://www.r-bloggers.com/upgrading-to-plotly-4-0-and-above/

    【讨论】:

      【解决方案3】:

      我今天遇到了这个问题,因为我试图为每个单独的标记定义颜色并苦苦挣扎。答案来自其他答案之一,但该答案表示它不适用于这个问题,但它适用于我的问题。

      这里是 MRE 的要点:https://gist.github.com/bhive01/d4821f6d73ea0f428767d9bb68fdd404

      本质上,不是用color 和/或colors 定义颜色,而是使用marker = list(color = ~color)。这假设您有像我这样的数据,其中每个点都有颜色。这对我来说很关键,因为当我使用 color = ~Hex, colors = ~Hex 时,使用了颜色,但是数据被排序并且颜色与其正确的标记不匹配。

      【讨论】:

      • 不确定答案是否清楚,但您需要在数据框中有一列存储实际颜色代码(或类似代码)。然后你使用marker = list(color = ~column_with_color),你完全放弃了colorcolors plotly选项。
      • 感谢 bhive01,这大部分解决了我的问题。我现在唯一的问题是没有显示图例。有人知道添加自定义颜色图例的方法吗?
      【解决方案4】:

      以下是对 plot_ly 语句的一些修改(~和 ~factor()),以使输出与较新版本的 plotly 一起使用。

      require(dplyr)
      require(plotly)
      
      set.seed(42)
      
      df <- data.frame(x = rep(LETTERS[1:5], 3), 
                       y = rexp(15, rate = 0.5),
                       z = c(rep("Adam", 5), rep("Arthur", 5), rep("Ford", 5)))
      df <- arrange(df, desc(z))
      
      plot_ly(df, 
              x = ~x, 
              y = ~y, 
              color = ~factor(z), 
              colors = c("grey50", "blue", "red"), 
              type = "bar") %>% 
        layout(barmode = "stack")
      

      干杯

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-10-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-06
        • 1970-01-01
        • 2014-11-26
        相关资源
        最近更新 更多