I'm plotting 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:
But if I specify 'gam' in the the geom_smooth
function, like:
geom_smooth(stat = 'smooth', color = 'Red', method = 'gam')
I get a different result:
Why is this happening?
By default, the loess or gam function is used for smoothing (in relation to the size of dataset). Using the method combo-box, you can change this function to lm, glm, gam, loess, rlm. By the formula property you can set the formula that will be used in the smoothing function.
geom_smooth() and stat_smooth() are effectively aliases: they both use the same arguments. Use stat_smooth() if you want to display the results with a non-standard geom.
stat_smooth: Add a smoother.Aids the eye in seeing patterns in the presence of overplotting.
The geom smooth function is a function for the ggplot2 visualization package in R. Essentially, geom_smooth() adds a trend line over an existing plot.
In the Documentation, you can see that:
smoothing method (function) to use, eg. "lm", "glm", "gam", "loess", "rlm".
For method = "auto" the smoothing method is chosen based on the size of the largest group (across all panels). loess is used for less than 1,000 observations; otherwise gam is used with formula = y ~ s(x, bs = "cs"). Somewhat anecdotally, loess gives a better appearance, but is O(n^2) in memory, so does not work for larger datasets.
Note that when method 'auto' uses gam, it also changes the formula. The default formula is
formula = y ~ x
So in the first scenario, it uses method gam, with the modified function function y ~ s(x, bs = "cs"). The second time, you only specify that method 'gam' should be used, but you don't overwrite the formula, so y ~x is still used. You could do this:
geom_smooth(stat = 'smooth', color = 'Red', method = 'gam', formula = y ~ s(x, bs = "cs"))
To get the same result. Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With