JohnCoene / echarts4r

🐳 ECharts 5 for R
http://echarts4r.john-coene.com/
Other
599 stars 81 forks source link

Different colors for e_bar #66

Closed turgut090 closed 5 years ago

turgut090 commented 5 years ago

How to set different colors for e_bar?

df = tibble(letter=LETTERS[1:5],
            values = c(-1,-5,5,-1,3),
            colors = if_else(values >0, 'green','blue'))

df %>% 
  e_charts(letter) %>% 
  e_bar(values) %>% 
  e_add("itemStyle", colors)

I thought I might be able to reproduce bar_chart like funnel:

funnel <- data.frame(
  stage = c("View", "Click", "Purchase"),
  value = c(80, 30, 20),
  color = c("blue", "red", "green")
)

funnel %>%
  e_charts() %>%
  e_funnel(value, stage) %>%
  e_add("itemStyle", color)
df = tibble(letter=LETTERS[1:5],
            values = c(-1,-5,5,-1,3),
            colors = if_else(values >0, 'green','blue'))

df %>% 
  e_charts() %>% 
  e_bar(letter,values) %>% 
  e_add("itemStyle", colors)
JohnCoene commented 5 years ago

I should document e_add better. It will add the column(s) named passed as children of the param argument.

What you want is:

itemStyle :{
  color:'blue'
}

Hence the column should be color and not colors. Thanks to issue #58 You can now inspect the json constructed by echarts4r with e_inspect; it can help when exploring such arguments.

In brief, this will work.

df = tibble(letter=LETTERS[1:5],
            values = c(-1,-5,5,-1,3),
            color = ifelse(values >0, 'green','blue'))

df %>% 
  e_charts(letter) %>% 
  e_bar(values) %>% 
  e_add("itemStyle", color)
turgut090 commented 5 years ago

Got it, thank you for the explanation

turgut090 commented 5 years ago

But why legend does not work properly

screen shot 2019-02-18 at 12 48 49 am

Is it possible to add there 2 labels: 1 for blue and 1 for green?

zhiiiyang commented 4 years ago

Also, it seems not working for polar coordinates. Please see the example and error messages below.


df = tibble(letter=LETTERS[1:5],
            values = c(-1,-5,5,-1,3),
            colors = if_else(values >0, 'green','blue'))

df %>%
  e_charts(letter) %>%  
  e_polar() %>%
  e_angle_axis() %>%
  e_radius_axis(letter) %>%
  e_bar(values, coord_system = "polar") %>% 
  e_add("itemStyle", color)

Error: Can't subset columns that don't exist. x Column color doesn't exist. Run rlang::last_error() to see where the error occurred.

JohnCoene commented 4 years ago

I'm not sure I see the advantage of specifying every bar colour individually instead of using convenient groups.

library(echarts4r)

df <- tibble::tibble(letter=LETTERS[1:5],
            values = c(-1,-5,5,-1,3),
            color = ifelse(values >0, 'green','blue'))

df %>% 
  group_by(color) %>% 
  e_charts(letter) %>% 
  e_bar(values, stack = T) %>% 
  e_color(c("blue", "red"))

@zhiiiyang I will look into the issue of polar axis tomorrow.

zhiiiyang commented 4 years ago

I am new to echarts4r so not really sure about what convenient groups mean even after a quick search. If it helps, what I originally have in mind is to re-create a ring plot of several bars just like something like Apple Watch fitness rings colored by categories. Thank you for looking into it and please take your time.