Draw Multiple Overlaid Histograms with ggplot2 Package in R (Example)

 

In this tutorial you’ll learn how to create overlaid and transparent histograms with the ggplot2 package in the R programming language.

The article looks as follows:

So now the part you have been waiting for – the examples…

 

Creation of Example Data

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

set.seed(97531)                                                     # Set seed for random data
data <- data.frame(values = c(rnorm(1000, 5, 3),                    # Create example data
                              rnorm(1000, 7, 2),
                              runif(1000, 8, 11)),
                   group = c(rep("A", 1000),
                             rep("B", 1000),
                             rep("C", 1000)))

Our data contains two columns: The variable values is containing the numeric values for the creation of three different histograms; and the variable group consists of the names of the three histograms (i.e. A, B, and C).

In preparation of the example, we also need to install and load the ggplot2 package to RStudio:

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

Now we can draw our overlaid histograms…

 

Example: Create Overlaid ggplot2 Histogram in R

In order to draw multiple histograms within a ggplot2 plot, we have to specify the fill to be equal to the grouping variable of our data (i.e. fill = group).

Furthermore, we have to specify the alpha argument within the geom_histogram function to be smaller than 1. The alpha argument specifies the transparency of our histograms and therefore allows to display multiple histograms overlaying each other.

Have a look at the following R code:

ggplot(data, aes(x = values, fill = group)) +                       # Draw overlaying histogram
  geom_histogram(position = "identity", alpha = 0.2, bins = 50)

 

multiple overlaid histograms in ggplot2 r

Figure 1: Multiple Overlaid Histograms Created with ggplot2 Package in R.

 

Figure 1 shows the output of the previous R syntax. As you can see, we created a ggplot2 plot containing of three overlaid histograms. The histograms are transparent, which makes it possible for the viewer to see the shape of all histograms at the same time.

 

Video, Further Resources & Summary

Do you need more info on the R programming codes of this tutorial? Then I can recommend to have a look at the following video which I have published on my YouTube channel. In the video, I’m explaining the contents of this tutorial in a live session.

 

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.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

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

 

Furthermore, you might want to read some of the related articles of this homepage.

 

To summarize: At this point you should have learned how to plot multiple overlaid histograms with the ggplot2 add-on package in R. If you have additional questions, let me know in the comments below.

 



Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top