Home > database >  How to color bar chart & rotate axis label in echarts4r
How to color bar chart & rotate axis label in echarts4r

Time:05-27

I have basic df and I'm trying to add different color for each bar. According to e_color doc we need to provide a vector of colors but it doesn't work, I'm getting only one color from the vector. Additionally I'd like to rotate by 45dg x axis label but haven't found any solution yet. Any help with both topics much appreciated.

Thanks
Adam

df <- data.frame(
  x = c('AAA','BBB','CCC','DDD','EEE','FFF','GGG'),
  y = c(10,20,30,40,50,60,70)
  )

my_colors <- brewer.pal(7, "GnBu")

df %>% e_chart(x) %>% e_bar(y) %>% e_color(my_colors) %>% e_x_axis(label = list(rotate = 45))

CodePudding user response:

First of all you need to assign your colors to your data frame named color. Then you can add them by using e_add with "itemStyle". To rotate your labels you should use axisLabel like this:

library(echarts4r)
library(RColorBrewer)
my_colors <- brewer.pal(7, "GnBu")

df$color <- my_colors

df %>% 
  e_chart(x) %>% 
  e_bar(y) %>% 
  e_add("itemStyle", color) %>%
  e_x_axis(axisLabel = list(rotate = 45))

Output:

enter image description here

  • Related