Jorge Hernández
Jorge Hernández

Reputation: 662

How to use "itemStyle" for multiple series on echarts4r?

Hello and thanks for reading me. Im trying to create a plot with conditional colors and I want to have the same colors for the scatter serie, but im just getting the custom color for the first serie. How I can apply the custom color for both series?

The code is the following:

library(echarts4r)
library(dplyr)

tibble(
  year = as.character(sample(1950:2020, 100, replace = T)),
  y = rnorm(100, 10, 3)
) |> 
  head(10) |> 
  mutate(
    color = if_else(y > 9, "red", "green")
  ) |> 
  e_charts(year) |> 
  e_bar(y ) |> 
  e_scatter(y) |> 
  e_add_nested("itemStyle", color) 

Thanks for the help

Upvotes: 1

Views: 262

Answers (1)

Thor6
Thor6

Reputation: 811

Instead of itemStyle you can use the e_visual_map as following :

tibble(
  year = as.character(sample(1950:2020, 100, replace = T)),
  y = rnorm(100, 10, 3)
) |> 
head(10) |> 
mutate(
  color = if_else(y > 9, "red", "green")
) |> 
e_charts(year) |> 
e_bar(y) |> 
e_scatter(y) |>
e_visual_map(
  type = "piecewise",
  pieces = list(
    list(
     gt = 9,
     color = "red"
    ),
    list (
     lte = 9,
     color = "green"
  )
 )
)

Upvotes: 1

Related Questions