r - 在ggplot中使用aes的两种方式有什么区别?
问题描述
我最近开始学习 R,但对 ggplot2 中的 aes 功能感到困惑。
我已经看到两个不同的地方 aes 被放置在代码中。
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point()
两者有什么区别?
解决方案
找不到骗子,所以这里有一个答案:
中指定的美学ggplot()
由后续层继承。在特定层中指定的美学仅特定于该层。这是一个例子:
library(ggplot2)
ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
geom_smooth()
ggplot(mtcars) +
geom_point(aes(wt, mpg)) +
geom_smooth() # error, geom_smooth needs its own aesthetics
当您希望不同的层具有不同的规格时,这非常有用,例如这两个图是不同的,您必须决定您想要哪个:
ggplot(mtcars, aes(wt, mpg, color = factor(cyl))) +
geom_point() +
geom_smooth()
ggplot(mtcars, aes(wt, mpg)) +
geom_point(aes(color = factor(cyl))) +
geom_smooth()
在单个图层上,您可以使用inherit.aes = FALSE
来关闭该图层的继承。如果您的大多数图层都使用相同的美学,但少数不这样做,这将非常有用。
推荐阅读
- reactjs - ReactJS Routing - Update Router, when Router and Link are in separate components
- php - 尝试安装 laravel 6,但命令返回 5.8.*
- javascript - vue.js render data getting 'of undefined'
- sql - 有没有办法根据扩展名提取文件名但可能未知值?
- c++ - Qt 错误:LNK1120:1 未解决的外部
- sql - 如何为表行实现类似 UNIX 的 ACL
- mysql - 重复的输入密钥 phpmyadmin
- python - matplotlib 中的 fill_between 函数有渐变功能吗?
- search - 如何使结果高于内容?Z-index 搜索字段问题
- java - 如何知道 Java 中 for-each 中的迭代值?