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

Shortcut for scale_y_continuous(expand = expansion(c(0, 0.05)) #3962

Closed
thomas-neitmann opened this issue Apr 25, 2020 · 5 comments
Closed
Labels
feature a feature request or enhancement scales 🐍

Comments

@thomas-neitmann
Copy link

When creating histogram or bar charts the standard expansion factor of 0.05 creates an unsighty gap between the bars and the x axis.

library(ggplot2)
data(mtcars)
ggplot(mtcars, aes(hp)) +
  geom_histogram()

image

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:

scale_y_tight <- function(...) {
  scale_y_continuous(expand = expansion(0, 0.05), ...)
}

This is in the same spirit as ylab() or ylim().

Thinking a bit further maybe this could even be an option:

exact_ylim <- function(...) {
  scale_y_continuous(limits = c(...), expand = expansion())
}

This would force the axis limits to be exactly those requested which is not the case currently with ylim().

@clauswilke
Copy link
Member

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.

@thomas-neitmann
Copy link
Author

thomas-neitmann commented 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
Copy link
Member

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.

@alanocallaghan
Copy link

alanocallaghan commented May 22, 2020

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.

@teunbrand
Copy link
Collaborator

teunbrand commented 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.

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`.

Created on 2025-01-07 with reprex v2.1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement scales 🐍
Projects
None yet
Development

No branches or pull requests

5 participants