The ggplot2 package also makes it very easy to create regression lines through your data. You use the stat_smooth() function to create this type of line.
The interesting thing about stat_smooth() is that it makes use of local regression by default. R has several functions that can do this, but ggplot2 uses the loess() function for local regression.
This means that if you want to create a linear regression model you have to tell stat_smooth() to use a different smoother function. You do this with the method argument.
To illustrate the use of a smoother, start by creating a scatterplot of unemployment in the longley dataset:
ggplot(longley, aes(x=Year, y=Employed)) + geom_point()
Next, add a smoother. This is as simple as adding stat_smooth() to your line of code.
ggplot(longley, aes(x=Year, y=Employed)) +
geom_point() + stat_smooth()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
Finally, tell stat_smooth to use a linear regression model. You do this by adding the argument method=“lm”.
ggplot(longley, aes(x=Year, y=Employed)) +
geom_point() + stat_smooth(method="lm")
## `geom_smooth()` using formula 'y ~ x'