Skip to content

Feature request: alternate expand parameter #6020

Closed
@teunbrand

Description

@teunbrand

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions