library(rtemis.draw)4 Histograms
draw_histogram() bins numeric values, optionally by group, as counts, proportions, or densities, with an optional kernel density curve.
4.1 Single Numeric Vector
draw_histogram(penguins$body_mass)Use breaks for a binning rule (such as "FD"), a suggested number of bins, or explicit numeric edges. As in graphics::hist(), a requested count is a suggestion for conveniently spaced boundaries:
draw_histogram(penguins$body_mass, breaks = 20)4.2 Grouped
draw_histogram(
penguins$body_mass,
group = penguins$species
)4.3 Compare distributions across species
Species have different sample sizes. Normalize each histogram to unit area to compare shapes, and add a kernel density estimate on the same scale. A species’ legend entry controls both its bins and its curve.
draw_histogram(
penguins$body_mass,
group = penguins$species,
breaks = "FD",
normalization = "density",
density = TRUE,
xlab = "Body mass (g)",
ylab = "Density (per g)"
)Bins share boundaries across all samples and span their actual numeric intervals. "count" shows raw counts, "probability" shows sample fractions, and "percent" shows percentages. If bin_edges supplies unequal-width intervals, use "density" (total area one) or "count_density" (total area equal to sample size).
4.4 Grouped and stacked bins
For grouped histograms, bar_mode = "group" places groups beside each other within each bin; "stack" adds their heights, separating positive and negative values.
draw_histogram(penguins$bill_len, group = penguins$species,
bar_mode = "stack", xlab = "Bill length (mm)")bin_stat can summarize the observations in each bin with "sum", "mean", "min", or "max". These statistics use their original units, require normalization = "count", and do not support a density overlay.
4.5 Configuration
setup_HistogramConfig() describes a histogram by column names:
config <- setup_HistogramConfig(x = "body_mass", group = "species",
breaks = "FD", xlab = "Body mass (g)")
draw(config, data = penguins)