Addem
Addem

Reputation: 3919

Histogram in R not looking how I expect

So I'm just making a basic histogram to represent the probability mass function of a die roll. I figure this should be easy, just

x <- c(1, 2, 3, 4, 5, 6)
hist(x)

seems like it should work, but when I run this, the lowest bin has twice the frequency. Pretty clearly, values 1 and 2 are getting put into the same bin, but I don't see why or how to correct for this. Tried setting right=FALSE but that just shifted where the overlap occurs: now on the right.

Can someone explain why I'm getting the unexpected result and how to fix it please?

Upvotes: 0

Views: 129

Answers (2)

Anthony
Anthony

Reputation: 1543

Not sure how the default vector function creates the breaks but you can override them explicitly. For example,

hist(c(1,2,3,4,5), breaks=c(.5,1.5,2.5,3.5,4.5,5.5))

will give you the evenly distributed histogramenter image description here

Upvotes: 1

Barranka
Barranka

Reputation: 21047

A really simple solution:

x <- 1:6 # The same as your x <- c(1,2,3,4,5,6)
hist(x, breaks=0:6)

Upvotes: 1

Related Questions