R中直方图中的确切二进制数

时间:2013-06-05 05:02:19

标签: r statistics histogram

我在R中制作直方图时遇到了麻烦。问题是我告诉它制作5个分箱,但是它会产生4个分区,而我告诉它制作5个分区,它会产生8个分区。

data <- c(5.28, 14.64, 37.25, 78.9, 44.92, 8.96, 19.22, 34.81, 33.89, 24.28, 6.5, 4.32, 2.77, 17.6, 33.26, 52.78, 5.98, 22.48, 20.11, 65.74, 35.73, 56.95, 30.61, 29.82);

hist(data, nclass = 5,freq=FALSE,col="orange",main="Histogram",xlab="x",ylab="f(x)",yaxs="i",xaxs="i")

关于如何修复它的任何想法?

5 个答案:

答案 0 :(得分:21)

使用break参数:

hist(data, breaks=seq(0,80,l=6),
       freq=FALSE,col="orange",main="Histogram",
       xlab="x",ylab="f(x)",yaxs="i",xaxs="i")

enter image description here

答案 1 :(得分:12)

指定为nclass参数的整数用作建议:

  

该号码仅为建议

另一种解决方案是将cut您的矢量分成指定数量的组并绘制结果:

plot(cut(data, breaks = 4))

enter image description here

答案 2 :(得分:9)

根据Rob Hyndman的回答:

考虑到数据的最小值和最大值以及中断次数= number_of_bins + 1,可能更通用的解决方案是中断。

hist(data,breaks=seq(min(data),max(data),l=number_of_bins+1), 
     freq=FALSE,col="orange",
     main="Histogram",xlab="x",ylab="f(x)",yaxs="i",xaxs="i")

答案 3 :(得分:2)

如果你不反对使用基本图形以外的东西,总会有ggplot2做事方式:

  

库(GGPLOT2)

     

数据&lt; - data.frame(x =数据)

    ggplot(data, aes(x=x))+
      geom_histogram(binwidth=18,color="black", fill="grey")+
      scale_x_continuous(breaks=c(0,20,40,60,80)

ggplot2的文档很好:docs.ggplot2.org/current /

对于直方图特定示例:http://docs.ggplot2.org/current/geom_histogram.html

答案 4 :(得分:2)

我希望对我的数据点非常准确:

hist(data,breaks = seq(min(data),max(data),by=((max(data) - min(data))/(length(data)-1))))

这样可以在很少手动输入的情况下自动完成整个过程。