Skip to contents

Histogram from numeric data, with optional grouping for multiple traces. Bins are computed using graphics::hist() with consistent break points across groups. Rectangles span their actual numeric intervals, including unequal widths. Grouped samples overlap with translucent fills. Intervals are right-closed, with the lowest edge included, using hist() boundary tolerance. Missing groups are excluded; empty samples have zero counts.

Usage

draw_histogram(
  x,
  group = NULL,
  breaks = "Sturges",
  palette = NULL,
  xlab = NULL,
  ylab = NULL,
  title = NULL,
  theme = NULL,
  margins = DEFAULT_MARGINS,
  width = NULL,
  height = NULL,
  element_id = NULL,
  filename = NULL,
  legend_position = "top",
  legend_placement = "outside",
  bins = NULL,
  bin_edges = NULL,
  normalization = "count",
  density = FALSE,
  n = 512L,
  bw = "nrd0",
  bandwidth = NULL,
  kernel = "gaussian",
  adjust = 1,
  fill_alpha = 0.25,
  na_rm = TRUE,
  verbosity = 1L,
  mode = "overlap",
  order = "input",
  bin_stat = "count",
  bar_mode = "overlay"
)

Arguments

x

Numeric or List: Values or named numeric vectors to bin.

group

Optional Atomic vector or single-column data frame: Grouping variable for multiple series.

breaks

Numeric, Character, or Numeric vector: Binning method. A single number (number of bins), a character string naming an algorithm (e.g. "Sturges", "Scott", "FD"), or a numeric vector of break points. Numeric forms are normalized to bins or bin_edges by the setup function. Bin counts are suggestions to hist().

palette

Optional Character: Series colors, overriding the theme palette for this chart. NULL uses the theme's.

xlab

Optional Character: X-axis title.

ylab

Optional Character: Y-axis title.

title

Optional Character: Chart title.

theme

Optional Theme: Theme override.

margins

Optional Named numeric vector or named list: Plot margins in pixels (or percentage strings) for any of "top", "right", "bottom", "left". Unspecified sides keep echarts' default auto-sizing. See draw_line() for details.

width

Optional Character or Numeric: Widget width.

height

Optional Character or Numeric: Widget height.

element_id

Optional Character: Explicit DOM element ID for the widget container. NULL lets htmlwidgets generate one.

filename

Optional Character: If provided, save the widget to this file via save_drawing().

legend_position

Character {"top", "bottom", "left", "right", "top-left", "top-right", "bottom-left", "bottom-right"}: Legend anchor. Top/bottom anchors use horizontal rows; left/right anchors use a vertical column. Corner anchors align within the top or bottom row.

legend_placement

Character {"outside", "inside"}: Relation to the plotting area. Outside placement reserves space for the complete legend; inside placement overlays the data. Neither setting adds a missing legend.

bins

Optional Integer [1, 1000000]: Suggested bin count.

bin_edges

Optional Numeric vector: Finite, strictly increasing edges spanning every retained observation. Overrides breaks; excludes bins.

normalization

Character: "count", "probability", "percent", "density", or "count_density".

density

Logical: Overlay a kernel density in histogram units.

n

Integer [2, Inf): Number of density evaluation points.

bw

Character or Numeric: Bandwidth selector ("nrd0", "nrd", "ucv", "bcv", "SJ", "SJ-ste", "SJ-dpi"). Numeric input is normalized to bandwidth by the setup function.

bandwidth

Optional Numeric (0, Inf): Explicit finite smoothing bandwidth in measurement units, overriding the selector.

kernel

Character: One of "gaussian", "epanechnikov", "rectangular", "triangular", "biweight", "cosine", or "optcosine".

adjust

Numeric (0, Inf): Finite bandwidth multiplier.

fill_alpha

Numeric [0, 1]: Distribution fill opacity.

na_rm

Logical: Whether to remove NA values before computing densities.

verbosity

Integer [0, Inf): Verbosity level for removed-NA messages.

mode

Character {"overlap", "ridge"}: Overlay samples or align rows on common x and y scales. Ridge rows use sample names in place of a legend. Increase figure height for many rows.

order

Character {"input", "mean", "median"}: Sample order. Summary orders are decreasing, retain ties in input order, and place empty samples last.

bin_stat

Character {"count", "sum", "mean", "min", "max"}: Statistic of x observations in each bin. Empty bins have height zero. Non-count statistics require normalization = "count" and density = FALSE.

bar_mode

Character {"overlay", "group", "stack"}: Histogram group layout. Stacks accumulate positive and negative values separately. Group/stack layouts require mode = "overlap" and density = FALSE.

Value

htmlwidget: Widget object.

Details

Normalization is within each sample: "count" is the raw count; "probability" and "percent" divide by sample size (and multiply by 100 for percent). "density" divides by sample size and bin width, so total area is one. "count_density" divides only by width, so area is sample size. Unequal-width bins require one of the two density scales.

With density = TRUE, each curve uses the same settings as draw_density() and is scaled to the chosen histogram units. Count, probability, and percent curves multiply the estimated density by the common bin width and the appropriate sample-size factor. Density/count-density curves need no width factor. The histogram and curve share a color and legend toggle.

Examples

draw_histogram(iris[["Sepal.Length"]], xlab = "Sepal length")