The Wayback Machine - https://web.archive.org/web/20240713025350/https://www.geeksforgeeks.org/how-to-create-a-histogram-of-two-variables-in-r/
Open In App

How to Create a Histogram of Two Variables in R?

Last Updated : 19 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to create a histogram of two variables in the R programming language.

Method 1: Creating a histogram of two variables with base R

In this approach to create a histogram pf two variables, the user needs to call the hist() function twice as there is two number of variables, and with the second hist() function the user needs to use the special argument of this function ‘add’  with which both the histogram with different variables will be plotted on the single plot.

Syntax:

hist(x, col = NULL, add=true)

Parameters:

  • x: a vector of values for which the histogram is desired.
  • col: a colour to be used to fill the bars
  • add: to merge the histogram

Example:

In this example, we will be creating the histogram using the hist() function with variables with random data points of 500 entries into the same plot.

R




# Variable-1 with 500 random data points
gfg1<-rnorm(500,mean=0.5,sd=0.1) 
  
# Variable-2 with 500 random data points
gfg2<-rnorm(500,mean=0.7,sd=0.1)  
  
# histogram of variable-1
hist(gfg1,col='green')
  
# histogram of variable-2
hist(gfg2,col='red',add=TRUE)


Output:

Method 2: Creating a histogram of two variables with ggplot2 package

In this method, to create a histogram of two variables, the user has to first install and import the ggplot2 package, and then call the geom_histrogram with the specified parameters as per the requirements and needs to create the dataframe with the variable to which we need the histogram in the R programming language.

To install & import the ggplot2 package, the user needs to use the below syntax:

Install - install.package('ggplot2')      
Import - library(ggplot2)                  

Example:

R




# load the package
library(ggplot2)
  
# create a dataframe
# with mean and standard deviation
gfg < - data.frame(toss=factor(rep(c("Head", "Tail"), each=500)),
                   coin=round(c(rnorm(500, mean=65, sd=5), 
                                rnorm(500, mean=35, sd=5))))
  
# plot the data using ggplot
ggplot(gfg, aes(x=coin, fill=toss, color=toss)) +
geom_histogram(position="identity")


Output:



Similar Reads

How to Create a Relative Frequency Histogram in R?
In this article, we will discuss how to create a Relative Frequency Histogram in the R programming language. Relative Frequency Histogram helps us to visualize the relative frequencies of values in a dataset. This shows how often a certain value is present in the dataset. A relative frequency histogram gives an empirical approximation to the probab
3 min read
R Variables - Creating, Naming and Using Variables in R
A variable is a memory allocated for the storage of specific data and the name associated with the variable is used to work around this reserved block. The name given to a variable is known as its variable name. Usually a single variable stores only the data belonging to a certain data type. The name is so given to them because when the program exe
6 min read
How to add Mean and Median to Histogram in R ?
Visualizing data can help gather insights from it that descriptive statistics can't. Anscombe's Quartet shows us how those statistics could be misleading, hence it becomes to analyze the data visually. Histograms are a way to visualize the data distribution of a continuous variable. Histograms are similar to bar plots, but bar plots visualize categ
3 min read
Histogram Equalization using R language
Histogram equalization is a technique for adjusting image intensities to enhance contrast. To enhance the image’s contrast, it spreads out the most frequent pixel intensity values or stretches out the intensity range of the image. By accomplishing this, histogram equalization allows the image’s areas with lower contrast to gain a higher contrast. H
3 min read
Extract Frequency Counts from Histogram in R
A histogram is basically used to represent data provided in a form of some groups. It is an accurate method for the graphical representation of numerical data distribution. It is a type of bar plot where the X-axis represents the bin ranges while the Y-axis gives information about frequency. We can extract Frequency Counts of Histogram using hist()
2 min read
Overlay Histogram with Fitted Density Curve in R
In this article, we will be looking at the different approaches to overlay histogram with fitted density curve in R programming language. Method 1: Using line() and density() functions In this approach for overlaying histogram with the fitted density curve user need not install or import any library as all the function are the base functions of the
3 min read
Add Count and Percentage Labels on Top of Histogram Bars in R
A histogram denotes the frequencies or contingency of values of the specified variable segregated into ranges. It groups the values into continuous ranges. Each bar of the histogram is used to denote the height, that is the number of values present in that specific range. The hist() method in base R is used to display a histogram of the given data
3 min read
Plot Normal Distribution over Histogram in R
In this article, we will discuss how to plot normal distribution over Histogram in the R Programming Language. In a random dataset, it is generally observed that the distribution of data is normal i.e. on its visualization using density plot with the value of the variable in the x-axis and y-axis we get a bell shape curve. The center of that curve
3 min read
How to Change Number of Bins in Histogram in R?
In this article, we will discuss how to change the number of bins in the histogram in the R Programming Language. A Histogram is a variation of a bar chart in which data values are grouped together and put into different classes. This grouping enables us to see how frequently data in each class occur in the dataset. While grouping the data into cla
2 min read
How to make histogram bars to have different colors in Plotly in R?
In this article, we are going to discuss making histogram bars to have different colors in Plotly in R Language. The Histogram is defined as the bar graph representation of data along the x-axis. The plotly package contains plot_ly() function which is used to visualize different plots in R like scatter plots, histogram,pie chart etc,. Syntax: plot_
1 min read
Article Tags :
three90RightbarBannerImg