R 比较数据分布

R 比较数据分布,r,histogram,data-visualization,R,Histogram,Data Visualization,我有两个向量: a = rnorm(10000,5) b = rnorm(10000,3) 我想比较这两个向量并使用: hist(a,xlim=c(0,10)) hist(b,col="gray20",add=T) 我还使用GGplot绘制了两个透明的直方图 相反,我可以绘制一个数据集的直方图,并将另一个数据集表示为一条线 如何做到这一点?您需要将其作为两个独立的层: ggplot() + geom_histogram(aes(x=a),fill="blue") + stat

我有两个向量:

a = rnorm(10000,5)
b = rnorm(10000,3)
我想比较这两个向量并使用:

 hist(a,xlim=c(0,10))
 hist(b,col="gray20",add=T)
我还使用GGplot绘制了两个透明的直方图

相反,我可以绘制一个数据集的直方图,并将另一个数据集表示为一条线


如何做到这一点?

您需要将其作为两个独立的层:

ggplot() + 
  geom_histogram(aes(x=a),fill="blue") + 
  stat_bin(aes(x=b),geom="line",colour="red")

您需要将其作为两个独立的层:

ggplot() + 
  geom_histogram(aes(x=a),fill="blue") + 
  stat_bin(aes(x=b),geom="line",colour="red")