liveBook · Manning

Geom_density() function

As explained in Statistics Slam Dunk

The ggplot2 geom_density() function computes a smoothed density estimate and draws it as a plot layer.

geom_density() computes a smoothed density estimate and draws it as a ggplot2 layer. The resulting plot shows the distribution of a continuous variable: the x-axis contains observed values, while the y-axis displays probability density rather than frequency or counts. section 4.5.1 section 5.5.1

Use a density plot to:

  • Visualize the underlying distribution of a continuous variable. section 4.5.1
  • Identify peaks or modes and assess skewness or symmetry. section 4.5.1
  • Compare distributions such as salary, win shares, and age, optionally marking means and medians with geom_vline(). section 4.5.1

NBA free-agent salary distribution

Start with ggplot(free_agents, aes(x = annual_salary)), add geom_density(alpha = .3, fill = "salmon"), and then overlay dashed mean and median lines. Similar plots replace annual_salary with win_shares or age; patchwork can combine the plots.

How Statistics Slam Dunk covers it

How deep it goes
Explains the plotted quantities, key arguments, overlays, and interpretation, but does not detail the smoothing algorithm’s internals.
Examples
Uses NBA salary, win-shares, age, and hustle deflections, with mean and median reference lines.
What it assumes
Assumes a numeric variable and introduces ggplot() initialization, density values, and comparisons with frequencies.
What you can do after
Apply it via ggplot2 to draw and interpret smoothed distributions for numeric variables.
How it connects
Connects geom_density() with ggplot2, geom_vline(), histograms, boxplots, Shapiro-Wilk tests, scales, and patchwork.

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.