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.

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


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

The results are shown above.


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

ds = read.csv("http://www.math.smith.edu/r/data/help.csv")
ds$gender = ifelse(ds$female==1, "female", "male")
library(lattice)
histogram(~ cesd | gender, data=ds)

The results are shown below.

3 comments:

Anonymous said...

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

February 8, 2012 at 12:07 PM
Ken Kleinman said...

All works in this blog are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License: http://creativecommons.org/licenses/by-nc-sa/3.0/.

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

February 8, 2012 at 2:07 PM
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

August 5, 2014 at 3:28 PM

Post a Comment

Subscribe to: Post Comments (Atom)

AltStyle によって変換されたページ (->オリジナル) /