geom_smooth() adds a smoothed trend or fitted-model layer to a ggplot2 plot. With method = lm, it draws a linear regression line through points, visualizing the direction and strength of a relationship. section 4.5.3
Use the function to:
- Add a line of best fit to a scatterplot created with geom_point(). section 4.5.3
- Show the default 95% confidence interval, or remove it with se = FALSE. section 4.5.3
- Compare relationships such as salaries versus win shares, deflections versus wins, or points scored versus points allowed. section 5.6.1 section 14.4.1
NBA salaries and win shares
p7 <- ggplot(free_agents, aes(x = annual_salary, y = win_shares)) + geom_point(size = 3) + geom_smooth(method = lm, se = FALSE) The resulting line summarizes the positive relationship between annual salary and win shares; the reported correlation is 0.76. section 4.5.3
p7 <- ggplot(free_agents, aes(x = annual_salary, y = win_shares)) +
geom_point(size = 3) +
labs(title = "Annual Salaries vs. Win Shares",
subtitle = "correlation coefficient = 0.76",
x = "Annual Salaries",
y = "Win Shares") +
geom_smooth(method = lm, se = FALSE) +
scale_x_continuous(label = scales::comma) +
theme(plot.title = element_text(face = "bold"))
print(p7)
Inspect the complete ggplot2 example, including labels, comma-formatted salary values, and plot styling. section 4.5.3
