1

I've made a histogram as follows:

data(mtcars)
hist(mtcars$disp)

How do I find out the bin width in the above histogram?

Regards

Henrik
  • 65,555
  • 14
  • 143
  • 159
Muhammad Kamil
  • 635
  • 2
  • 15
  • Related: [Getting frequency values from histogram in R](https://stackoverflow.com/questions/7740503/getting-frequency-values-from-histogram-in-r) (in the sense that both answers describes the elements of an object of class `histogram`) – Henrik Sep 06 '21 at 17:58

2 Answers2

3

R does a number of things by default in creating a histogram

You can print out this parameters of the histogram by assigning to an an objec histinfo and then printing:

breaks will give you the bin width:

histinfo<-hist(mtcars$disp)
histinfo

> histinfo
$breaks
 [1]  50 100 150 200 250 300 350 400 450 500

$counts
[1] 5 7 4 1 4 4 4 1 2

$density
[1] 0.003125 0.004375 0.002500 0.000625 0.002500 0.002500 0.002500
[8] 0.000625 0.001250

$mids
[1]  75 125 175 225 275 325 375 425 475

$xname
[1] "mtcars$disp"

$equidist
[1] TRUE

attr(,"class")
[1] "histogram"
TarJae
  • 72,363
  • 6
  • 19
  • 66
2

Save the result of the hist() call, then extract the breaks:

h <- hist(mtcars$disp)

h$breaks
#>  [1]  50 100 150 200 250 300 350 400 450 500
# To get the widths, use diff().  Here all are the same:
unique(diff(h$breaks))
#> [1] 50

Created on 2021-09-06 by the reprex package (v2.0.0)

Thanks to @BenBolker for the suggestion to calculate the width explicitly.

user2554330
  • 37,248
  • 4
  • 43
  • 90