在 aes() 中传递选项与在 gg​​plot2 中传递选项之间的区别 [英] Difference between passing options in aes() and outside of it in ggplot2

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

问题描述

在 ggplot2 中摆弄点大小选项后,我注意到在 geom_point()aes() 参数内部和外部传递大小的示例.来自 `geom_point() 哈德利网站上的页面:

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)

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

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.

此外,传递的大小也没有意义;我需要使用 range=c(min,max) 指定相对大小并将中断设置为两个大小而不是默认的五个.

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.

如果我在 aes() 之外传递大小,大小似乎很受尊重,但我无法在我的情节中获得图例;我尝试了 scale_sizescale_size_continuousscale_size_manual 都没有成功.

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.

geom_point() 页面有:

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

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

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

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.

推荐答案

当在 aes 中指定时,美学被映射到数据中的变量值.由于数据和可见美学之间存在映射,因此有一个图例显示了该映射.在aes 调用之外,美学只是设置 到一个特定的值.在您显示的示例中,所有点的大小(和颜色)都设置为相同的值.在这种情况下,不需要图例,因为大小(或颜色)没有传达任何意义(关于基础数据).

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

您在图例中看到的问题是由于大小被映射到连续变量.碰巧这个变量在你的数据中只有两个值,但原则上,连续变量可以采用任何值.如果它真的只是一个二选一的变量,请将其设为因子(在原始数据中或在美学调用中aes(size=factor(qsec)).

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

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

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