Skip to content

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

@thomas-neitmann

Description

@thomas-neitmann

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().

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions