# Overlay Histogram with Fitted Density Curve in Base R & ggplot2 Package (2 Examples)

In this tutorial you’ll learn how to **fit a density plot to a histogram** in the R programming language.

Table of contents:

- Introduction of Example Data
- Example 1: Histogram & Density with Base R
- Example 2: Histogram & Density with ggplot2 Package
- Video, Further Resources & Summary

Let’s just jump right in!

## Introduction of Example Data

In the examples of this R programming tutorial, we’ll use the following example data:

set.seed(18462) # Create example data data <- data.frame(x = round(rnorm(1000, 10, 10))) head(data) # Print example data # x # 1 6 # 2 7 # 3 14 # 4 4 # 5 -10 # 6 16 |

set.seed(18462) # Create example data data <- data.frame(x = round(rnorm(1000, 10, 10))) head(data) # Print example data # x # 1 6 # 2 7 # 3 14 # 4 4 # 5 -10 # 6 16

As you can see based on the output of the RStudio console, our example data contains only one numeric column. Now, let’s draw these data…

## Example 1: Histogram & Density with Base R

Example 1 explains how to fit a density curve to a histogram with the basic installation of the R programming language. First, we need to use the hist function to draw a histogram:

hist(data$x, prob = TRUE) # Create histogram with Base R |

hist(data$x, prob = TRUE) # Create histogram with Base R

**Figure 1: Histogram Created with Base R.**

Figure 1 shows the output of the previous R code: A histogram without a density line. If we want to add a kernel density to this graph, we can use a combination of the lines and density functions:

lines(density(data$x), col = "red") # Overlay density curve |

lines(density(data$x), col = "red") # Overlay density curve

**Figure 2: Histogram & Overlaid Density Plot Created with Base R.**

Figure 2 illustrates the final result of Example 1: A histogram with a fitted density curve created in Base R.

## Example 2: Histogram & Density with ggplot2 Package

Example 2 shows how to create a histogram with a fitted density plot based on the ggplot2 add-on package. First, we need to install and load ggplot2 to R:

install.packages("ggplot2") # Install & load ggplot2 library("ggplot2") |

install.packages("ggplot2") # Install & load ggplot2 library("ggplot2")

Now, we can use a combination of the ggplot, geom_histogram, and geom_density functions to create out graphic:

ggplot(data, aes(x)) + # ggplot2 histogram & density geom_histogram(aes(y = stat(density))) + geom_density(col = "red") |

ggplot(data, aes(x)) + # ggplot2 histogram & density geom_histogram(aes(y = stat(density))) + geom_density(col = "red")

**Figure 3: Histogram & Overlaid Density Plot Created with ggplot2 Package.**

Figure 3 visualizes our histogram and density line created with the ggplot2 package. Note that the histogram bars of Example 1 and Example 2 look slightly different, since by default the ggplot2 packages uses a different width of the bars compared to Base R.

## Video, Further Resources & Summary

Some time ago I have published a video on my YouTube channel, which illustrates the content of this tutorial. You can find the video below:

**Please accept YouTube cookies to play this video.** By accepting you will be accessing content from YouTube, a service provided by an external third party.

If you accept this notice, your choice will be saved and the page will refresh.

Furthermore, you might want to have a look at some of the related articles which I have published on my homepage.

- Create Histogram in Base R
- Create Kernel Density Plot in R
- Create ggplot2 Histogram in R
- Draw Multiple Overlaid Histograms with ggplot2 Package
- Draw Multiple Variables as Lines to Same ggplot2 Plot
- R Graphics Gallery
- The R Programming Language

In this tutorial, I illustrated how to **combine histograms with probability on the y-axis and density plots** in the R programming language. If you have additional questions or comments, let me know in the comments section below.

### Statistics Globe Newsletter