1
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 breaks=5 I expect to see 1 bar at x=5 with height = 1.0, because the value 5 makes up 100% of the data.

Why do I instead see 1 bar that ranges from x=0 to x=5 and has height = 0.2 ??

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
user3182532
  • 1,097
  • 5
  • 22
  • 37

1 Answers1

4

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?

Community
  • 1
  • 1
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453