为什么 stats 包中的 scatter.smooth 和 ggplot2 包中的 geom_smooth 给出不同的结果?

问题描述

这是基于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))

enter image description here

这是用ggplot生成的黄土曲线:

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

enter image description here

为什么它们不同?

解决方法

这是因为 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 日创建

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...