在ggplot2中aes()和其外部的传递选项之间的区别 [英] Difference between passing options in aes() and outside of it in ggplot2

查看:1069
本文介绍了在ggplot2中aes()和其外部的传递选项之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ggplot2中调整点大小选项之后,我注意到 geom_point的 aes()参数内部和外部都传递了大小的示例()。从Hadley网站的 geom_point()页面

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

#在
p内传递+ geom_point(aes (size = qsec))
p + geom_point(aes(size = qsec))+ scale_area()

#在
p外传递geom_point(color =red,size = 3)
p + geom_point(color =grey50,size = 4)



我们发现它们在传说中的表现有所不同。当传入 aes()时,我可以得到一个图例,尽管我只需要设置两个不同大小的点;否则,即使只使用了2个点,我也会得到5个点的大小范围。



另外,通过的尺寸不可理解,我需要使用 range = c(min,max)来指定相对大小,并将中断设置为两个大小而不是默认的五个大小。



如果我在 aes()之外传递大小,尺寸看起来很荣幸,但我无法在我的情节中获得图例;我试过 scale_size scale_size_continuous scale_size_manual ,但没有成功。



geom_point()页面有以下内容:

< blockquote>

以下美学可与geom_point一起使用。美学是
与aes函数映射到数据中的变量: geom_point(aes(x = var))



...

缩放控制着变量是如何映射到美学上的,并且在每个审美之后都会列出。

[列出所有审美选项(形状,颜色,大小等)]


仍然不完全清楚 aes()内部和外部的选项(这个问题的大小,但这应该对其他美学有意义)如何影响结果。

解决方案

当在 aes 中指定时,审美被映射到变量的值数据。由于数据和可见美学之间存在映射关系,因此有一个显示映射的图例。在 aes 调用之外,审美只设置为特定值。在你展示的例子中,所有点的大小(和颜色)被设置为相同的值。在这种情况下,没有必要使用图例,因为大小(或颜色)没有表达任何意义(关于基础数据)。



问题你看到的图例是由于大小映射到一个连续变量。恰巧这个变量在你的数据中只有两个值,但原则上,一个连续变量可以取任何值。如果它真的只是一个二选一变量,那就把它作为一个因素(无论是在原始数据还是在审美调用中),都可以使用 aes(size = factor(qsec))


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

这篇关于在ggplot2中aes()和其外部的传递选项之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆