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.
