Simple histogram plot wrong?

后端 未结 1 1320
予麋鹿
予麋鹿 2021-01-24 08:48
test <- rep(5,20)
hist(test,freq=FALSE,breaks=5)

The vector contains 20 times the value 5. When I plot this with freq=FALSE and b

相关标签:
1条回答
  • 2021-01-24 09:15

    hist plots an estimate of the probability density when freq=FALSE or prob=TRUE, so the total area of the bars in the histogram sums to 1. Since the horizontal range of the single bar that is plotted is (0,5), it follows that the height must be 0.2 (5*0.2=1)

    If you really want the histogram you were expecting (i.e. heights correspond to fraction of counts, areas don't necessarily sum to 1), you can do this:

     h <- hist(test,plot=FALSE)
     h$counts <- h$counts/length(test)
     plot(h)
    

    Another possibility is to force the bar widths to be equal to 1.0, e.g.

     hist(test,freq=FALSE,breaks=0:10)
    

    Or maybe you want

     plot(table(test)/length(test))
    

    or

     plot(table(test)/length(test),lwd=10,lend="butt")
    

    ? See also: How do you use hist to plot relative frequencies in R?

    0 讨论(0)
提交回复
热议问题