Setting method as default method in geom_smooth gives different result

I am drawing some data and have the following code:

ggplot(aes(x = x, y = y), data = data) + 
  geom_point(alpha = 1/15, color = 'blue')+
  scale_y_continuous('y')+
  scale_x_continuous('x')+
  geom_smooth(stat = 'smooth', color = 'Red')

      

The graph looks like this:

enter image description here

But if I specify "gam" in a function geom_smooth

like:

geom_smooth(stat = 'smooth', color = 'Red', method = 'gam')

      

I get a different result:

enter image description here

Why is this happening?

+3


source to share


1 answer


In the Documentation, you can see that:

an antialiasing method (function) to use eg. "lm", "glm", "gam", "leess", "RLM".

For method = "auto", the anti-aliasing method is selected based on the size of the largest group (across all panels). loess used fewer than 1000 observations; otherwise the gamma is used with the formula = y ~ s (x, bs = "CS"). Somewhat anecdotally, loess gives a better look and feel, but O (n ^ 2) in memory, so doesn't work for large datasets.

Note that when the 'auto' method uses gam, it also changes the formula. Default formula

formula = y ~ x



So, in the first scenario, he uses the gam method, with a modified function function y ~ s (x, bs = "cs"). The second time, you only indicate that the "gam" method should be used, but you don't overwrite the formula, so y ~ x is still used. You can do it:

geom_smooth(stat = 'smooth', color = 'Red', method = 'gam', formula = y ~ s(x, bs = "cs"))

      

To get the same result. Hope this helps!

+3


source







All Articles