情节:如何在甜甜圈图中自定义颜色? [英] Plotly: How to customize colors in a donut chart?

查看:56
本文介绍了情节:如何在甜甜圈图中自定义颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问问您是否可以帮助我自定义由plotly创建的甜甜圈图中的颜色.

I would like to ask you if you could help me in customizing of colors in a donut chart created by plotly.

问题出在后面-我必须重新创建一个仪表板(从Excel文件到html文件).仪表板的一部分是一个图表,向我们提供了有关每个实体的早期生产的信息.该图表按情节是甜甜圈图类型.由于每个实体都是由整个仪表板中的特定颜色(以RGB定义)定义的,因此我也需要将这些颜色保留在甜甜圈图中.但有一个问题.我总是收到以下警告:

The problem is following - I have to recreate a dashboard (from an excel file to a html file). 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. 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. But there is a problem. I always get the following warning:

警告信息:在RColorBrewer :: brewer.pal(N,"Set2")中:n太大,允许调色板Set2的最大值为8用多种颜色返回您要的调色板

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

,得到的甜甜圈图仅包含一个颜色未指定的实体(请参见代码下方的图片).另外,图例中的颜色也不是所定义的颜色.

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)))

推荐答案

事实证明您需要在plot_ly()中放入 type ='pie' color =〜Entity,并指定 marker = list(colors =〜EntityColor).

It turns out you needed to have type='pie' in plot_ly(), comment out color = ~Entity, and specify 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)))

我希望这是您想要的.不要犹豫,让我知道.

I hope this is what you were looking for. Don't hesitate to let me know if not.

这篇关于情节:如何在甜甜圈图中自定义颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆