Skip to content

Instantly share code, notes, and snippets.

@senior
Last active June 19, 2017 22:11
Show Gist options
  • Save senior/9a8fd0a4549fe007d2bbfa35effa849a to your computer and use it in GitHub Desktop.
Save senior/9a8fd0a4549fe007d2bbfa35effa849a to your computer and use it in GitHub Desktop.

Use Sturges’ formula to get a better suited number of bins, rather than a one size fits all of 8. This will give us a starting point, using the orders by subtotal as an example, we would have:

; Orders has a count of 17624
(Math/ceil (+ 1 (/ (Math/log10 17624) (Math/log10 2))))
;-> 16

With this we have a better guess at a good number of bins, but we’d like to have round numbers for the bin width. The above would be a bin width of 5.4593750000000005. This will look a little odd, even when rounded. It’s even worse if the min/max are decimals along with the bin width being a decimal. Instead we can round down the min, round up the max, and figure out what the bin width would be for that “ideal” number of bins.

;; Max - 99.37
;; Min - 12.02
;; Number of bins - 16
(Math/ceil (/ (- 100 12.00) 16))
;-> 6.0

This rounds up the bin-width, and will lower our total number of bins to 15. Using our rounding, we’ll have bins at the following values of subtotal:

12 18 24 30 36 42 48 54 60 66 72 78 84 90 96

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment