How to Overlay Normal Curve on Histogram in R (2 Examples)


Often you may want to overlay a normal curve on a histogram in R.

The following examples show how to do so in base R and in ggplot2.

Example 1: Overlay Normal Curve on Histogram in Base R

We can use the following code to create a histogram in base R and overlay a normal curve on the histogram:

#make this example reproducible
set.seed(0)

#define data
data <- rnorm(1000)

#create histogram
hist_data <- hist(data)

#define x and y values to use for normal curve
x_values <- seq(min(data), max(data), length = 100)
y_values <- dnorm(x_values, mean = mean(data), sd = sd(data)) 
y_values <- y_values * diff(hist_data$mids[1:2]) * length(data) 

#overlay normal curve on histogram
lines(x_values, y_values, lwd = 2)

overlay normal curve on histogram in R

The black curve in the plot represents the normal curve.

Feel free to use the col, lwd, and lty arguments to modify the color, line width, and type of the line, respectively:

#overlay normal curve with custom aesthetics
lines(x_values, y_values, col='red', lwd=5, lty='dashed')

Example 2: Overlay Normal Curve on Histogram in ggplot2

We can use the following code to create a histogram in ggplot2 and overlay a normal curve on the histogram:

library(ggplot2) 

#make this example reproducible
set.seed(0)

#define data
data <- data.frame(x=rnorm(1000))

#create histogram and overlay normal curve
ggplot(data, aes(x)) +
  geom_histogram(aes(y = ..density..), fill='lightgray', col='black') +
  stat_function(fun = dnorm, args = list(mean=mean(data$x), sd=sd(data$x)))

overlay histogram in ggplot2 in R

The black curve in the plot represents the normal curve.

Feel free to use the col, lwd, and lty arguments to modify the color, line width, and type of the line, respectively:

#overlay normal curve with custom aesthetics
ggplot(data, aes(x)) +
  geom_histogram(aes(y = ..density..), fill='lightgray', col='black') +
  stat_function(fun = dnorm, args = list(mean=mean(data$x), sd=sd(data$x)),
                col='red', lwd=2, lty='dashed'))

Note: You can find the complete documentation for stat_function here.

Additional Resources

The following tutorials explain how to perform other common operations in R:

How to Create a Relative Frequency Histogram in R
How to Specify Histogram Breaks in R

Leave a Reply

Your email address will not be published.