Skip to contents

Create histograms using Insper's visual identity. Bin width comes from the Sturges, Freedman-Diaconis, or Scott rule, or from a bin count you supply.

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: "main", "muted", "turquesa", "vermelho", etc. If NULL (default), uses "main". 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

The four bin selection methods are described below.

  • **Sturges**: \(k = \lceil \log_2(n) + 1 \rceil\). Assumes roughly normal data and tends to undersmooth large samples.

  • **Freedman-Diaconis**: Derives bin width from the IQR, so outliers move it less than the other two.

  • **Scott**: Derives bin width from the standard deviation, assuming roughly normal data.

  • **Manual**: Uses the bin count given in `bins`.

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)
}