3

我当前的数据集data.df来自大约 420 名学生,他们在 3 位教师中的一位指导下进行了 8 个问题的调查。escore是我感兴趣的结果变量。


    '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 ...
     

我想知道如何生成escore基于inst给定观察值有条件分离的直方图。在我的脑海中,伪代码可能如下所示:


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

但这当然行不通:-(

理想情况下,我的直方图应该是这样的:

3 个独立的直方图,每个直方图约 140 个观察值,根据它们的“inst”值分组 http://terpconnect.umd.edu/~briandk/escoreHistogramsbyInstructor-1.png

像往常一样,我觉得必须有一个简单的方法来做到这一点。在任何“条件/分组”的意义上,我可以从我的数据中提取这些图表,我认为它必须对你想要根据某些条件制作的各种图进行概括。

另外,如果这个问题之前已经回答过,我真的很抱歉。我的主要困难在于弄清楚如何以一种有意义的方式提出问题。

在此先感谢您的帮助!

4

2 回答 2

14

使用格子包:

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

如果X是你的 data.frame 对象。

于 2009-10-05T17:46:21.400 回答
12

您也可以在 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)

替代文字 http://www.cs.princeton.edu/~jcone/hists.png

于 2009-10-05T18:19:59.617 回答