2013-02-01 78 views
9

我有两个变量,我想在下面的直方图中进行比较。对于直方图的每个bin,显示两个变量的频率,以便于比较它们。R中两个变量的直方图

enter image description here

+2

这个问题的答案说明了另一种方法,它可能更适合高斯数据:http://stackoverflow.com/questions/3541713/how-to-plot-two-histograms-together-in-r – Lenna

回答

5

可以使用prop.tablebarplot这样

somkes <- sample(c('Y','N'),10,replace=T) 
amount <- sample (c(1,2,3),10,replace=T) 
barplot(prop.table(table(somkes,amount)),beside=T) 

enter image description here

10

可以使用add参数hist(见?hist?plot.histogram):

hist(rnorm(1000, mean=0.2, sd=0.1), col='blue', xlim=c(0, 1)) 
hist(rnorm(1000, mean=0.8, sd=0.1), col='red', add=T) 

enter image description here

要了解的add参数我注意到,在?hist...争论说,这些都传递给plot.histogram参数,add?plot.histogram记录。或者,?hist底部的其中一个示例使用add参数。

+2

I建议使用col = rgb(0,0,1,0.5)和col = rgb(1,0,0,0.5),因此颜色是透明的,并且重叠部分可见性更好。 –