将 aes() 函数放在 ggplot() 函数内部或放在 geom_point/geom_bar/geom_line () 函数内部有什么区别?
我是编程和编码新手,正在尝试在 Google 课程中学习 R。
他们给出了几个使用 ggplot 函数进行视觉效果的示例,但他们以两种方式使用了 aes()。
第一:
ggplot(data=palmerpenguins) + geom_point(mapping = aes(x = bill_length_mm,y = body_mass_g)) aes() 函数位于 geom_point() 函数内部。
然后他们显示: ggplot(data, aes(x=distance, y= dep_delay, color=carrier, size=air_time, shape = Carrier)) + geom_point()
现在 aes() 函数位于 ggplot() 函数中,它们在其中指定数据集。
切换的原因是什么?看起来 aes() 可以放在任何一个地方。这是真的吗?对于像编码这样如此具体的事情,令人困惑的是为什么你可以用任何一种方式来做。任何解释将不胜感激。谢谢
I am new to programming and coding, trying to learn R in the Google course.
They have given several examples for visuals using the ggplot functions, but they have used the aes() in two ways.
First:
ggplot(data=palmerpenguins) + geom_point(mapping = aes(x = bill_length_mm,y = body_mass_g))
The aes() function is inside the geom_point() function.
Then they show:
ggplot(data, aes(x=distance, y= dep_delay, color=carrier, size=air_time, shape = carrier)) + geom_point()
Now the aes() function is in the ggplot() function, where they specify the dataset.
What is the reason for the switch? It seems like aes() can go in either place. Is this true? For something that is so specific like coding, it's confusing why you could do it either way. Any explanation would be appreciated. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你只有一层,那真的没关系。每个后来的(geom)都可以有它自己的一组映射。如果将其添加到 ggplot() 调用中,则如果后者未指定它自己的映射,则将使用该“默认”映射。您实际上可以在
ggplot()
和geom_
调用之外添加aes()
,这也将在默认情况下起作用。都是相同的这是一个具有不同映射的两个不同图层的示例
请注意,颜色仅适用于稍后的抖动,而不适用于箱线图图层。
If you only have one layer, it really doesn't matter. Each later (geom) can have it's own set of mappings. If you add it to the
ggplot()
call, that's the "default" mapping that will be used if the later doesn't specify it's own. You can acually add theaes()
outside theggplot()
andgeom_
calls and that will also act at the default. There are all the sameHere's an example with two different layers with different mappings
Note that the color will only apply to the jitter later, not to the boxplot layer.
如果您在
ggplot
调用中定义映射(使用aes()
),您将为所有附加的几何图形创建一组默认映射值。如果您想为添加的每个几何对象应用不同的映射,可以在几何对象本身的
aes()
调用中定义它们。If you define the mapping (use
aes()
) inside theggplot
call, you create a set of default mapping values for all the attached geoms.If you want to apply different mappings for each geom you add, you can define them in an
aes()
call within the geom itself.