A trend line, also called a regression line or line of best fit, summarizes the direction of a relationship in plotted data. It is drawn to minimize the distances between the line and the data points; a steeper line indicates a stronger correlation. section 4.5.3 section 1.2.1
Use a trend line to:
- Add a linear summary to a scatterplot with ggplot2 by calling geom_smooth(method = lm). section 4.5.3
- Compare relationships, such as points allowed versus wins and points scored versus wins, with matched plots. section 14.4.1
- Show different patterns across subsets by drawing separate lines for selected ranges of the data. section 8.5.1
- Remove the automatically added 95% confidence interval with se = FALSE when the shaded band is not wanted. section 4.5.3
NBA salaries and win shares
A scatterplot of annual salary against win shares uses geom_point() for the observations and geom_smooth(method = lm, se = FALSE) for the fitted line. The reported correlation coefficient is 0.76, indicating a positive and strong relationship; the line summarizes that pattern rather than replacing the observations. section 4.5.3
MILp1 <- ggplot(MILx, aes(x = play_length2, y = avg, group = 1)) +
geom_line(aes(y = avg), color = "darkgreen", size = 2) +
geom_point(color = "wheat2", size = 3) +
labs(title = "Points Scored per Second Increment",
subtitle = "2019-20 Milwaukee Bucks",
caption = "regular season only",
x = "Number of Seconds into Possession",
y = "Average Number of Points Scored") +
geom_smooth(method = lm, color = "blue", se = FALSE) +
geom_smooth(method = lm, color = "gold",
data = MILx[MILx$play_length2 < 13,], se = FALSE) +
geom_smooth(method = lm, color = "purple",
data = MILx[MILx$play_length2 > 11,], se = FALSE) +
theme_classic() +
theme(plot.title = element_text(face = "bold")) +
inset_element(bucks, left = 0.80, bottom = 0.80,
right = 0.95, top = 0.95)
Inspect executable R code that overlays overall, before-12-second, and after-12-second regression lines on Milwaukee Bucks possession data. section 8.5.1



