How to Create a Histogram of Two Variables in R


A histogram is a useful way to visualize the distribution of values for a given variable.

To create a histogram for one variable in R, you can use the hist() function. And to create a histogram for two variables in R, you can use the following syntax:

hist(variable1, col='red')
hist(variable2, col='blue', add=TRUE)

The following example shows how to use this syntax in practice.

Example: Create a Histogram of Two Variables in R

The following code shows how to create a histogram of two variables in R:

#make this example reproducible
set.seed(1)

#define data
x1 = rnorm(1000, mean=0.6, sd=0.1)
x2 = rnorm(1000, mean=0.4, sd=0.1)

#plot two histograms in same graph
hist(x1, col='red')
hist(x2, col='blue', add=TRUE)

Since the values of the histograms overlap, it’s a good idea to use rgb() colors with increased transparency:

#make this example reproducible
set.seed(1)

#define data
x1 = rnorm(1000, mean=0.6, sd=0.1)
x2 = rnorm(1000, mean=0.4, sd=0.1)

#plot two histograms in same graph
hist(x1, col=rgb(0,0,1,0.2), xlim=c(0, 1),
     xlab='Values', ylab='Frequency', main='Histogram for two variables')
hist(x2, col=rgb(1,0,0,0.2), add=TRUE)

Histogram for two variables in R

You can also add a legend to make the histograms easier to interpret:

#make this example reproducible
set.seed(1)

#define data
x1 = rnorm(1000, mean=0.6, sd=0.1)
x2 = rnorm(1000, mean=0.4, sd=0.1)

#plot two histograms in same graph
hist(x1, col=rgb(0,0,1,0.2), xlim=c(0, 1),
     xlab='Values', ylab='Frequency', main='Histogram for two variables')
hist(x2, col=rgb(1,0,0,0.2), add=TRUE)

#add legend
legend('topright', c('Variable 1', 'Variable 2'),
       fill=c(rgb(0,0,1,0.2), rgb(1,0,0,0.2)))

Histogram of two variables in R example


You can find more R tutorials on this page.

Leave a Reply

Your email address will not be published.