rggplot2regressionloess

How to change lambda of loess function in geom_smooth?


I have been reading this article on how loess is defined. I'm trying to change the alpha and lambda parameters in the geom_smooth function of the tidyverse. Using a local dataset in R, I have plotted a normal loess using this default code:

#### Load Library ####
library(tidyverse)

#### Plot Default Loess ####
ggplot(iris,
       aes(x=Sepal.Width,
           y=Sepal.Length))+
  geom_point()+
  geom_smooth()

Which gives this typical loess plot:

enter image description here

However, I'm not entirely sure how to modify the alpha and lambda. I'm assuming so far that the span argument in geom_smooth is what alters alpha, as seen below:

#### Change Alpha ####
ggplot(iris,
       aes(x=Sepal.Width,
           y=Sepal.Length))+
  geom_point()+
  geom_smooth(span = .35)

enter image description here

From the description of alpha in the article, this seems to be correct. As alpha is lowered, fitting the line to more noise is normal. However, I didn't seem to see if lambda could be modified in the same fashion. Is there a way to change this in ggplot, or does one have to find some other manual way?


Solution

  • The geom_smooth function calls the stats::loess function which has the paramters span (alpha), and degree (lambda), which defaults to 2, but can be changed with the method.args parameter:

    library(tidyverse)
    iris %>% 
    ggplot(aes(x = Sepal.Width, y = Sepal.Length)) +
      geom_point() +
      geom_smooth(method = loess, method.args = list(degree = 1))