学习ggplot2并且不理解为什么第二组代码会产生错误。我所要做的只是在第三组代码中为stat_smooth命令添加美学,它运行正常,但我不明白为什么。
ggplot(df, aes(x=wave.height, y=ship.deploy)) + geom_point() +
stat_smooth(method="glm", method.args=list(family="binomial"), se=FALSE)
ggplot(data = df) +
geom_point(mapping = aes(x = wave.height, y = ship.deploy)) +
stat_smooth(method = "glm", method.args = list(family = "binomial"), se = FALSE)
Error: stat_smooth requires the following missing aesthetics: x, y
ggplot(data = df) +
geom_point(mapping = aes(x = wave.height, y = ship.deploy)) +
stat_smooth(mapping = aes(x = wave.height, y = ship.deploy),method = "glm", method.args = list(family = "binomial"), se = FALSE)
答案 0 :(得分:1)
只有顶级指定的美学映射ggplot(aes())
才会被后续图层继承。在单个图层中指定的美学,geom_point(aes())
仅适用于该图层。
为避免重新指定相同的映射,请将它们放在顶部,就像在第一个代码中一样。