美学内部或外部aes() [英] aesthetic inside or outside aes()

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

问题描述

我试过了:
$ b
我已经试过了:

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

希望能够实现所有点蓝色的情节,但令我惊讶的是,它们都是红色的!在印刷版本的第11页或在线版本的第3.3节,我发现它应该是

  ggplot(data = mpg)+ geom_point(x = displ,y = hwy),color =blue)
<事实上,他们声明,要手动设置审美,您必须将其赋予aes()函数以外,但在相应的geom中,geom_point()在此处。为什么这样?这种行为的确切解释是什么?实际上,我认为正确的语法将成为第一个命令之一,这似乎很自然。我想这个问题与图层和/或变量的范围有关,但我无法得到它的诀窍。有人可以用勺子喂我吗?

编辑:对不起我没有做正确的作业:这只是练习1中提出的文字本身,在相应的章节末尾。答案然而仍然逃避我。

解决方案

这个问题,更具体地说,两个提到的命令的输出差异是在Hadley Wickham自己的第二版ggplot2。用于数据分析的优雅图形中第5.4.2节中明确地论述过:或者你可以映射(在 aes 中)a您可以将数据变量设置为美学的,例如 color ,或者您可以设置(在 aes 之外),但在<例如,code> geom 元素)一个美学值为一个常量值,蓝色。在第一种情况下,绘制审美图(如 color )时,ggplot2会根据所有可用颜色的均匀平均颜色(在颜色轮处)选择一种颜色,因为映射变量的值都是恒定的;为什么选择的颜色应该与您选择映射的常量值一致?更明确地说,如果你尝试下面的命令:

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

您可以得到完全相同的输出图原始问题的第一个命令。


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

I have tried:

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

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

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?

Edit: 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.

解决方案

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: either you can map (inside aes) a variable of your data to an aesthetic, e.g., color, or you can set (outside aes, but inside a geom element) an aesthetic to a constant value, "blue", for instance. 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天全站免登陆