Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R中两个变量的直方图_R_Plot_Histogram_Data Visualization - Fatal编程技术网

R中两个变量的直方图

R中两个变量的直方图,r,plot,histogram,data-visualization,R,Plot,Histogram,Data Visualization,我有两个变量,我想在下面的直方图中进行比较。对于柱状图的每个箱子,显示了两个变量的频率,这使得比较它们变得容易 您可以像这样使用prop.table和barplot 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) somkes您可以使用add参数添加到hist(请参

我有两个变量,我想在下面的直方图中进行比较。对于柱状图的每个箱子,显示了两个变量的频率,这使得比较它们变得容易


您可以像这样使用
prop.table
barplot

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)

somkes您可以使用
add
参数添加到
hist
(请参见
?hist
?plot.histogram
):


为了了解
add
参数,我注意到在
?hist
中,
..
参数表示这些是传递给
plot.histogram
的参数,
add
记录在
plot.histogram
中。或者,
?hist
底部的一个例子使用了
add
参数。

这个问题的答案说明了另一种方法,它可能更适合高斯数据:我建议使用col=rgb(0,0,1,0.5)和col=rgb(1,0,0,0.5),这样颜色是透明的,重叠更明显。
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)