A histogram
is a representation of the distribution of a numeric variable. This
document explains how to build it with R and the ggplot2
package.
Firstly, we create an example data.
ggplot2
packageIt is relatively straightforward to build a histogram with
ggplot2
thanks to the geom_histogram()
function. Only one numeric variable is needed in the input.
library(ggplot2)
library(hrbrthemes) # library for themes
# Basic histogram
hist <- ggplot(data, aes(x=value)) +
geom_histogram(binwidth=.5, fill="#69b3a2", color="#e9ecef") + # vary bin width, colors
ggtitle("Histogram") + # title
theme_ipsum() + # theme of the plot
theme(
plot.title = element_text(size=15) # title size
)
hist
This document is a work of the statistics team in the Biostatistics and Medical Information Department at Saint-Louis Hospital in Paris (SBIM).
Based on The R Graph Gallery by Yan Holtz.