【问题标题】:Logistic regression models in ggplot2 [duplicate]ggplot2中的逻辑回归模型[重复]
【发布时间】: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)

【问题讨论】:

  • 我已投票结束该问题,因为它与统计数据无关。
  • 在您的第一个示例中,您将xy 全局映射到ggplot。这些全局美学传递到其他层次。在第二个示例中,您不使用全局美学,而是仅在 geom_point 层中映射 xy。这些不会传递给其他层,所以stat_smooth 没有xy 美学可以使用,你会得到一个错误。

标签: r ggplot2


【解决方案1】:

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

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

【讨论】:

  • 说的有道理,明白了,谢谢
猜你喜欢
  • 1970-01-01
  • 2021-04-28
  • 1970-01-01
  • 2020-12-22
  • 1970-01-01
  • 2013-06-05
  • 2016-12-11
  • 2018-01-26
  • 2019-06-01
相关资源
最近更新 更多