R ggplot2中的逻辑回归模型

R ggplot2中的逻辑回归模型,r,ggplot2,R,Ggplot2,正在学习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) +

正在学习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)

只有在顶层指定的美学映射,
ggplot(aes())
,才被后续层继承。在单个图层中指定的美学,
geom_point(aes())
仅适用于该图层


为了避免重新指定相同的映射,请将它们放在顶部,就像在第一个代码中一样。

我投票结束这个问题,因为它与统计无关。在第一个示例中,您在
ggplot
中全局映射
x
y
。这些全球美学将传递给其他层。在第二个示例中,您不使用全局美学,而是仅在
geom_点
层中映射
x
y
。这些不会传递到其他层,因此
stat\u smooth
没有
x
y
美学可供使用,您会得到一个错误。这很有意义,请理解,谢谢