How does ggplot scale_continuous expand argument work?
Asked Answered
L

2

44

I am trying to figure out how scale_continuous() expand argument works. According to scale_continuous documentation:

A numeric vector of length two giving multiplicative and additive expansion constants. These constants ensure that the data is placed some distance away from the axes. The defaults are c(0.05, 0) for continuous variables, and c(0, 0.6) for discrete variables.

Since they are "expansion constants", they are not actual units. Is there any way to convert them to some actual measurement to predict the actual output? For anything except 0, I just try random numbers until it works. There must be a more proper way to approach this.

Larvicide answered 25/5, 2017 at 1:25 Comment(1)
Not sure what is meant by "actual units": pixels, mm etc.? That's not how expand operates; it's just trying to provide a little padding based on the range of your data.Shrieval
E
55

The document is pretty clear. If you set limits manually, it would be more clear. I'll give some examples to show how it works:

the first argument gives expansion equal to its multiplication by limit range;

ggplot(mpg, aes(displ, hwy)) +
    geom_point() +
    scale_x_continuous(limits = c(1, 7), expand = c(0.5, 0))
# right most position will be 7 + (7-1) * 0.5 = 10

the second gives the absolute expansion added to both end of the axis:

ggplot(mpg, aes(displ, hwy)) +
    geom_point() +
    scale_x_continuous(limits = c(1, 7), expand = c(0.5, 2))
# right most position will be 7 + (7-1) * 0.5  + 2 = 12

Finally, the same expansion applies to both end of the axis.


2019-01-23: I learned from @C.Liu answer that the new expand_scale function could be used to achieve different expansion of lower limits and upper limits. The multi and add parameters are similar to the two values required for expand = but allows a vector of length two for setting the lower limit and upper limit. See C.liu's answer for detail.

2020-11-25: expand_scale() is deprecated at least since version 3.3.2, use expansion() instead. This is a name change only. The name and meaning of parameters of expansion remain the same as expand_scale.

Eaton answered 25/5, 2017 at 1:38 Comment(5)
This is much clearer. Is there a way to apply it to only the right or left side or is that much more complicated?Larvicide
@burger, as for as I know, there is no such option. But you can work around by manually setting the limits and optionally set expand to c(0, 0) if needed.Eaton
Thanks for the input. I don't have constant limits, so I was hoping to do that dynamically. Good to know it's not trivial.Larvicide
Is there any way to apply expand only on one end of the axis ?Aeroplane
@DanChaltiel, this is possible now with expand_scale, see the answer of @C.Liu.Eaton
P
34

expand_scale could be the choice for fine tuning only one end of the axis.

ggplot(mpg, aes(displ, hwy)) +
geom_point() +
scale_x_continuous(limits = c(1, 7), 
                   expand = expand_scale(mult = c(0, 0.5), 
                                         add = c(2, 0))
# left most position will be 1 - (7-1) * 0.0  -2 = -1, 
# right most position will be 7 + (7-1) * 0.5 = 10

This is a convenience function for generating scale expansion vectors for the expand argument of scale__continuous and scale__discrete. The expansions vectors are used to add some space between the data and the axes.

expand_scale(mult = 0, add = 0)

Arguments mult

vector of multiplicative range expansion factors. If length 1, both the lower and upper limits of the scale are expanded outwards by mult. If length 2, the lower limit is expanded by mult1 and the upper limit by mult[2]. add

vector of additive range expansion constants. If length 1, both the lower and upper limits of the scale are expanded outwards by add units. If length 2, the lower limit is expanded by add1 and the upper limit by add[2].

Generate expansion vector for scales

Prothalamion answered 22/10, 2018 at 4:12 Comment(2)
Beautiful choice!Asben
expand_scale() is deprecated; use expansion() instead.Asben

© 2022 - 2024 — McMap. All rights reserved.