我目前的数据集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)
但当然不起作用: - (
理想情况下,我的直方图如下所示:
像往常一样,我觉得必须有一个简单的方法来做到这一点。在任何“条件/分组”意义上,我可以从我的数据中提取这些图表,我认为得到可以推广到您想要根据特定条件制作的各种图表。另外,如果此问题得到解答,我真的很抱歉。我的主要困难在于弄清楚如何以有意义的方式提出问题。
提前感谢您的帮助!
答案 0 :(得分:14)
使用网格包:
library(lattice)
histogram( ~ escore | inst, data=X)
如果X
是您的data.frame对象。
答案 1 :(得分: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)