Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does ggplot scale_continuous expand argument work?

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.

like image 778
burger Avatar asked May 25 '17 01:05

burger


People also ask

What does scale_x_log10 do in R?

scale_x_log10() and scale_x_log10() are shortcuts for the base-10 logarithmic transformation of an axis. The same could be achieved by using, e.g., scale_x_continuous(trans = "log10") . The latter can take a selection of options, namely "reverse" , "log2" , or "sqrt" .

What is Scale_y_continuous?

scale_y_continuous is used to set values for continuous y-axis scale aesthetics. The function is part of the ggplot2 package, and it's mostly used with ggplot objects to modify different parameters for graphs to be drawn.

What are scales in Ggplot?

Scales in ggplot2 control the mapping from data to aesthetics. They take your data and turn it into something that you can see, like size, colour, position or shape. They also provide the tools that let you interpret the plot: the axes and legends.


2 Answers

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.

like image 56
mt1022 Avatar answered Oct 18 '22 20:10

mt1022


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

like image 29
C.Liu Avatar answered Oct 18 '22 21:10

C.Liu