【发布时间】:2017-02-16 03:22:17
【问题描述】:
学习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)
【问题讨论】:
-
我已投票结束该问题,因为它与统计数据无关。
-
在您的第一个示例中,您将
x和y全局映射到ggplot。这些全局美学传递到其他层次。在第二个示例中,您不使用全局美学,而是仅在geom_point层中映射x和y。这些不会传递给其他层,所以stat_smooth没有x和y美学可以使用,你会得到一个错误。