Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to set fixed colors for each value in echarts4r?

I want to plot a pie chart and give each value a fixed color. Give the e_color() function a named list does unfourtunately not work. Any suggestions?

library(tibble)
library(echarts4r)

tibble(class=c("A", "B"), n=c(34,12)) %>%
e_charts(name) %>%
e_pie(n) %>%
e_color(color = c("A" = "red", "B" = "yellow"))

Note: I use the pie chart in a shiny app where the values for class can take on different values depending on user input. Sometimes only class A occurs, sometimes A,B,C and so on, whereby the colors of the different values of class should always remain the same.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This should work

tibble(class=c("A", "B"), n=c(34,12)) %>%
e_charts(class) %>%
e_pie(n) %>%
e_color(color = c("red", "yellow"))

Edit: you can create a data frame with a colour for each class and depending on the value for class you will get a fixed colour

df_colours <- data.frame(class = LETTERS[1:4],
                         colours = c("red", "yellow", "blue", "green"))

df <- tibble(class=c("A", "B"), n=c(34,12))

colour <- df_colours %>%
  filter(class %in% df$class) %>%
  select(colours) %>%
  unlist() %>% 
  unname()


df %>%
  e_charts(class) %>%
  e_pie(n) %>%
  e_color(color = colour)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading