我最近开始学习R,但我对ggplot2中的aes功能感到困惑。
我已经看到了两个不同的地方,其中aes放在代码中。
ggplot(data = mpg)+ geom_point(mapping = aes(x = …
找不到傻瓜,所以这里有一个答案:
美学中指定的美学 ggplot() 由后续图层继承。特定层中指定的美学仅特定于该层。这是一个例子:
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 关闭该层的继承。如果你的大多数图层使用相同的美学,这是非常有用的,但有一些不是。
inherit.aes = FALSE