Skip to content

Additive constant in expand= behaves unexpectedly on discrete axes in facets with one group #2281

Closed
r-lib/scales
#161
@brianwdavis

Description

@brianwdavis

This issue occurs when using a discrete axis on in a faceted plot with free scales and free space, and one of the panels only has a single break on the axis. I would expect that the physical space taken up by each tick is the same (it is), but also that the expand = argument would behave independently of the number of ticks on the axis (it does not). This means that the physical space outside the ticks increases for panels with multiple ticks, but it is not changing for panels with only one tick.


library(ggplot2)
library(dplyr)
library(forcats)

iris %>% 
  mutate(S2 = fct_collapse(Species, "not versicolor" = c("setosa", "virginica"))) %>% 
  ggplot(aes(Species, Sepal.Length)) + 
  geom_boxplot(width = 1) +
  facet_grid(~S2, scales = "free_x", space = "free_x") +
  scale_x_discrete(expand = c(0, 0.6))

# Note the default `expand = c(0, 0.6)`

image

Here the boxes touch the edges of the panel in the versicolor facet, but there is an expansion gap in the not versicolor facet. This seems to not be expected behavior, since the space for 0.6 of an extra tick should be there in both panels.

If you try to increase the additive constant to add an expansion gap in the versicolor facet, no gap is created:

iris %>% 
  mutate(S2 = fct_collapse(Species, "not versicolor" = c("setosa", "virginica"))) %>% 
  ggplot(aes(Species, Sepal.Length)) + 
  geom_boxplot(width = 1) +
  facet_grid(~S2, scales = "free_x", space = "free_x") +
  scale_x_discrete(expand = c(0, 2))

# Note additive constant increased from 0.6 to 2

image

Space for a full discrete break was added on each side of the not versicolor facet, but no space was added to the sides of the versicolor facet, so the edges of the box still touch the edge of the panel.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behaviorfacets 💎

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions