Home > Blockchain >  ggplot x axis expansion not consistent with facets
ggplot x axis expansion not consistent with facets

Time:12-01

I have some data I would like to display with a ggplot. The data has several groups (5 in this example), each group containing a variable number of subgroups.

# consistent simulation results
set.seed(1)

# packages
library(dplyr)
library(ggplot2)

# data
data.frame(group=c("group A",
                   rep("group B",2),
                   rep("group C",3),
                   rep("group D",4),
                   rep("group E",5)),
           subgroup=c("a",
                      "a","b",
                      "a","b","c",
                      "a","b","c","d",
                      "a","b","c","d","e"),
           var=rnorm(15,50,10))->data

I want to display these data with subgroup on the x-axis and facets for each group

data%>%
  ggplot(aes(x=subgroup,
             y=var)) 
    geom_col() 
    facet_grid(.~group,
               scales="free",
               space="free")->p

plot p looks like this: no expansion. Now I want to give a little space around group A, so I used scale_x_discrete(expand=expansion(mult=n)) trying various number for n

p scale_x_discrete(expand=expansion(mult=0.1)) labs(title=0.1)
p scale_x_discrete(expand=expansion(mult=0.2)) labs(title=0.2)
p scale_x_discrete(expand=expansion(mult=0.5)) labs(title=0.5)
p scale_x_discrete(expand=expansion(mult=0.7)) labs(title=0.7)
p scale_x_discrete(expand=expansion(mult=1)) labs(title=1)

Which gave me some interesting outputs:

Is there a way to make the spacing for each facet consistent? It seems like there is variable behavior in each facet depending on the number of bars present?

CodePudding user response:

It seems like there is variable behavior in each facet depending on the number of bars present?

That's what multiplicative expansion does. It multiplies the range - if you multiply a small range (1 or 2 bars) it pads by a little bit, if you multiply a big range (10 bars) it pads by a lot.

Is there a way to make the spacing for each facet consistent?

Yes, use additive expansion (expansion(add = X)) not multiplicative (expansion(mult = X)).

Generally, additive expansion makes more sense for discrete scales, and multiplicative expansion makes more sense for continuous scales. This is reflected in the defaults, from ?discrete_scale

The defaults are to expand the scale by 5% on each side for continuous variables, and by 0.6 units on each side for discrete variables.

This add = 0.6 default is what you show in the "no expansion" picture.

  • Related