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

Axis alignment over multiple panels #5826

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

teunbrand
Copy link
Collaborator

@teunbrand teunbrand commented Apr 4, 2024

This is a proof-of-concept PR exploring to fix #5820.

Briefly, this PR adds extra spacers to the axis gtable that have 'null' units so that the spacers take up the 'available' space. This works to align labels across panels as 'available space' is flexible and adapts to the size of the cell in the plot's gtable, which is always* based on the largest axis.

* See caveat below

devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2

data.frame(
  F = c(rep("F1", 3), rep("F2", 3)),
  Y = c("AAAAAAAAAAAAAAAAAAA", "BBBBB", "CCCCCCCCC", "DDDDD", "EEEEEEEE", "FFF"),
  Cnt = c(10, 20, 30, 50, 40 ,60)
) |>
  ggplot(mapping = aes(y = Y, x = Cnt)) +
  geom_col() +
  facet_grid(
    rows = vars(F),
    scales = "free_y"
  ) +
  theme(
    axis.text.y = element_text(hjust = 0)
  )

I consider this PR a POC because this solution is not perfect. In some circumstances, the plot's gtable cell size allocated to axes is smaller than the actual axis size, in particular when facet_wrap() has ragged panels that need to be labelled on the ragged ends. In the plot below, notice that the right axis of the bottom-middle panel is misplaced and also the bottom axis of the top-right panel is misplaced.

# The only failing unit test is this plot
ggplot(mtcars, aes(mpg, disp)) +
  geom_point() +
  guides(x = "axis", y = "axis", x.sec = "axis", y.sec = "axis") + 
  facet_wrap(vars(cyl, vs), axes = "all", axis.labels = "margins")

Created on 2024-04-04 with reprex v2.1.0

However, the axis doesn't know when it will be placed in smaller cells and installing the plumbing to let the axis know would require tempering with already very complicated code that places back axes. Unless I can find some trick to mitigate this problem, I'm stuck at this 90% solution, which is why I wouldn't merge this PR for now.

@teunbrand teunbrand marked this pull request as draft April 4, 2024 14:35
@teunbrand teunbrand marked this pull request as ready for review April 5, 2024 10:24
@teunbrand teunbrand changed the title POC: axis alignment over multiple panels Axis alignment over multiple panels Apr 5, 2024
@thomasp85 thomasp85 marked this pull request as draft May 15, 2024 07:47
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 this pull request may close these issues.

When setting "scales" in facet_grid, the horizontal justification of the axis labels shifts among the panels
1 participant