开发者

Conditionally grouped histograms from my dataset

My current dataset data.df comes from about 420 students who took an 8-question survey under one of 3 instructors. escore is my outcome variable of interest.


    'data.frame':   426 obs. of  10 variables:
     $ ques01: int  1 1 1 1 1 1 0 0 0 1 ...
     $ ques02: int  0 0 1 1 1 1 1 1 1 1 ...
     $ ques03: int  0 0 1 1 0 0 1 1 0 1 ...
     $ ques04: int  1 0 1 1 1 1 1 1 1 1 ...
     $ ques05: int  0 0 0 0 1 0 0 0 0 0 ...
     $ ques06: int  1 0 1 1 0 1 1 1 1 1 ...
     $ ques07: int  0 0 1 1 0 1 1 0 0 1 ...
     $ ques08: int  0 0 1 1 1 0 1 1 0 1 ...
     $ inst  : Factor w/ 3 levels "1","2","3": 1 1 1 1 1 1 1 1 1 1 ...
     $ escore: int  3 1 5 5 3 3 4 4 2 5 ...
     

I'm wondering how I can generate escore histograms that are conditionally separated based upon the value of inst for a given observation. In my head, the pseudo-code might look like this:


    par(mfrow=c(1,3)) 
    hist(escore, data.df$inst = 1)
    hist(escore, data.df$inst = 2)
    hist(escore, data.df$inst = 3)

but of course that won't work :-(

Ideally, my histograms would look like this:

3 separate histograms of ~140 observations each, grouped according to their "inst" value http://terpconnect.umd.edu/~briandk/escoreHistogramsbyInstructor-1.png

As usual, I sense there's got to be an easy way to do this. In whatever "conditional/grouping" sense I can extract these graphs from my data, I assume it's got to be generalizable for all sorts of plots you'd want to make based on certain conditions.

Also, I'm really sorry if this question has been answered before. My primary difficulty is in figuring out how to ask it in a way that makes sense.

Thanks in advance for your开发者_Go百科 help!


Use the lattice package:

library(lattice)
histogram( ~ escore | inst, data=X)

if X is your data.frame object.


You can also do this in ggplot2:

data.df <- data.frame(inst = factor(sample(3, 426, replace=TRUE)), 
                      escore = sample(5, 426, replace=TRUE))
qplot(escore, fill=inst, data=data.df) + facet_wrap(~inst, ncol=3)

alt text http://www.cs.princeton.edu/~jcone/hists.png

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜