问题描述
这是基于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)
为什么它们不同?
解决方法
这是因为 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 日创建