Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
oude-gao opened this issue Apr 3, 2024 · 10 comments
Open

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

oude-gao opened this issue Apr 3, 2024 · 10 comments
Assignees
Labels
bug Something isn't working

Comments

@oude-gao
Copy link

oude-gao commented Apr 3, 2024

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
Copy link
Owner

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

@oude-gao
Copy link
Author

oude-gao commented Apr 3, 2024

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

@munoztd0
Copy link
Collaborator

what do you mean?

image

@oude-gao
Copy link
Author

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
Copy link
Collaborator

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
Copy link
Author

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
Copy link
Author

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
Copy link
Collaborator

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
Copy link
Author

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
Copy link
Collaborator

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

@munoztd0 munoztd0 self-assigned this Apr 16, 2024
@munoztd0 munoztd0 added the bug Something isn't working label Apr 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants