Search code examples
recharts4r

Use two different y-axis names with e_grid()


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)

Solution

  • 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