Search code examples
rggplot2smoothing

Adding method.args to ggplot stat_smooth() causes error


I am trying this example from Introduction to Data Science: Data Analysis and Prediction Algorithms with R, chapter 28:

library(tidyverse)
library(dslabs)
data("polls_2008")

polls_2008 %>% ggplot(aes(day, margin)) +
  geom_point() + 
  geom_smooth(span = 0.15, method.args = list(degree=1))

This causes the error, "Warning message: Computation failed in stat_smooth(): 'what' must be a function or character string." Removing the method.args... arguments results in normal operation. It seems that defining method.args as anything including an empty list causes the problem.

I am using R version 4.0.1 (2020-06-06) built for Windows and ggplot2_3.3.2. Thanks for any help.


Solution

  • I double checked and this worked. They changed the default for method from "auto" to NULL. This still functions the same as "auto" use to without method.arg. But I think you need to tell it what method you are running so it can properly use the method.args

    polls_2008 %>%
      ggplot(aes(day, margin)) +
      geom_point() + 
      geom_smooth(method = "loess", 
                  span = 0.15, 
                  method.args = list(degree=1))