# 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')
```

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')
``` 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') 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') 