How to Create a Histogram of Two Variables in R? Last Updated : 19 Dec, 2021 Summarize Comments Improve Suggest changes Share Like Article Like 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 barsadd: 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: Comment More infoAdvertise with us Next Article How to Make a Histogram with ggvis in R G geetansh044 Follow Improve Article Tags : R Language R-plots R-Charts R-Graphs R-ggplot +1 More 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 histog 3 min read How to Create a Scatterplot in R with Multiple Variables? In this article, we will be looking at the way to create a scatter plot with multiple variables in the R programming language. Â Using Plot() And Points() Function In Base R: In this approach to create a scatter plot with multiple variables, the user needs to call the plot() function Plot() function: 3 min read How to Make a Histogram with ggvis in R In R Programming ggvis is the successor of ggplot package which is used to visualize data. Mainly ggvis package is introduced to plot HTML graphs so that these plots can be used in shiny web applications with short efforts. The layer_histogram() in ggvis is used to ensure the plot is a Histogram Plo 2 min read How to Create a Two Way Table in R? In this article, we will create a two-way table in R programming language. A two-way table is used to display frequency for two categorical variables. The rows represent the categorical features and the column represents frequency. We can create two-way table using as.table() method. as.table() func 2 min read How to plot a histogram with various variables in Matplotlib in Python? In this article, we are going to see how to plot a histogram with various variables in Matplotlib using Python. A histogram is a visual representation of data presented in the form of groupings. It is a precise approach for displaying numerical data distribution graphically. It's a type of bar plot 3 min read How to Make a Histogram in Excel: 3 Easy Methods A histogram is a vital tool for analyzing and visualizing the distribution of data, allowing users to easily identify patterns, trends, and outliers. Commonly used in statistics and data-driven decision-making, histograms help present data frequency in a structured and readable format. Excel provide 6 min read Like