calc_bin_width: Calculate bin width for a histogram using various recommended...

View source: R/calc_bin_width.R

calc_bin_widthR Documentation

Calculate bin width for a histogram using various recommended methods

Description

There are many different methods that have been recommended over the years to determine an appropriate bin width for a histogram. This function collects those methods so that they are available easily. No one is really recommended over another. A few should be considered and then choose the best to visualize the distribution.

Usage

calc_bin_width(x, binw_select = "FD")

Arguments

x

A numeric object

binw_select

Character choice for which method to use to calculate bin width: "FD" (Default), "Sturges", "Scott", "Square-root", "Rice", "Shimazaki", "Juran"

Value

A numeric

References

https://en.wikipedia.org/wiki/Histogram#Square-root_choice https://stats.stackexchange.com/questions/798/calculating-optimal-number-of-bins-in-a-histogram https://arxiv.org/pdf/1612.07216.pdf https://cran.r-project.org/web/packages/essHist/essHist.pdf https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.170.173&rep=rep1&type=pdf https://www.neuralengine.org//res/histogram.html https://www.qimacros.com/histogram-excel/how-to-determine-histogram-bin-interval/#:~:text=Here's

Examples

library(ggplot2)

calc_bin_width(diamonds$carat, binw_select = "FD")

ggplot(data = diamonds,
       aes(x = carat)) +
  geom_histogram(colour = "white",
                 binwidth = calc_bin_width(diamonds$carat, binw_select = "FD"))


calc_bin_width(diamonds$carat, "Juran")

ggplot(data = diamonds,
       aes(x = carat)) +
  geom_histogram(colour = "white",
                 binwidth = calc_bin_width(diamonds$carat, binw_select = "Juran"))


ggplot(economics_long, aes(value)) +
  facet_wrap(~variable, scales = 'free_x') +
  geom_histogram(colour = "white",
                 binwidth = function(x) calc_bin_width(x, binw_select = "FD"))


ggplot(economics_long, aes(value)) +
  facet_wrap(~variable, scales = 'free_x') +
  geom_histogram(colour = "white",
                 binwidth = function(x) calc_bin_width(x, binw_select = "Sturges"))

emilelatour/laviz documentation built on Nov. 11, 2024, 9:42 p.m.