Plotly:如何自定义圆环图中的颜色?
Plotly: How to customize colors in a donut 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
并且生成的圆环图仅包含一个未指定颜色的实体(请参见代码下方的附图)。此外,图例中的颜色不是定义的颜色。
知道如何处理它吗?非常感谢您。
# 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)))
原来你需要 type='pie'
in plot_ly()
,注释掉 color = ~Entity,
并指定 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")
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)))
我希望这就是您要找的。如果没有,请随时告诉我。
我想问你是否可以帮助我在 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
并且生成的圆环图仅包含一个未指定颜色的实体(请参见代码下方的附图)。此外,图例中的颜色不是定义的颜色。
知道如何处理它吗?非常感谢您。
# 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)))
原来你需要 type='pie'
in plot_ly()
,注释掉 color = ~Entity,
并指定 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")
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)))
我希望这就是您要找的。如果没有,请随时告诉我。