0

I want to have two different y-axis labels for two separate y-axes using echarts4r::e_grid().

Here is the sample data taken from the example here:

df <- data.frame(
  x = 1:20, 
  w = runif(20, 1, 100),
  z = runif(20, 25, 75)
)

Adding name = "w" works:

df |> 
  e_charts(x) |> 
  e_line(w) |> 
  e_line(z, x_index = 1, y_index = 1) |> 
  e_grid(height = "35%") |> 
  e_grid(height = "35%", top = "50%") |> 
  e_y_axis(gridIndex = 1, name = "w") |>
  e_x_axis(gridIndex = 1)

first-name-works

I tried adding another e_y_axis() and specifying gridIndex = 0, but this results in a blank white plot.


df |> 
  e_charts(x) |> 
  e_line(w, x_index = 0, y_index = 0) |> 
  e_line(z, x_index = 1, y_index = 1) |> 
  e_grid(height = "35%") |> 
  e_grid(height = "35%", top = "50%") |> 
  e_y_axis(gridIndex = 1, name = "w") |>
  e_y_axis(gridIndex = 0, name = "z") |>
  e_x_axis(gridIndex = 1)
Giovanni Colitti
  • 1,982
  • 11
  • 24

1 Answers1

1

Sometimes these interactive charting libraries and their documentation drive me crazy. (; Using e_y_axis(index = 1, name = "z") with the index argument seems to work:

set.seed(123)

df <- data.frame(
  x = 1:20, 
  w = runif(20, 1, 100),
  z = runif(20, 25, 75)
)

library(echarts4r)
  
df |> 
  e_charts(x) |> 
  e_line(w) |> 
  e_line(z, x_index = 1, y_index = 1) |> 
  e_grid(height = "35%") |> 
  e_grid(height = "35%", top = "50%") |> 
  e_y_axis(gridIndex = 1, name = "w") |>
  e_y_axis(index = 1, name = "z") |>
  e_x_axis(gridIndex = 1)

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51
  • Thank you! Do you know _why_ this works? I'm struggling to make sense of the [documentation](https://echarts4r.john-coene.com/articles/grid.html). – Giovanni Colitti Oct 05 '22 at 13:53
  • Not really. This was more a trial and error approach after figuring out form the docs that e_axis has an index argument. As I mentioned in my answer I struggle with the docs of `echarts4r` or `echarts` too. Same with `plotly` or `highcharter`. While all offer a lot of options and arguments it's not always clear to me what they are doing and which one to use to achieve a desired result. – stefan Oct 05 '22 at 20:42