Plotly (R) - Pie chart: How to fixate the color assignment color per group?

Stan W

We are building a Shiny app in which we use a dynamic plotly pie chart. Users can select variables to include in the plot.

Adding custom color codes allow me to add our custom colors, but they won't stick to the grouping variable as they do in plotly bar charts (using the colors command).

Example:

library(plotly)

dat <- data.frame(dis = c(1,2,3), value = c(20,30,24))

plot_ly(dat, labels = ~dis, values = ~value, sort = F, 
        marker = list(colors = c("1" = "#B76C9E", 
                                 "2" = "#4285F4",
                                 "3" = "#EA4335"))) %>%
  add_pie(hole = 0.3) %>%
  layout(legend = list(orientation = 'h'), margin = list(l = 0 , r = 0, t = 0, b = 100, pad = 1),
         yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))

enter image description here

But when I remove dis == 1 from the data.frame (like my users will do in the Shiny app) the colors shift and won't remain their assigned color:

dat2 <- data.frame(dis = c(2,3), value = c(30,24))

plot_ly(dat2, labels = ~dis, values = ~value, sort = F, 
        marker = list(colors = c("1" = "#B76C9E", 
                                 "2" = "#4285F4",
                                 "3" = "#EA4335"))) %>%
  add_pie(hole = 0.3) %>%
  layout(legend = list(orientation = 'h'), margin = list(l = 0 , r = 0, t = 0, b = 100, pad = 1),
         yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))

enter image description here

Is there a way to lock the color to the categorical variable so "2" will always get the same color?

StupidWolf

I guess you got to pass update the colors inside the call, something like this.. but I think it's pretty rough:

pal = c("#B76C9E", "#4285F4","#EA4335")
names(pal) = as.character(1:3)

updated_pal = pal[unique(as.character(dat2$dis))]

plot_ly(dat2, 
labels = ~dis, values = ~value, sort = F,
marker=list(colors=updated_pal)) %>%
  add_pie(hole = 0.3) %>%
  layout(legend = list(orientation = 'h'), margin = list(l = 0 , r = 0, t = 0, b = 100, pad = 1),
         yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to hard code the color for the displayed variable in plotly pie chart?

Plotly - Create Pie Chart with continuous color scale

plotly - how to make a bar chart and a pie chart share the same color code

R- Shiny - how to change color of legends in pie chart?

How to define separate color palettes per trace in R plotly plots?

How plot a pie chart colored with one scaled color and using plotly package

R Plotly: how to order pie chart?

How to horizontally center a pie chart with plotly in R?

How to set the chart pie color in correct orders?

How to set specific color to the pie chart?

How to create a single color pie chart in Tableau

Color in plotly bar chart

How to color bars in grouped plotly bar chart

How set color family to pie chart in chart.js

Julia: How to change color of a group in plotly scatterplot

Change the background color in pie chart

Pie chart color text matplotlib

Replace pie chart color with gradient

R Plotly: How to set the color of Individual Bars of a Waterfall Chart in R Plot.ly?

Color in Multiple Subplots Plotly Chart

R plotly pie chart - how to display all of a long legend?

How to use field value as a color value in vega lite pie chart?

How could I get a different pie chart color

How to customize color in pie chart of NVD3

Python - How to change autopct text color to be white in a pie chart?

How to color a pie chart, based on levels of a nominal feature, in a shiny app?

How do I change Legend color in a Recharts pie chart?

How To change Color in pie chart with the using of pykcharts library?

How to set specific color to each slice of Pie Chart?