You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Shortcut for scale_y_continuous(expand = expansion(c(0, 0.05)) #3962
To remove this gap currently one has to add scale_y_continuous(expand = expansion(c(0, 0.05)) to the plot. This is cumbersome to type, easy to forget and hard to grasp for beginners.
I'd love to have a shortcut for that, something like:
I think this is a good idea, but I'd propose a different interface. We already have ylab() and ylim(), so it would make sense to add something like yexpand(). It could take either string arguments (e.g. "both", "none", "upper", "lower"), which would apply default expansions on both side, on neither side, or on the upper or lower end only, or numerical arguments similar to expansion().
Not sure how to implement this though. I'll have to look into how ylab() and ylim() communicate their arguments to the relevant scale.
I like the idea of yexpand() but then the question arises what should be the default for the argument. For the use case I have in mind it should be "upper" which feels kind of unintuitive because you want to get rid of the lower expansion.
In any case, I'd be more than happy to implement something along these lines and make a pull request 😀
I've thought about this a bit and at this time I'm not sure how to implement this. xlab() and xlim() are implemented using entirely different mechanisms, and both seem ad-hoc. In general, we don't have a good approach to feeding in additional parameters into a scale that exists already. The same problems would arise if we wanted to, for example, set the breaks separately from the color mapping in a color scale. This has broader implications for other issues, such as default scales via theme (#2691), and we may have to think carefully about a good solution to this issue.
Seems like neither xlim nor xlab directly modify the scale, which seems non-ideal. This is, incidentally, something I've noticed already - if I use a function that uses ggplot2 internally to produce a plot, and I want to alter something about a scale (eg, add a transformation) I need to replace the scale entirely, which then triggers a message ("replacing scale for fill", or something along those lines. Apologies for being too lazy to generate one to check right now).
I guess if you make all aspects of all scales modifiable you have colour_lims, fill_lims etc (for all reasonable components of a scale and all types of scale) which may be a bit messy. Alternatively you could maybe have something like modify_scale(scale = "fill", trans = "log10") which doesn't require you to cover all bases but is maybe a bit less intuitive than things like xlim, ylab.
We now have the following, which is at least easier to understand: we turn off the expansion at the bottom of the plot.
It works at the coord level, so turning off the expansion wouldn't fix the scales in place.
devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2
ggplot(mtcars, aes(hp)) +
geom_histogram() +
coord_cartesian(expand= c(bottom=FALSE))
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Activity
clauswilke commentedon Apr 25, 2020
I think this is a good idea, but I'd propose a different interface. We already have
ylab()andylim(), so it would make sense to add something likeyexpand(). It could take either string arguments (e.g."both","none","upper","lower"), which would apply default expansions on both side, on neither side, or on the upper or lower end only, or numerical arguments similar toexpansion().Not sure how to implement this though. I'll have to look into how
ylab()andylim()communicate their arguments to the relevant scale.thomas-neitmann commentedon Apr 25, 2020
I like the idea of
yexpand()but then the question arises what should be the default for the argument. For the use case I have in mind it should be"upper"which feels kind of unintuitive because you want to get rid of the lower expansion.In any case, I'd be more than happy to implement something along these lines and make a pull request 😀
clauswilke commentedon Apr 30, 2020
I've thought about this a bit and at this time I'm not sure how to implement this.
xlab()andxlim()are implemented using entirely different mechanisms, and both seem ad-hoc. In general, we don't have a good approach to feeding in additional parameters into a scale that exists already. The same problems would arise if we wanted to, for example, set the breaks separately from the color mapping in a color scale. This has broader implications for other issues, such as default scales via theme (#2691), and we may have to think carefully about a good solution to this issue.alanocallaghan commentedon May 22, 2020
Seems like neither
xlimnorxlabdirectly modify the scale, which seems non-ideal. This is, incidentally, something I've noticed already - if I use a function that uses ggplot2 internally to produce a plot, and I want to alter something about a scale (eg, add a transformation) I need to replace the scale entirely, which then triggers a message ("replacing scale for fill", or something along those lines. Apologies for being too lazy to generate one to check right now).I guess if you make all aspects of all scales modifiable you have
colour_lims,fill_limsetc (for all reasonable components of a scale and all types of scale) which may be a bit messy. Alternatively you could maybe have something likemodify_scale(scale = "fill", trans = "log10")which doesn't require you to cover all bases but is maybe a bit less intuitive than things likexlim,ylab.teunbrand commentedon Jan 7, 2025
We now have the following, which is at least easier to understand: we turn off the expansion at the bottom of the plot.
It works at the coord level, so turning off the expansion wouldn't fix the scales in place.
Created on 2025-01-07 with reprex v2.1.1