JohnCoene / echarts4r

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

Bar plot with two X-axes - add an X-axis for an additional grouping variable. #498

Open yogat3ch opened 1 year ago

yogat3ch commented 1 year ago

Hi @JohnCoene &/| @helgasoft, I'm wandering in the dark on this one. I have some data represented below:

d <- tibble::tribble(
  ~scenario_id, ~reservoir_name, ~total_traces, ~scenario_name, ~total_years, ~type_vulnerable, ~vulnerable,
             1,     "Lake Mead",            32,  "Scenario  1",           32,         "Traces",       56.25,
             1,     "Lake Mead",            32,  "Scenario  1",           32,          "Years",        87.5,
             1,   "Lake Powell",            32,  "Scenario  1",           32,         "Traces",      96.875,
             1,   "Lake Powell",            32,  "Scenario  1",           32,          "Years",      96.875,
             2,     "Lake Mead",           114,  "Scenario  2",           39,         "Traces",    18.42105,
             2,     "Lake Mead",           114,  "Scenario  2",           39,          "Years",    66.66667,
             2,   "Lake Powell",           114,  "Scenario  2",           39,         "Traces",    28.07018,
             2,   "Lake Powell",           114,  "Scenario  2",           39,          "Years",    87.17949
  )

and some simple plotting code:

 dplyr::group_by(d, scenario_name, type_vulnerable) |>
    echarts4r::e_charts(reservoir_name) |>
    echarts4r::e_bar(vulnerable) |>
    e_default_opts(
      tooltip = list(
        trigger = 'axis'
        # formatter = UU::glue_js(
        #   "function(params) {
        #     debugger;
        #   }"
        # )
      ),
      datazoom = FALSE,
      x_axis_formatting = FALSE,
      y_axis_formatting = y_formatting
    )

The first X-axis variable is reservoir_name, the second level grouping variable (for the legend colors) is scenario_name. I'd like to add an X-axis based on the grouping variable type_vulnerable. In other words, type_vulnerable would form a nested x-axis with a Traces & Years for each of the reservoir_name's.

I found this fiddle but have not had luck adapting it to this situation.

Any elegant solutions to this?