Skip to contents

Create histograms with formal bin selection methods using Insper's visual identity. Implements Sturges, Freedman-Diaconis, and Scott algorithms for optimal bin width.

Usage

insper_histogram(
  data,
  x,
  fill = NULL,
  palette = NULL,
  bins = NULL,
  bin_method = c("sturges", "fd", "scott", "manual"),
  border_color = "white",
  zero = TRUE,
  ...
)

Arguments

data

A data frame containing the data to plot

x

Variable for x-axis (numeric)

fill

Fill aesthetic. Can be:

  • A quoted color name/hex (e.g., `"blue"`, `"#FF0000"`) for static color

  • A bare column name (e.g., `factor(cyl)`) for discrete grouping

  • A continuous variable (e.g., `hp`) for gradient coloring (rare for histograms)

If `NULL` (default), uses Insper red.

palette

Character. Color palette name for variable mappings. Options: "categorical", "main", "bright", "reds", "teals", etc. If NULL (default), uses "categorical". Only applies to variable mappings.

bins

Numeric. Number of bins. Only used when bin_method = "manual"

bin_method

Character. Bin selection method: "sturges", "fd" (Freedman-Diaconis), "scott", or "manual". Default is "sturges"

border_color

Character. Color for bar borders. Default is "white"

zero

Logical. If TRUE, adds a horizontal line at y = 0. Default is TRUE

...

Additional arguments passed to ggplot2::geom_histogram()

Value

A ggplot2 object

Details

Bin selection methods:

  • **Sturges**: \(k = \lceil \log_2(n) + 1 \rceil\). Works well for normal distributions.

  • **Freedman-Diaconis**: Uses IQR to determine bin width. Robust to outliers.

  • **Scott**: Uses standard deviation. Optimal for normal distributions.

  • **Manual**: Specify exact number of bins with the `bins` parameter.

Examples

if (FALSE) { # has_insper_fonts()
# Simple histogram with Sturges method
insper_histogram(mtcars, x = mpg)

# Using Freedman-Diaconis method
insper_histogram(mtcars, x = mpg, bin_method = "fd")

# Manual bin specification
insper_histogram(mtcars, x = mpg, bin_method = "manual", bins = 15)
}