# Overlay Normal Density Curve on Top of ggplot2 Histogram in R (Example)

In this R tutorial you’ll learn how to draw a ggplot2 histogram and a normal density line in the same graph.

The tutorial will consist of one example for the plotting of histograms and normal curves. To be more precise, the tutorial contains this content:

Let’s start right away:

## Example Data, Packages & Default Plot

To start with, we need to construct some data that we can use in the following examples:

```set.seed(119864293) # Create example data data <- data.frame(x = rnorm(300)) head(data) # Print head of example data``` Have a look at the table that got returned after executing the previous R programming syntax. It shows that our example data has 300 observations and one column.

If we want to plot the data using the ggplot2 add-on package, we also have to install and load ggplot2:

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

Next, we can create a plot of the data using the ggplot2 package:

```ggplot(data, aes(x)) + # Draw regular histogram without density geom_histogram()``` As shown in Figure 1, we have created a ggplot2 histogram with default specifications and without a normal density curve on top.

## Example: Add Normal Density Curve to ggplot2 Histogram Using stat_function()

This example explains how to create a ggplot2 histogram with overlaid normal density curve.

First, we have to convert the y-axis values of our histogram to probabilities. Otherwise, our density curve will not be shown properly.

Have a look at the following R code and the resulting graphic:

```ggplot(data, aes(x)) + # Draw histogram with probabilities geom_histogram(aes(y = ..density..))``` Figure 2 shows the output of the previous code: A ggplot histogram with probabilities on the y-axis. However, there’s still no normal density line in the plot…

We can add such a normal density curve to our plot using the stat_function command as shown below:

```ggplot(data, aes(x)) + # Draw histogram with density geom_histogram(aes(y = ..density..)) + stat_function(fun = dnorm, args = list(mean = mean(data\$x), sd = sd(data\$x)), col = "#1b98e0", size = 5)``` After running the previous R syntax the ggplot2 histogram with normal density curve shown in Figure 3 has been drawn.

Looks great!

## Video, Further Resources & Summary

Do you want to know more about ggplot2 graphics? Then you may watch the following video of my YouTube channel. I’m illustrating the topics of this article in the video.

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.

In addition, you may have a look at some of the related articles on this homepage.

Summary: In this R tutorial you have learned how to overlay a normal distribution line on top of a ggplot2 histogram. Tell me about it in the comments below, in case you have further questions.

Subscribe to the Statistics Globe Newsletter
{"ft_lang_label":"__label__en","ft_lang_prob":0.7363553,"math_prob":0.9067438,"size":3856,"snap":"2022-27-2022-33","text_gpt3_token_len":890,"char_repetition_ratio":0.15628245,"word_repetition_ratio":0.18357489,"special_character_ratio":0.24455394,"punctuation_ratio":0.112676054,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99546623,"pos_list":[0,1,2,3,4,5,6,7,8,9,10],"im_url_duplicate_count":[null,3,null,3,null,3,null,3,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-07-04T20:47:09Z\",\"WARC-Record-ID\":\"<urn:uuid:8980bb10-4b00-4a06-baed-bf5c57390675>\",\"Content-Length\":\"134739\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:f946ef4d-e625-48e0-8923-56d5ab8642ae>\",\"WARC-Concurrent-To\":\"<urn:uuid:2bf7bbcd-a326-44a3-afba-6132e4109e64>\",\"WARC-IP-Address\":\"217.160.0.159\",\"WARC-Target-URI\":\"https://statisticsglobe.com/normal-density-curve-on-top-of-histogram-ggplot2-r\",\"WARC-Payload-Digest\":\"sha1:NTBLATJWHVAI4W4EBFMRKWVJXKGP5R4V\",\"WARC-Block-Digest\":\"sha1:CSIGVRJYGWTWE64QRUWTCPG5MKWS427Q\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656104496688.78_warc_CC-MAIN-20220704202455-20220704232455-00150.warc.gz\"}"}