+5 votes
in Programming Languages by (71.8k points)

When I run the hist() function with some value for the "breaks" parameter, the function does not return the correct number of bins. 

E.g., in the below code, I want to divide the data into 25 bins, but hist() splits the data into 20 bins. How can I get the correct number of bins using the hist() function?

> rv <- runif(1000)

> hs <-  hist(rv, breaks = 25, plot = FALSE)

> length(hs$breaks)

[1] 21

> length(hs$counts)

[1] 20

1 Answer

+1 vote
by (349k points)
selected by
 
Best answer

Instead of giving a single number for the parameter "breaks," you can use a vector giving the breakpoints between histogram cells. That way, you will get the current number of bins.

Here is an example to show how to use a vector instead of a number for the parameter "breaks":

> rv <- runif(1000)
> hs <-  hist(rv, breaks = seq(floor(min(rv)), ceiling(max(rv)),1/25), plot = FALSE)
> length(hs$counts)
[1] 25
> length(hs$breaks)
[1] 26


...