Plotly:如何自定义堆叠条形图中的颜色?

Plotly: How to customize colors in a stacked bar chart?

我想问你是否可以帮助我在 plotly 创建的 堆叠条形图 中自定义颜色。

问题如下 - 我必须重新创建仪表板(从 excel 文件到 html 文件)。仪表板的一部分是一个图表,为我们提供有关每个实体的早期生产的信息。该图表是 plotly 的堆叠条形图类型。由于整个仪表板中每个实体都由特定颜色(以 RGB 定义)定义,因此我也需要将这些颜色保留在圆环图中。但有一个问题。我总是收到以下警告:

Warning message: In RColorBrewer::brewer.pal(N, "Set2") : n too large, allowed maximum for palette Set2 is 8 Returning the palette you asked for with that many colors

并且生成的圆环图仅包含一个未指定颜色的实体。此外,图例中的颜色不是定义的颜色。

知道如何处理它吗?非常感谢您。

代码:

library(dplyr)
library(plotly)

dt <- as.data.frame(matrix(ncol = 13, nrow = 19))
colnames(dt) <- c("Entity", month.abb)

for (i in 1:nrow(dt)) {
  dt[i, 1] <- paste("Entity", i, sep="")
  dt[i, -1] <- floor(runif(12, min=0, max=100))
}

# assign colors to entities

dt$"EntityColor" <- c("#074263", "#0B5394", "#3D85C6", "#6D9EEB", "#A4C2F4", "#CFE2F3", "#5B0F00", "#85200C", "#A61C00", "#CC4125", "#DD7E6B", "#E6B8AF", "#F8CBAD", "#F4CCCC", "#274E13", "#38761D", "#E06666", "#CC0000", "#20124D")

data.table::melt(dt) %>%

  plot_ly(x = ~variable,
          y = ~value,
          type = "bar",
          color = ~Entity,
          marker = list(colors = ~EntityColor)
  ) %>%

  layout(yaxis = list(title = ""),
         xaxis = list(title = ""),
         barmode = 'stack')

剧情:

评论后的改进方法:

因为颜色有点棘手(见下面的初步建议)我不得不把整个事情分解并在循环中使用 plot_ly()add_traces() 的组合来制作确保 plotly 设置没有以错误的顺序应用颜色。 下面的情节应该正是你要找的。

剧情:

请注意,我附加了一个连续的数字列 ID。为什么?因为您希望名称按字母顺序排列,并且行会按照它们在您的源代码中出现的顺序添加到图中。这有点棘手,因为使用 dt %>% arrange((Entity)) 直接订购会给你 Entity1, Enitity10, Entity11 等。如果你想以任何其他方式调整它,请告诉我。

代码:

library(dplyr)
library(plotly)

# data
set.seed(123)

dt <- as.data.frame(matrix(ncol = 13, nrow = 19))
colnames(dt) <- c("Entity", month.abb)
for (i in 1:nrow(dt)) {
  dt[i, 1] <- paste("Entity", i, sep="")
  dt[i, -1] <- floor(runif(12, min=0, max=100))
}

# assign colors to entities
dt$"EntityColor" <- c("#074263", "#0B5394", "#3D85C6", "#6D9EEB", "#A4C2F4", "#CFE2F3", "#5B0F00", "#85200C", "#A61C00", "#CC4125", "#DD7E6B", "#E6B8AF", "#F8CBAD", "#F4CCCC", "#274E13", "#38761D", "#E06666", "#CC0000", "#20124D")



# sort data
dt$ID <- seq.int(nrow(dt))
dt <- dt %>% arrange(desc(ID))


# specify month as factor variable to ensure correct order
months=names(dt)[2:13]
months<- factor(months, levels = c(months))

# plotly setup
p <- plot_ly(type = 'bar')

# add trace for each entity
nrows = nrow(dt)
for(i in 1:nrows) {
    p <- p %>% add_trace(x=months, y = unlist(dt[i,2:13], use.names=F), type = 'bar',
                         #name = paste(dt[i,1], dt[i,14], sep = "_"),
                         name = dt[i,1],
                         type = 'bar',  
                         marker=list(color = dt[i,14])) %>%
       layout(barmode = 'stack')

}

# Edit layout
p <- p %>% layout(title = list(xanchor='right', text='Correct colors, orderered legend'),
                  yaxis = list(title = ''),
                  xaxis = list(title = 'month'))
p

颜色正确性验证:

初步建议

这是初步建议。首先,color = ~Entity 必须离开。 marker = list(color = ~EntityColor)marker = list(colors = ~EntityColor) 给出了两个不同的结果。 even 更奇怪的是 pie chart documentation 使用:

marker = list(colors = colors, ...)

... 而 bar chart documentation 使用:

marker = list(color = c('rgba(204,204,204,1)', 'rgba(222,45,38,0.8)', ...)

...在 color.

末尾没有 s

无论哪种方式,您都应该测试 marker = list(color = ~EntityColor)marker = list(colors = ~EntityColor),看看哪个适合您。

剧情:

代码:

dt <- as.data.frame(matrix(ncol = 13, nrow = 19))
colnames(dt) <- c("Entity", month.abb)

for (i in 1:nrow(dt)) {
  dt[i, 1] <- paste("Entity", i, sep="")
  dt[i, -1] <- floor(runif(12, min=0, max=100))
}

# assign colors to entities

dt$"EntityColor" <- c("#074263", "#0B5394", "#3D85C6", "#6D9EEB", "#A4C2F4", "#CFE2F3", "#5B0F00", "#85200C", "#A61C00", "#CC4125", "#DD7E6B", "#E6B8AF", "#F8CBAD", "#F4CCCC", "#274E13", "#38761D", "#E06666", "#CC0000", "#20124D")

data.table::melt(dt) %>%

  plot_ly(x = ~variable,
          y = ~value,
          name= ~Entity,
          type = "bar",
          #color = ~Entity,
          marker = list(colors = ~EntityColor)
  ) %>%

  layout(yaxis = list(title = ""),
         xaxis = list(title = ""),
         barmode = 'stack')

看看它对你的效果如何。