R中两个变量的直方图

ale*_*lex 10 plot r data-visualization histogram

我有两个变量,我想在直方图中比较,如下所示.对于直方图的每个区间,显示两个变量的频率,以便于比较它们.

在此输入图像描述

mat*_*fee 12

您可以使用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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

要了解关于add我注意到,在参数?hist...论点说,这些都传递给参数plot.histogram,并add在被记录在案?plot.histogram.或者,底部的一个示例?hist使用add参数.

  • 我建议使用col = rgb(0,0,1,0.5)和col = rgb(1,0,0,0.5),因此颜色是透明的,重叠更明显. (2认同)

ags*_*udy 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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述