JohnCoene / echarts4r

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

margin not working when there are multiple groups of series #623

Open oude-gao opened 3 months ago

oude-gao commented 3 months ago

Hi - I am trying to adjust margin in e_y_axis so the plot can be centered around the actual visual (e.g. line), vs. having 0 as the minimum value of y as default. However, when the dataset is group_byed before plotting, seems like it only applies to one group of the series being plotted:

mtcars |>
  group_by(cyl) |>
  e_charts(disp) |>
  e_line(mpg) |>
  e_y_axis_('mpg', margin = 0)

image

SessionInfo(): R version 4.3.3 (2024-02-29) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 20.04.6 LTS

Matrix products: default BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0 LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0

locale: [1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8 LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8
[7] LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C

time zone: America/New_York tzcode source: system (glibc)

attached base packages: [1] stats graphics grDevices datasets utils methods base

other attached packages: [1] aws.s3_0.3.21 gtExtras_0.5.0 gt_0.10.1 reactable_0.4.4
[5] shinyWidgets_0.8.1 bsicons_0.1.2 glue_1.7.0 echarts4r_0.4.5
[9] bslib_0.6.1 shiny_1.8.0 lubridate_1.9.3 forcats_1.0.0
[13] stringr_1.5.1 dplyr_1.1.4 purrr_1.0.2 readr_2.1.5
[17] tidyr_1.3.1 tibble_3.2.1 ggplot2_3.4.4 tidyverse_2.0.0
[21] here_1.0.1

JohnCoene commented 3 months ago

There is no margin for the y axis, can you tell me what you are trying to achieve?

oude-gao commented 3 months ago

I would like to apply margin = 0 to all groups here, so the yellow and green dots show up too.

munoztd0 commented 2 months ago

what do you mean?

image

oude-gao commented 2 months ago

By applying margin = 0 (I think that's the default when you use function like e_y_axis()), I am expecting the minimum value of the y axis tick would be the same as min(mtcars$mpg) instead of 0 in the original plot. However, the current function seems to only allow that value to be min(mtcars[mtcars$cyl == 4, 'mpg']) because cyl == 4 is the first serie to be plotted?

munoztd0 commented 2 months ago
library(echarts4r)

plot <- mtcars |>
  group_by(cyl) |>
  e_charts(disp) |>
  e_line(mpg) |>
  e_y_axis_('mpg', margin = 12)
#Numbers are used as percentage of total plotting area.

Created on 2024-04-10 with reprex v2.0.2

image

oude-gao commented 2 months ago

Thanks, can you explain how you came up 12 as the margin? I'm creating these plots in a Shiny app dynamically, so most likely I will need to be able to calculate the margins automatically.

oude-gao commented 2 months ago

The reason I submitted an issue is I thought there was a solution from the way margin was applied to the series plotted to this problem, so it represents the true margin as indicated in the function:

min = serie − margin and max = serie + margin

munoztd0 commented 2 months ago

Yeah, I see.. It conflist with the group by..

Here it works as intended..

library(echarts4r)

plot <- mtcars |>
  e_charts(disp) |>
  e_line(mpg) |>
  e_y_axis_('mpg', margin = 0)

image

I'm gonna look into it, but for now a simple workaround would be to do like this:

library(echarts4r)
library(dplyr)

a <- mtcars |>
  summarise(min = min(mpg), max = max(mpg)) 

plot <- mtcars |>
  group_by(cyl) |>
  e_charts(disp) |>
  e_line(mpg) |>
  e_y_axis( min = a$min, max = a$max )

plot

image

Created on 2024-04-16 with reprex v2.0.2

oude-gao commented 2 months ago

Thanks!! That's actually not a bad workaround at all. I have been doing something very similar anyways (calculating the margin by using min and max).

munoztd0 commented 2 months ago

Glad it helped ! Here is my buy me a coffee link if you feel like it