在hist中设置不同数量的间隔和相对频率

时间:2017-01-23 04:39:43

标签: r histogram intervals

我想为直方图设置5和7个间隔...

我的信息

abc <- c(38,40,30,35,39,40,48,36,31,36,47,35,34,43,41,36,41,43,48,40,32,34,
       41,30,46,35,40,30,46,37,55,39,33,32,32,45,42,41,36,50,42,50,37,39,
       33,45,38,46,36,31)
h = hist(abc)
h$density = h$counts/sum(h$counts)
plot(h,freq=FALSE,ylab = "Freq Rel")

感谢。

2 个答案:

答案 0 :(得分:1)

h=hist(abc, breaks=seq(30,55,l=6),
       freq=FALSE,main="Histogram",
       xlab="x",ylab="f(x)",yaxs="i",xaxs="i")
h$density = h$counts/sum(h$counts)
plot(h,freq=FALSE,ylab = "Freq rel",col="orange")

这是我的解决方案,你怎么看?

答案 1 :(得分:1)

您可以修改自己的代码来获取此信息:

h1=hist(abc, breaks = seq(min(abc), max(abc), length=6))
h1$density = h1$counts/sum(h1$counts)
h2=hist(abc, breaks = seq(min(abc), max(abc), length=8), col='red')
h2$density = h2$counts/sum(h2$counts)
par(mfrow=c(2,1))
plot(h1,freq=FALSE,ylab = "Freq Rel", col='red')
plot(h2,freq=FALSE,ylab = "Freq Rel", col='blue')

enter image description here

或使用ggplot2

library(gridExtra)
grid.arrange(ggplot(as.data.frame(abc), aes(abc)) +
               geom_bar(aes(y = ..count.. / sum(..count..)), fill='red', binwidth=(max(abc)-min(abc))/4),
             ggplot(as.data.frame(abc), aes(abc)) +
               geom_bar(aes(y = ..count.. / sum(..count..)), fill='blue', binwidth=(max(abc)-min(abc))/6))

enter image description here