Search code examples
rggplot2loess

Why do scatter.smooth from package stats and geom_smooth from package ggplot2 give different results?


This is the loess smoothing curve in base 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

And this is the loess curve produced with ggplot:

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

enter image description here

Why are they different?


Solution

  • It's because scatter.smooth() does not use the default loess() arguments, whereas geom_smooth() does. If you provide the method arguments that are default in scatter.smooth() to the ggplot2 method, they produce very similar results.

    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"))
    

    Created on 2021-02-03 by the reprex package (v1.0.0)