liveBook · Manning

Geom_smooth() function

As explained in Statistics Slam Dunk

The ggplot2 geom_smooth() function adds a smoothed trend or fitted-model layer to a plot.

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

How Statistics Slam Dunk covers it

How deep it goes
Explains linear fitting and confidence-interval control, without detailing regression computation or model diagnostics.
Examples
Uses NBA salaries, win shares, deflections, wins, points scored, and points allowed as plotted variable pairs.
What it assumes
Assumes a ggplot2 plot with two numeric variables and introduces regression-line visualization through method = lm.
What you can do after
Apply it via ggplot2 to add fitted linear trends and control their displayed confidence intervals.
How it connects
Connects scatterplots, correlation coefficients, linear regression, confidence intervals, geom_point(), and ggplot2.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Statistics Slam Dunk. The section links point to the parts of the book it draws on.