[英]Plotly: How to customize colors in a donut chart?
I would like to ask you if you could help me in customizing of colors in a donut chart created by plotly.我想问你是否可以帮助我在由 plotly 创建的圆环图中自定义颜色。
The problem is following - I have to recreate a dashboard (from an excel file to a html file).问题如下 - 我必须重新创建仪表板(从 excel 文件到 html 文件)。 A part of the dashboard is a chart providing us with information about early production of each entity.仪表板的一部分是一个图表,为我们提供有关每个实体早期生产的信息。 The chart is a donut chart type by plotly.该图表是由 plotly 绘制的圆环图类型。 As each entity is defined by a specific color (defined in RGB) throughout whole dashboard, I need to keep these colors in the donut chart as well.由于每个实体都由整个仪表板中的特定颜色(以 RGB 定义)定义,因此我还需要将这些颜色保留在圆环图中。 But there is a problem.但有一个问题。 I always get the following warning:我总是收到以下警告:
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警告消息:在 RColorBrewer::brewer.pal(N, "Set2") 中:n 太大,调色板 Set2 允许的最大值为 8
and the resulting donut chart containts only one Entity with a not-specified color (see attached picture below the code).并且生成的圆环图仅包含一个具有未指定颜色的实体(请参见代码下方的附图)。 Also, the colors in legend are not those which are defined.此外,图例中的颜色不是定义的颜色。
Any idea what to do with it?知道该怎么做吗? Thank you so much in advance.非常感谢你。
# create dataset
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")
dt
Entity Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec EntityColor
1 Entity1 60 98 88 66 5 4 10 28 96 12 49 36 #074263
2 Entity2 14 0 54 67 55 95 11 67 82 87 54 83 #0B5394
3 Entity3 71 88 61 57 34 84 75 55 67 99 37 95 #3D85C6
4 Entity4 20 29 14 12 31 33 42 88 47 42 73 74 #6D9EEB
5 Entity5 70 77 60 85 59 69 28 14 53 91 2 86 #A4C2F4
6 Entity6 50 12 72 18 38 2 23 98 61 39 70 36 #CFE2F3
7 Entity7 1 69 86 16 73 61 72 43 85 35 87 86 #5B0F00
8 Entity8 64 58 73 80 38 60 18 66 25 29 89 96 #85200C
9 Entity9 36 49 20 15 54 89 62 94 68 38 60 4 #A61C00
10 Entity10 98 11 61 42 58 87 9 20 75 53 13 65 #CC4125
11 Entity11 78 66 34 30 92 2 59 63 9 74 46 29 #DD7E6B
12 Entity12 21 82 14 80 51 66 5 54 4 38 0 20 #E6B8AF
13 Entity13 22 75 68 91 0 77 99 69 46 20 63 63 #F8CBAD
14 Entity14 7 75 31 15 86 65 64 6 20 75 21 45 #F4CCCC
15 Entity15 65 67 42 55 89 11 20 47 2 26 28 62 #274E13
16 Entity16 79 29 68 30 72 98 54 88 47 80 14 67 #38761D
17 Entity17 41 68 7 59 62 70 36 44 44 94 2 63 #E06666
18 Entity18 5 1 25 99 27 49 16 98 40 18 59 24 #CC0000
19 Entity19 11 20 31 62 93 32 67 81 54 12 6 10 #20124D
# create donut chart
dt %>%
mutate(Sum = rowSums(dt[, -c(1,ncol(dt))])) %>%
plot_ly(labels = ~Entity,
values = ~Sum,
textposition = "inside",
textinfo = 'label+percent',
color = ~Entity,
marker = list(color = ~EntityColor)) %>%
add_pie(hole = 0.4) %>%
layout(showlegend = T,
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = F),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = F),
annotations = list(text=sum(rowSums(dt[, -c(1,ncol(dt))])), "showarrow"=F, font=list(size = 40)))
It turns out you needed to have type='pie'
in plot_ly()
, comment out color = ~Entity,
and specify marker = list(colors = ~EntityColor)
.事实证明,您需要in plot_ly()
有type='pie'
,注释掉color = ~Entity,
并指定marker = list(colors = ~EntityColor)
。
Plot:阴谋:
Code:代码:
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")
dt %>%
mutate(Sum = rowSums(dt[, -c(1,ncol(dt))])) %>%
plot_ly(labels = ~Entity,
values = ~Sum,
textposition = "inside",
textinfo = 'label+percent',
type='pie',
hole=0.4,
#color = ~Entity,
marker = list(colors = ~EntityColor)
) %>% add_pie(hole = 0.4) %>%
layout(showlegend = T,
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = F),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = F),
annotations = list(text=sum(rowSums(dt[, -c(1,ncol(dt))])), "showarrow"=F, font=list(size = 40)))
I hope this is what you were looking for.我希望这就是你要找的。 Don't hesitate to let me know if not.如果没有,请不要犹豫,让我知道。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.