R Resources‎ > ‎

Overlapping Histograms

Here, I will introduce a way to two overlapping histograms with transparent colors, which makes a nice way to compare two distributions.

We start by plotting two histograms, each of 100 numbers with similar distributions but with slightly different means.
#Two objects: 100 numbers each, randomly distributed with a standard deviation of 1. One has mean of 10, the other with mean of 13.

# print the two histograms side-by-side

Now what we want to do is to put these two histograms together on the same plot, but to make it transparently colored so that we can see both distributions.

The key piece of code here is to assign the color using the rgb() function. The syntax is rgb(red, green, blue, alpha), where alpha<1 makes the color transparent. *Note that col=rbg(1,1,1,1) creates white, and rgb(0,0,0,1) creates black... think 'light' not 'paint'.

Also make sure to assign the same axes for the histograms. I also take out the title and the redundant axis names. par(new=TRUE) is a general way to let you plot one plot over another. I usually keep the axes in the second plot in, so that I'll notice if they happened to be on a different scale than the first, but once you've got it down it might be nice to take out the redundant axis lines by using axes=FALSE.


Play around with the rgb() yourself, but here's a quick set I've tried (opaque colors):

red: rgb(1,0,0,1)
green: rgb(0,1,0,1)
blue: rgb(0,0,1,1)
gray: rgb(0.5,0.5,0.5,1)
yellow: rgb(1,0.9,0,1)
purple: rgb(1,0,1,1)
orange: rgb(1,0.5,0,1)