Open In App

How to Plot Multiple Histograms in R?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to plot multiple histograms in the R Programming language.

Method 1: Multiple Histogram in Base R

To create multiple histograms in base R, we first make a single histogram and then add another layer of the histogram on top of it. But in doing so some plots may clip off as axis are made according to the first plot. So, we can add the xlim, and the ylim parameters in the first plot to change the axis limit according to our dataset.

Syntax:

hist( data, col, xlim, ylim )
hist( data, col )

where,

  • data: determines the data vector to be plotted.
  • xlim: determines the vector with x-axis limit.
  • ylim: determines the vector with y-axis limit.
  • col: determines the color of bars of the histogram.

Example:

Here, is basic multiple histograms made in the base R Language with help of hist() function.

R




# create data vector
x1 = rnorm(1000, mean=60, sd=10)
x2 = rnorm(1000, mean=0, sd=10)
x3 = rnorm(1000, mean=30, sd=10)
 
# create multiple histogram
hist(x1, col='red', xlim=c(-35, 100))
hist(x2, col='green', add=TRUE)
hist(x3, col='blue', add=TRUE)


Output:

Multiple Histograms in RGeeksforgeeks

Multiple Histograms in R

  • In this part, the rnorm() function is used to produce the three data vectors x1, x2, and x3. 1000 random numbers produced using a normal distribution are contained in each vector.
     
  • For each data vector in this section, histograms are generated using the hist() function. The data’s frequency distribution is determined and represented as a histogram via the hist() function.
     
  • The histogram for x1 is plotted using the first hist() method. The input was col=’red’ causes the histogram’s bars to be red, while xlim=c(-35, 100) determines the plot’s x-axis boundaries.
     
  • A second histogram for x2 is added to the current plot using the second hist() function. The input col=’green’ causes the bars to be green, and add=TRUE indicates that the histogram is active.
     
  • Similar to this, the third hist() function extends the plot with a histogram for x3. This histogram’s bars have a blue color (col=’blue’).

It is simpler to contrast the distributions of the various data vectors when several histograms are generated and added to the same plot. The histograms in this instance display the distribution of random numbers drawn from three distinct normal distributions, each with a unique mean and standard deviation. The distributions of x1, x2, and x3 are shown in the red, green, and blue histograms, respectively.

Method 2: Multiple Histogram Using ggplot2

To create multiple histograms in ggplot2, we use ggplot() function and geom_histogram() function of the ggplot2 package. To visualize multiple groups separately we use the fill property of the aesthetics function to color the plot by a categorical variable.

Syntax:

ggplot( df, aes( x, fill ) ) + geom_histogram( color, alpha ) 

where,

  • df: determines the data frame to be plotted.
  • x: determines the data variable.
  • fill: determines the color of bars in the histogram.
  • color: determines the color of the boundary of bars in the histogram.
  • alpha: determines the transparency of the plot.

Example:

Here, is basic multiple histograms made by using the geom_histogram() function of the ggplot2 package in the R Language.

R




# load library ggplot2
library(ggplot2)
  
# set theme
theme_set(theme_bw(12))
 
# create x vector
xAxis <- rnorm(500)            
 
# create groups in variable using conditional
# statements
group <- rep(1, 500)             
group[xAxis > -2] <- 2
group[xAxis > -1] <- 3
group[xAxis > 0] <- 4
group[xAxis > 1] <- 5
group[xAxis > 2] <- 6
 
# create sample data frame
sample_data <- data.frame(xAxis, group)
  
# create histogram using ggplot()
# function colored by group
ggplot(sample_data, aes(x=xAxis, fill = as.factor(group)))+
   geom_histogram( color='#e9ecef', alpha=0.6, position='identity')


Output:

Multiple Histograms in RGeeksforgeeks

Multiple Histograms in R

  • The powerful R data visualization package ggplot2 is loaded in the first line of code.
     
  • The theme of the plot is set to “theme_bw(12)” in the following line, which is a predefined theme in ggplot2 that employs a black-and-white color scheme with a base font size of 12.
     
  • Then, using the rnorm() method, the code generates a vector called xAxis that contains 500 random integers taken from a typical normal distribution.
     
  • The group vector is initialized on the next line by setting each value to 1. Based on specific criteria, this vector will be used to categorize the data points.
     
  • Depending on the values in the xAxis vector, conditional statements are used in the following lines to assign various groups to the group vector. The relevant indices in the group vector are given a corresponding group number by each condition, which determines if the value of xAxis is greater than a particular threshold.
     
  • The data. frame() function is used to build a data frame with the name sample_data after the groups have been defined. The group vectors and xAxis are combined into a single data frame.
     
  • Finally, the data frame (sample_data) and mapping aesthetics are specified using the ggplot() method. The fill aesthetic is set to as. factor(group), which turns the group variable into a factor and gives each distinct group a different color. The x variable is set to xAxis.
     

The histogram figure is made using the geom_histogram() tool. The transparency (alpha) of the histogram bars is set to 0.6, the color of the bars is specified as “#e9ecef,” and the bars are plotted in the “identity” position without stacking or adjusting.

Method 3: Multiple Histogram Using Plotly:

We can use the plot_ly() function in R and supply several add_histogram() calls to produce histogram traces for each variable or group in order to create numerous histograms.

  • df represents the data frame containing the variable to be plotted.
  • The plot_ly() function is used to create the Plotly figure. Set the data parameter to the data frame, 
  • specify type = “histogram” to create a histogram plot, and use the x parameter to specify the data variable for the histogram.
  •  Additional parameters can be added to customize the histogram, such as nbinsx, opacity, histogram, etc.
  • Customize the layout using the layout() function, specifying the title, x-axis, and y-axis labels, and other layout properties.

R




# Load required library
library(plotly)
 
# Generate sample data
set.seed(123)
x1 <- rnorm(1000)
x2 <- rnorm(1000, mean = 2)
 
# Create Plotly figure with multiple histograms
fig <- plot_ly() %>%
  add_histogram(x = ~x1, name = "Variable 1", nbinsx = 30, opacity = 0.7) %>%
  add_histogram(x = ~x2, name = "Variable 2", nbinsx = 30, opacity = 0.7) %>%
  layout(title = "Multiple Histograms",
         xaxis = list(title = "X Values"),
         yaxis = list(title = "Frequency"))
 
# Render the plot
fig


Output:

Multiple Histograms in RGeeksforgeeks

Multiple Histograms in R

  • The data frame containing the variables to be plotted is represented by df.
     
  • A histogram trace is added to the Plotly figure with each call to add_histogram(). The data variable for the histogram is specified by the x argument. To refer to a variable from a data frame, use the formula syntax (variable). 
     
  • The histograms can be customized by adding other parameters like name, nbinsx, opacity, etc.
    For each variable or group, you want to plot as a histogram, call add_histogram() again.
     

we can alter the layout by specifying the title, x-axis, and y-axis labels, and other layout properties using the layout() function.
Finally, render the plot using the plotly:plotly () function.



Last Updated : 13 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads