Histograms in R

Good afternoon. Recently I've faced a problem of building a histogram in R. I type:
SP500hist<-hist(SP500logreturns,col="lightblue",breaks = 140, border="white",main="", xlab="Time",xlim=c(-0.001,0.001))

Hereby I specify, that the number of breaks = 140
But when I type:

I get 179 breaks instead of 140. How could it happen?

From the help file on hist()

breaks	one of:
               a vector giving the breakpoints between histogram cells,
               a function to compute the vector of breakpoints,
               a single number giving the number of cells for the histogram,
               a character string naming an algorithm to compute the number of cells (see ‘Details’),
               a function to compute the number of cells.
               In the last three cases the number is a suggestion only; as the
               breakpoints will be set to pretty values, the number is limited to
               1e6 (with a warning if it was larger). If breaks is a function, the
               x vector is supplied to it as the only argument (and the number
               of breaks is only limited by the amount of available memory).

So, the breaks argument is only a suggestion. If you require a precise number of breaks, you must pass in an exact vector of break point values of length number of breaks you want + 1.

x <- rnorm(1000)
n <- 5
h1 <- hist(x, breaks = n)

#> [1] 7
my_breaks <- seq(min(x), max(x), length.out = n + 1)
h2 <- hist(x, breaks = my_breaks)

#> [1] 5

Created on 2020-09-05 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.