Plotly: How to customize colors in a donut chart?

若如初见. 提交于 2021-02-10 08:05:33

问题


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

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:

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


回答1:


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



来源:https://stackoverflow.com/questions/59857236/plotly-how-to-customize-colors-in-a-donut-chart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!