Visualizing multiple distributions with ridgeplots can be done using the following steps:
- Load the libraries:
library(ggplot2) library(ggridges)
- Build a ggplot description:
ggplot(airquality) + aes(Temp, Month, group = Month) + geom_density_ridges()
- Explicitly make Month a factor:
ggplot(airquality) + aes(Temp, as.factor(Month) ) + geom_density_ridges()
- Color the ridges:
ggplot(airquality) + aes(Temp, Month, group = Month, fill = ..x..) + geom_density_ridges_gradient() + scale_fill_viridis(option = "C", name = "Temp")
- Reshape the dataframe and add facets:
library(tidyr) airquality %>% gather(key = "Measurement", value = "value", Ozone, Solar.R, Wind, Temp ) %>% ggplot() + aes(value, Month, group = Month) + geom_density_ridges_gradient() + facet_wrap( ~ Measurement, scales = "free")