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

geom_area does not align with geom_line with duplicate x coords #5227

Closed
banbh opened this issue Mar 13, 2023 · 1 comment · Fixed by #5239
Closed

geom_area does not align with geom_line with duplicate x coords #5227

banbh opened this issue Mar 13, 2023 · 1 comment · Fixed by #5239

Comments

@banbh
Copy link

banbh commented Mar 13, 2023

When x coordinates are duplicated geom_area does not align with geom_line. For example, in the following example, some of the x=2 coordinates are shifted back slightly (but visibly).

library(ggplot2) # ggplot2_3.4.1

data.frame(x = c(1,2,2,3), y = 4:1) |> 
  tidyr::crossing(id = c(0, 1e2)) |>
  ggplot(aes(x + id, y)) +
  facet_wrap(vars(id), scales = 'free_x') +
  geom_area() + # not aligned with lines from next geom_line
  geom_line()
@teunbrand
Copy link
Collaborator

Thanks for the report, I can reproduce the issue. Here is the plot rendered:

library(ggplot2)

data.frame(x = c(1,2,2,3), y = 4:1) |> 
  tidyr::crossing(id = c(0, 1e2)) |>
  ggplot(aes(x + id, y)) +
  facet_wrap(vars(id), scales = 'free_x') +
  geom_area() + # not aligned with lines from next geom_line
  geom_line()

Created on 2023-03-13 with reprex v2.0.2

Ultimately, I think this turns out less than ideal due to the fudge factor of 0.001 used in this line:

adjust <- diff(range(unique_loc, na.rm = TRUE)) * 0.001

While each panel has a range of 2 on the x-axis, this fudge factor becomes 0.001 * 103 = 0.103 units, which is visible. I think it might be best to do the alignment calculations per panel instead of for the data as a whole. The stacking, that stat = "align" makes easier, applies on a panel-level rather than the whole data level as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants