JohnCoene / echarts4r

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

Line scatter with grouping variable, two y-axes, two categorical data points on x: assistance needed #546

Closed yogat3ch closed 1 year ago

yogat3ch commented 1 year ago

Hi @helgasoft & @JohnCoene, I'm at a loss for how to arrive at a plot with two y-axes, where the x-axis has only two points (category seems like the best way to achieve this), that preserves the grouping of the lines. I'm going for something like this (the lines on the left are a conjoined plot that I've already figured out and can be disregarded for this example):

image

Here's what I have so far:

Reprex ```r require(tibble) require(dplyr) require(echarts4r) d <- tibble::tribble( ~trace_number, ~timestep, ~metric_value, ~trace_value, 1, 2060, 100, 0, 2, 2060, 25, 0, 3, 2060, 25, 0, 4, 2060, 60, 0, 5, 2060, 32.5, 0, 6, 2060, 62.5, 0, 7, 2060, 92.5, 0, 8, 2060, 42.5, 0, 9, 2060, 100, 0, 10, 2060, 97.5, 0 ) dplyr::group_by(d, trace_number) |> echarts4r::e_chart(timestep) |> echarts4r::e_line(trace_value) |> echarts4r::e_line(metric_value, y_index = 1) ```

Any advice on how to achieve this? Thanks in advance

munoztd0 commented 1 year ago

Hi,

I'm not really sure what you really want so I tried to reproduce something that would be similar.

require(tibble)
#> Loading required package: tibble
require(dplyr)
#> Loading required package: dplyr
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
require(echarts4r)
#> Loading required package: echarts4r

d <- tibble::tribble(
  ~Species, ~Sepal.Length, ~Sepal.Width, ~trace_value,
              1,      "A",           100,            0,
              1,      "B",            25,            0,
              2,      "A",            45,            0,
              2,      "B",           100,           1,
              3,      "A",            25,           1,
              3,      "B",            45,           1
  )

 plot <- d |> 
            group_by(Species) |> 
            e_charts(Sepal.Length) |>
            e_line(Sepal.Width) |>
            e_line(trace_value, y_index = 1, itemStyle = list(opacity = 0.0), lineStyle = list(opacity = 0.0)) |>
            e_tooltip(trigger = "axis")

Created on 2023-06-13 with reprex v2.0.2.9000

image

Would that be the end visual ?

(This is just an hacky way but it's to be sure I understand)

yogat3ch commented 1 year ago

Hi @munoztd0, I do believe this will get me to where I need to go. I had a feeling that a longer format table was the way to go to get the x-axis right and this confirms it. I'll report back with the solution once solved. Thanks!

yogat3ch commented 1 year ago

Hi @munoztd0, I haven't quite been able to solve this. I've altered the starting data to accentuate the differential in scale between the features to better illustrate what I'm hoping to achieve. I'd like the trace_value to be plotted on the left y-axis with it's own min/max and the metric_value to be plotted on the right y-axis with it's own min/max. Is this possible? What I've got so far is below:

require(tibble)
require(dplyr)
require(echarts4r)
tibble::tribble(
  ~trace_number, ~timestep, ~metric_value,     ~trace_value,
              1,      2060,           100,  1373374.7224072,
              2,      2060,            25, 3571298.17887815,
              3,      2060,            25, 7604822.33805931,
              4,      2060,            60, 8339799.28681836,
              5,      2060,          32.5, 7790387.17420632,
              6,      2060,          62.5,  4165155.8124288,
              7,      2060,          92.5, 5292495.02117722,
              8,      2060,          42.5, 1853854.47525675,
              9,      2060,           100, 204271.290965844,
             10,      2060,          97.5, 429805.868342286
  ) |> 
  dplyr::select(-timestep) |> 
  tidyr::pivot_longer(
    cols = c(metric_value, trace_value),
    names_to = "timestep",
    values_to = "trace_value"
  )
dplyr::group_by(d, trace_number) |> 
  echarts4r::e_chart(timestep) |> 
  echarts4r::e_line(trace_value) |> 
  echarts4r::e_line(trace_value, y_index = 1)
munoztd0 commented 1 year ago
require(dplyr)
#> Loading required package: dplyr
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
require(echarts4r)
#> Loading required package: echarts4r

d <- tibble::tribble(
  ~trace_number, ~timestep, ~metric_value,     ~trace_value,
              1,      2060,           100,  1373374.7224072,
              2,      2060,            25, 3571298.17887815,
              3,      2060,            25, 7604822.33805931,
              4,      2060,            60, 8339799.28681836,
              5,      2060,          32.5, 7790387.17420632,
              6,      2060,          62.5,  4165155.8124288,
              7,      2060,          92.5, 5292495.02117722,
              8,      2060,          42.5, 1853854.47525675,
              9,      2060,           100, 204271.290965844,
             10,      2060,          97.5, 429805.868342286
  ) 

 plot <- d |> 
            mutate(metric_value = metric_value * 1e+5) |>
            dplyr::select(-timestep) |> 
            tidyr::pivot_longer(
              cols = c(metric_value, trace_value),
              names_to = "timestep",
              values_to = "trace_value"
            ) |> 
            mutate(metric_value = trace_value / 1e+5) |>
            group_by(trace_number) |> 
            e_charts(timestep) |>
            e_line(metric_value) |>
            e_line(trace_value, y_index = 1, itemStyle = list(opacity = 0.0), lineStyle = list(opacity = 0.0)) |>
            e_tooltip(trigger = "axis")

image

Created on 2023-06-20 with reprex v2.0.2.9000

This is again really hacky and kinda only work with constant differences but tell me if it helps

yogat3ch commented 1 year ago

Thank you @munoztd0, that is indeed hacky! It does appear that it is only possible to use two axes on a timeseries when there's constant differences, is this correct?

munoztd0 commented 1 year ago

Well the difference doesn't have to be constant but just linear (constant = a+b and linear = a* x)

yogat3ch commented 1 year ago

Ah, thank you for the clarification @munoztd0! We'll adapt this for our purposes. Thank you for all the help! Do you have a way of taking donations for the support?

munoztd0 commented 1 year ago

@yogat3ch you're welcome ! Thanks for the offer here is my buy me a coffee link !