You could, of course, plot a histogram with R for every data sample you want to look at. You can use the histogram() function pretty easily to plot histograms for different groups.

Using the formula interface, you can plot two histograms at once using the following code:

> library(lattice)
> histogram(~temp | factor(activ), data=beaver2)

The histogram() function uses a one-sided formula, so you don’t specify anything at the left side of the tilde (~). On the right side, you specify the following:

  • Which variable the histogram should be created for: In this case, that’s the variable temp, containing the body temperature.

  • After the vertical line (|),the factor by which the data should be split: In this case, that’s the variable activ that has a value 1 if the beaver was active and 0 if it was not. To convert this numeric variable to a factor, you use the factor() function.

The vertical line (|) in the formula interface can be read as “conditional on.” It’s used in that context in the formula interfaces of more advanced statistical functions as well.