Welcome to the histogram section of the R graph gallery. A histogram is used to study the distribution of one or several variables, as explained in data-to-viz.com. If you're looking for a simple way to implement it in R, pick an example below. This page focuses on ggplot2 but base R examples are also provided. This online course can be a great addition to know more about ggplot2.
Histograms can be built with
ggplot2 thanks to the
geom_histogram() function. It requires only 1 numeric
variable as input. This function automatically cut the variable in
bins and count the number of data point per bin. Remember to try
different bin size
Control bin size
Playing with histogram bin size is an important step. Histogram appearance can greatly change, and so does the message you're trying to convey.
Using small multiple and histogram allows to compare the distribution of many groups with cluttering the figure.
Of course it is possible to build high quality histograms without
ggplot2 or the
tidyverse. Here are a few
examples illustrating how to proceed.
Two histograms on same Axis
Compare the distribution of 2 variables with this double histogram built with base R function.
Two histograms on split windows
Compare the distribution of 2 variables plotting 2 histograms one beside the other.
Histogram with colored tails
Coloring tails sometimes allow to highlight specific areas of the distribution.
Mirrored histogram in base R
A mirrored histogram allows to compare the distribution of 2 variables. Here is how to build one in base R.