[英]Plotly: How to customize colors in a stacked bar chart?
我想问你是否可以帮助我在 plotly 创建的堆叠条形图中自定义颜色。
问题如下 - 我必须重新创建一个仪表板(从 excel 文件到 html 文件)。 仪表板的一部分是一个图表,为我们提供有关每个实体的早期生产的信息。 该图表是 plotly 的堆叠条形图类型。 由于整个仪表板中每个实体都由特定颜色(以 RGB 定义)定义,因此我也需要将这些颜色保留在圆环图中。 但有一个问题。 我总是收到以下警告:
警告消息:在 RColorBrewer::brewer.pal(N, "Set2") 中:n 太大,调色板 Set2 允许的最大值为 8 返回您要求的具有这么多颜色的调色板
生成的圆环图仅包含一个未指定颜色的实体。 此外,图例中的颜色不是定义的颜色。
知道如何处理它吗? 非常感谢你提前。
代码:
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)
给出了两个不同的结果。 更奇怪的是饼图文档使用:
marker = list(colors = colors, ...)
...并且条形图文档使用:
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')
看看它是如何为你工作的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.