该参数何时在aes()的内部或外部进行? [英] When does the argument go inside or outside aes()?

查看:69
本文介绍了该参数何时在aes()的内部或外部进行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注Wickham和Grolemund的数据科学R"的第1章有关可视化的问题.

I am following Chapter 1 of Wickham and Grolemund's "R for data science" on visualization.

我尝试过:

 ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, color = "blue"))

希望获得一个将所有点都染成蓝色的图,但是令我惊讶的是,它们全都是红色!在印刷版的第11页或

hoping to achieve a plot with all points colored blue, but instead, to my surprise, they were all red! Reading the correct code to achieve the blue points, in page 11 of the printed version or in Section 3.3 of the online version, I found it should be

ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy), color = "blue")

,实际上,他们指出,要手动设置美观,您必须在aes()函数之外,但在此处对应的geom,geom_point()中给出它.为什么会这样呢?对此行为的确切解释是什么?实际上,对我而言,正确的语法将是第一个命令,这似乎很自然.我想这个问题与层和/或变量的范围有关,但我无法理解.有人可以喂我吗?

and, in fact, they state that, to manually set an aesthetic you have to give it outside the aes() function, but inside the corresponding geom, geom_point() here. Why is it so? What is the exact explanation for this behavior? In fact, it seemed natural to me that the correct syntax would be the one of the first command.I guess this issue is related either to layers and/or to scope of variables, but I just could not get the hang of it... Can someone spoon feed me?

对不起,我没有正确做功课:这只是相应部分末尾文本中建议的练习1 ...但是答案仍然让我感到困惑.

Sorry for not doing my correct homework: this is just Exercise 1 proposed in the text itself at the end of the corresponding Section... The answer however still escapes me.

推荐答案

此问题,更具体地说,是上述两个命令的输出差异,在"ggplot2"第二版的5.4.2节中明确处理.图形进行数据分析",作者:哈德利·威克姆(Hadley Wickham)自己:

This issue and more specifically the difference in the output from the two mentioned commands are explicitly dealt with in Section 5.4.2 of the 2nd edition of "ggplot2. Elegant graphics for data analysis", by Hadley Wickham himself:

要么:

  • 您可以(在aes内部)将数据的变量映射到美观的内容,例如aes(..., color = VarX)或...
  • 您可以(在aes外部,但在geom元素内部)将美观性设置为恒定值,例如蓝色"
  • you can map (inside aes) a variable of your data to an aesthetic, e.g., aes(..., color = VarX), or ...
  • you can set (outside aes, but inside a geom element) an aesthetic to a constant value e.g. "blue"

在第一种情况下,为了映射美学,例如color,ggplot2根据所有可用颜色(在色轮上)的均匀平均值的一种来选择颜色,因为映射变量的值都是恒定的;为什么选择的颜色应该与您碰巧选择要映射的常数值一致?更明确地说,如果您尝试执行以下命令:

In the first case, of mapping an aesthetic, such as color, ggplot2 chooses a color based on a kind of uniform average of all available colors (at the colorwheel), because the values of the mapped variable are all constant; why should the chosen color coincide with the constant value you happend to choose to map from? More explicitly, if you try the command:

ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y =hwy, color = "foo"))

您将获得与原始问题的第一个命令完全相同的输出图.

you get exactly the same output plot as in the first command of the original question.

这篇关于该参数何时在aes()的内部或外部进行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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