Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting method as default method in geom_smooth gives different result

Tags:

r

ggplot2

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:

enter image description here

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:

enter image description here

Why is this happening?

like image 681
snapcrack Avatar asked Jul 15 '17 05:07

snapcrack


People also ask

What is the default method in Geom_smooth?

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.

What is the difference between Stat_smooth and Geom_smooth?

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.

What does Stat_smooth method lm do?

stat_smooth: Add a smoother.Aids the eye in seeing patterns in the presence of overplotting.

What is Geom_smooth Ggplot?

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.


1 Answers

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!

like image 116
Florian Avatar answered Nov 06 '22 11:11

Florian