Completion requirements
You will learn the layered syntax of ggplot2 for scatterplots in this section. It also demonstrates how regression lines can be added (compared with the base-R syntax shown in the introductory video).
Scatter plots with multiple groups
Add regression lines
Regression lines can be added as follow:
# Add regression lines ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) + geom_point() + geom_smooth(method=lm) # Remove confidence intervals # Extend the regression lines ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) + geom_point() + geom_smooth(method=lm, se=FALSE, fullrange=TRUE)
Note that you can also change the line type of the regression lines by using the aesthetic linetype = cyl.
The fill color of confidence bands can be changed as follow :
ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) + geom_point() + geom_smooth(method=lm, aes(fill=cyl))