A probability density function describes how concentrated continuous data are across their numeric range. In a density plot, the x-axis shows values while the y-axis shows density: higher density generally corresponds to more observations. section 4.5.1
Density plots help answer practical distribution questions:
- Reveal the underlying shape of continuous data without choosing histogram bin counts. section 4.5.1
- Highlight peaks or modes and make skewness or symmetry easier to assess. section 4.5.1
- Support distribution checks, such as comparing a density plot with a Shapiro-Wilk normality test. section 5.5.1
NBA data distributions
Plot annual salary, projected win shares, or age for shortlisted free agents. Add vertical lines for the mean and median to see how typical values relate to the distribution. A separate deflections plot, paired with a Shapiro-Wilk p-value of 0.4235, is treated as consistent with a normal distribution. section 4.5.1 section 5.5.1
p1 <- ggplot(free_agents, aes(x = annual_salary)) +
geom_density(alpha = .3, fill = "salmon") +
geom_vline(aes(xintercept = mean(annual_salary, na.rm = TRUE)),
color = "red", linetype = "longdash", size = .8) +
geom_vline(aes(xintercept = median(annual_salary, na.rm = TRUE)),
color = "blue", linetype = "longdash", size = .8) +
labs(title = "Annual Salary Distribution",
subtitle = "Shortlisted Free Agents",
x = "Annual Salary",
y = "Density",
caption = "Salary data is illustrative only") +
scale_x_continuous(labels = comma) +
theme(plot.title = element_text(face = "bold")) +
annotate("text", x = 18000000,
y = .000000025, label = "Mean", color = "black",
size = 4, fontface = "bold", angle = 90) +
annotate("text", x = 14000000,
y = .000000025, label = "Median", color = "black",
size = 4, fontface = "bold", angle = 90)
Use the ggplot2 example to draw a salary density curve, overlay mean and median lines, format the x-axis, and annotate the plot. section 4.5.1





