Skip to contents

Creates histograms with Insper's visual identity. The number of bins comes from the Sturges, Freedman-Diaconis, or Scott rule, or from a 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.

x

<data-masked> Numeric variable for the x-axis.

fill

Fill aesthetic. Accepts one of the options below.

  • A quoted color string, used as a static fill (e.g., "blue", "#FF0000").

  • A bare discrete column or factor expression, used for grouping (e.g., Species or factor(cyl)).

  • NULL (default), which uses Insper red.

palette

Character. Palette for mapped variables. If NULL (default), uses "main".

bins

Numeric. Number of bins. Used only when bin_method = "manual".

bin_method

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

border_color

Character. Color of the bar borders. Default is "white".

zero

Logical. If TRUE (default), draws a horizontal line at y = 0.

...

Additional arguments passed to ggplot2::geom_histogram().

Value

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