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.
CodePudding user response:
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)