Exact Number of Bins in Histogram in R

Exact number of bins in Histogram in R

Use the breaks argument:

hist(data, breaks=seq(0,80,l=6),
freq=FALSE,col="orange",main="Histogram",
xlab="x",ylab="f(x)",yaxs="i",xaxs="i")

Sample Image

R: Control number of histogram bins

From ?hist, there are several options for controlling the bins through the breaks argument.

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; the
breakpoints will be set to pretty values. If breaks is a function, the
x vector is supplied to it as the only argument.

For the greatest precision, you have to set the breakpoints exactly, either by supplying a vector of breakpoints, or a function to compute them. You need to cover the entire range of x with your breakpoints and there will be 1 more breakpoint than bins (i.e. no_bins + 1 = no_breaks).

Get index of density bin values of histogram in hist() R

There's a bit of confusion about what hist does or doesn't do here.

  1. There is no n= argument to hist, only breaks=. I think It gives the same result by chance since pretty() uses n= and that function is used to define the bins.
  2. Setting breaks=5000 does not guarantee 5000 bins, as @Onyambu notes, due to pretty()-ification of the break-points. From ?hist: ...the number is a suggestion only; as the breakpoints will be set to pretty values.
  3. testhist$density gives a density in each bin. You can verify this with:


set.seed(1)
x <- rnorm(1000, 1000, 100)
testhist <- hist(x, n=5000, xlim = c(0,5000), probability = TRUE)
length(testhist$mids)
#[1] 6820
length(testhist$density)
#[1] 6820
length(testhist$breaks)
#[1] 6821

6820 midpoints of bins, 6820 corresponding densities, and 6821 breaks since you need n+1 breaks to give n bins.

The original 1000 data-points are represented in these 6820 bins, with many of the counts and corresponding densities being zero.

sum(testhist$counts)
#[1] 1000
sum(testhist$counts == 0)
#[1] 5954
sum(testhist$density == 0)
#[1] 5954

If you want to know which original value of x corresponds with which bin, you can do:

cut(x, testhist$breaks, labels=FALSE)

R: histogram plot, number of bins = number of classes


hist(Dice,breaks=seq(1.5,12.5))

Sample Image

Histogram in R with 1 bin for zeros only

Use cut to have 0 on its own, then use seq to create bins:

barplot(table(cut(fish$num, c(0, seq(1, 15, 3)), right = FALSE)), space = 0)

Sample Image


Edit: First bar only includes zeros, see:

table(fish$num)
# 0 1 2 4 6 7 8 9 10 11 13
#31 4 1 1 2 1 2 1 1 4 2

table(cut(fish$num, c(0, seq(1, 15, 3)), right = FALSE))
# [0,1) [1,4) [4,7) [7,10) [10,13)
# 31 5 3 4 5

R histogram number of instances in each bin on plot

Try this

set.seed(1)
x<-rnorm(1:100)
y <- hist(x, plot=FALSE)
plot(y, ylim=c(0, max(y$counts)+5))
text(y$mids, y$counts+3, y$counts, cex=0.75)

which gives:

Sample Image



Related Topics



Leave a reply



Submit