简体   繁体   中英

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

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.

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)) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM