Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between passing options in aes() and outside of it in ggplot2

Tags:

plot

r

ggplot2

size

After fiddling with point size options in ggplot2, I noticed examples in which size was passed both inside and outside the aes() parameter of geom_point(). From the `geom_point() page on Hadley's site:

p <- ggplot(mtcars, aes(wt, mpg))

# passed inside
p + geom_point(aes(size = qsec)) 
p + geom_point(aes(size = qsec)) + scale_area() 

# passed outside
p + geom_point(colour = "red", size = 3) 
p + geom_point(colour = "grey50", size = 4)

I've found these behave differently when it comes to legends. When passing inside aes() I can get a legend to appear, though I need to set breaks even though I only have two differently sized points; otherwise, I get a range of five point sizes even though only 2 are used.

Also, the sizes passed aren't understandably meaningful; I need to specify the relative size using range=c(min,max) and set breaks to just two sizes instead of the default five.

If I pass size outside of aes(), the sizes seem honored but I can't get a legend on my plot; I tried scale_size, scale_size_continuous, and scale_size_manual without success.

From the geom_point() page there's this:

The following aesthetics can be used with geom_point. Aesthetics are mapped to variables in the data with the aes function: geom_point(aes(x = var))

...

Scales control how the variable is mapped to the aesthetic and are listed after each aesthetic.

[Listing of all the aesthetic options here (shape, colour, size, etc.)]

From that, it's still not exactly clear how the options (size in this question, but this should be meaningful for other aesthetics) inside and outside of aes() affect the result.

like image 553
Hendy Avatar asked Jul 16 '12 20:07

Hendy


People also ask

What is the AES () command in ggplot2 used to control?

Aesthetic Mapping ( aes ) In ggplot2 , aesthetic means “something you can see”. Each aesthetic is a mapping between a visual cue and a variable.

What does the function AES () in the ggplot2 package execute?

Description. aes creates a list of unevaluated expressions. This function also performs partial name matching, converts color to colour, and old style R names to ggplot names (eg. pch to shape, cex to size)


1 Answers

When specified inside aes, an aesthetic is mapped to the value of a variable in the data. Since there is a mapping between the data and the visible aesthetic, there is a legend which shows that mapping. Outside of an aes call, the aesthetic is just set to a specific value. In the examples you show, the size (and colour) are set to the same value for all points. In this case, there is no need for a legend because the size (or colour) does not convey any meaning (with regard to the underlying data).

The issue you are seeing with the legend is due to the size being mapped to a continuous variable. It happens that there are only two values that this variable takes on in your data, but in principle, a continuous variable could take on any value. If it really is just a choice-of-two variable, make it a factor (either in the original data or in the aesthetic call aes(size=factor(qsec)).

like image 65
Brian Diggs Avatar answered Oct 03 '22 19:10

Brian Diggs