Description
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