What does it mean that the parameter "breaks" for one number in the histogram in R?
I am looking into a bar chart plot in R but I have a problem with the "breaks" parameter for one number. The help says:
breaks: a single number indicating the number of cells for the histogram
I did the following experiment:
data("women")
hist(women$weight, breaks = 7)
I think he should give me 7 bins, but the result is not what I expected! This gives me 10 bins.
Do you know what it means breaks = 7
? What does "number of cells" mean in help?
source to share
After carefully reading the argument to arguments page breaks
, she says:
breaks
one of:
- a vector giving breakpoints between the cells of the histogram,
- a function to calculate a vector of breakpoints,
- one number indicating the number of cells for the histogram,
a character string, naming the algorithm for calculating the number of cells (see "Details"),
function for calculating the number of cells.
In the last three cases, the number is a sentence only ; breakpoints will be set to value values. If breaks are a function, x is passed to it as the only argument.
So, as you can see, n
it only counts as a "suggestion", it is probably trying to get close to that value, but it depends on the input values ββand if they can be bucketd nicely n
(it uses a function pretty
to compute them).
Hence, the only way to force the number of breaks is to provide a vector of interval breakpoints between cells.
eg.
data("women")
n <- 7
minv <- min(women$weight)
maxv <- max(women$weight)
breaks <- c(minv, minv + cumsum(rep.int((maxv - minv) / n, n-1)), maxv)
hist(women$weight, breaks = breaks)
source to share