简体   繁体   中英

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

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:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM