Catalogs of posts

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:

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

    ReplyDelete
    Replies
    1. 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!

      Delete
  2. 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

    ReplyDelete

Note: Only a member of this blog may post a comment.