2017-02-16 143 views
0

学习ggplot2并不明白为什么第二组代码会产生错误。我所要做的就是在第三组代码中添加美学到stat_smooth命令,并且它运行良好,但我不明白为什么。ggplot2中的逻辑回归模型

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

我已投票结束此问题,因为它与统计数据无关。 – SmallChess

+0

在第一个示例中,您在'ggplot'中全局映射'x'和'y'。这些全球美学传递到其余层次。在第二个例子中,你不使用全局美学,而只是在'geom_point'图层中映射'x'和'y'。这些不会传递给其他图层,因此'stat_smooth'没有'x'和'y'美学用途,并且会出现错误。 – aosmith

回答

0

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

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

+0

这是有道理的,理解,谢谢 – Jeff