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 color bar chart & rotate axis label in echarts4r

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

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 :

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

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