Histogram with several groups - ggplot2



A histogram displays the distribution of a numeric variable. A common task is to compare this distribution through several groups. This document explains how to do so using R and ggplot2.

Histogram Section About histogram

Several histograms on the same axis


Using small multiple


If the number of group you need to represent is high, drawing them on the same axis often results in a cluttered and unreadable figure.

A good workaroung is to use small multiple where each group is represented in a fraction of the plot window, making the figure easy to read. This is pretty easy to build thanks to the facet_wrap() function of ggplot2.

Note: read more about the dataset used in this example here.

Related chart types


Violin
Density
Histogram
Boxplot
Ridgeline



❤️ 10 best R tricks ❤️

👋 After crafting hundreds of R charts over 12 years, I've distilled my top 10 tips and tricks. Receive them via email! One insight per day for the next 10 days! 🔥