【问题标题】:Why do scatter.smooth from package stats and geom_smooth from package ggplot2 give different results?为什么包 stats 中的 scatter.smooth 和包 ggplot2 中的 geom_smooth 会给出不同的结果?
【发布时间】:2021-02-03 12:07:25
【问题描述】:

这是基础r中的黄土平滑曲线

dat <- data.frame(RT = c(151, 160, 168, 169, 172, 175, 189, 279),
              IQ = c(123, 121, 115, 110, 105, 103, 100, 120))
with(dat, scatter.smooth(IQ, RT, span = 0.8))

这是用ggplot制作的黄土曲线:

ggplot(dat, aes(x = IQ, y = RT)) +
geom_point() +
geom_smooth(method = stats::loess, se = F, span = 0.8)

它们为什么不同?

【问题讨论】:

    标签: r ggplot2 loess


    【解决方案1】:

    这是因为scatter.smooth() 不使用默认的loess() 参数,而geom_smooth() 使用。如果您将 scatter.smooth() 中的默认方法参数提供给 ggplot2 方法,它们会产生非常相似的结果。

    dat <- data.frame(RT = c(151, 160, 168, 169, 172, 175, 189, 279),
                      IQ = c(123, 121, 115, 110, 105, 103, 100, 120))
    with(dat, scatter.smooth(IQ, RT, span = 0.8))
    
    library(ggplot2)
    

    ggplot(dat, aes(x = IQ, y = RT)) +
      geom_point() +
      geom_smooth(formula = y ~ x, method = "loess", se = F, span = 0.8,
                  method.args = list(degree = 1, family = "symmetric"))
    

    reprex package (v1.0.0) 于 2021 年 2 月 3 日创建

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-09
      • 2018-11-18
      • 2014-02-01
      • 2021-08-26
      • 1970-01-01
      • 1970-01-01
      • 2015-11-16
      • 2017-04-29
      相关资源
      最近更新 更多