Basic histogram


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.

Data


Firstly, we create an example data.

# Create example data
set.seed(6789)
data = data.frame(value = rnorm(100))

Basic histogram using ggplot2 package


It 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




Contact

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.

SBIM