Use geom_hdr() and geom_hdr_lines() for density visualization
mainThe ggdensity package provides drop-in replacements for standard ggplot2 density functions to visualize Highest Density Regions (HDRs). HDRs represent the smallest regions that contain a specific amount of probability, making them more interpretable than standard contour plots.
- Use
ggdensity::geom_hdr()instead ofggplot2::geom_density_2d_filled()to show filled regions of probability. - Use
ggdensity::geom_hdr_lines()instead ofggplot2::geom_density_2d()to show contour lines of probability.
By default, geom_hdr() plots the $50%$, $80%$, $95%$, and $99%$ HDRs. You can customize these using the probs argument.
library("ggplot2")
library("ggdensity")
df <- data.frame("x" = rnorm(1000), "y" = rnorm(1000))
p <- ggplot(df, aes(x, y)) + coord_equal()
# Use geom_hdr() for filled regions
p + geom_hdr()
# Use geom_hdr_lines() for contour lines
p + geom_hdr_lines()