Monday, June 13, 2011

Example 8.40: Side-by-side histograms

It's often useful to compare histograms for some key variable, stratified by levels of some other variable. There are several ways to display something like this. The simplest may be to plot the two histograms in separate panels.

In SAS, the most direct and generalizable approach is through the sgpanel procedure.

proc sgpanel data = 'c:\book\help.sas7bdat';
panelby female;
histogram cesd;

The results are shown above.

In R, the lattice package provides a similarly direct approach.

ds = read.csv("")
ds$gender = ifelse(ds$female==1, "female", "male")
histogram(~ cesd | gender, data=ds)

The results are shown below.


Anonymous said...

Does the Side-by-side histogram at the top of the page have any copyrights on it?

Ken Kleinman said...

All works in this blog are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License:

If you have some use outside that license in mind, please just e-mail one of us. Thanks for asking!

Anonymous said...

For SAS.....If the 'female field' were continuous (say 1-10), how would you specify bin increments for each lattice graph in SAS? In R, one would enter the increment in the histogram statement - not sure about SAS