Open In App

How to Plot Multiple Histograms in R?

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,



Example:

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




# 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 R

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,

Example:

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




# 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 R

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.




# 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 R

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.


Article Tags :