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

Feature request: alternate expand parameter #6020

Closed
teunbrand opened this issue Aug 1, 2024 · 1 comment · Fixed by #6027
Closed

Feature request: alternate expand parameter #6020

teunbrand opened this issue Aug 1, 2024 · 1 comment · Fixed by #6027

Comments

@teunbrand
Copy link
Collaborator

A common reason to muck around with a scale's expand argument is to ensure 0-anchored geometries like geom_bar() or geom_area) touch the x-axis without a gap.

library(ggplot2)

ggplot(mpg, aes(class)) +
  geom_bar() +
  scale_y_continuous(expand = c(0, 0, 0.05, 0))

The expand argument in scales is not very intuitive and I just happen to remember c(0, 0, 0.05, 0) as a rote incantation to get what I want. The helper function, expansion(), is also no help in this case, as I want to asymmetrically expand the scale. Adding to the confusion is that coord_cartesian(expand), which has the same argument name and fusses over the same setting, takes a true/false value instead of the four values.

What I'm proposing is to rewire the coord_*(expand) argument to take top/right/bottom/left true/false values. It mirrors the setup we have for margin(), so it shouldn't be too unfamiliar to regular users.

ggplot(mpg, aes(class)) +
  geom_bar() +
  coord_cartesian(
    expand = c(TRUE, TRUE, FALSE, TRUE)
  )
#> Error in if (!expand) {: the condition has length > 1

Created on 2024-08-01 with reprex v2.1.1

@teunbrand
Copy link
Collaborator Author

It turns out expansion() is vectorised, so you could do asymmetric expansion, but the docs don't mention this and it wasn't obvious to me.

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

Successfully merging a pull request may close this issue.

1 participant