plot_binned_counts: Plot counts of observations across bins of a numeric...

View source: R/export-ObservationCounts.R

plot_binned_countsR Documentation

Plot counts of observations across bins of a numeric variable, optionally stratified

Description

Bins a specified numeric variable into intervals, counts observations per value of a specified variable and bin groups, and plots lines for each bin group using ggplot2. If a stratification variable is provided, counts are calculated per strata and plotted as separate colored lines. If an additional stratification variable is provided, separate plot windows are created for each level.

Usage

plot_binned_counts(
  data,
  x,
  bin_by,
  stratify_by = NULL,
  for_each = NULL,
  n_bins = 8,
  bin_width = NULL,
  title = "Observation counts",
  subtitle = NULL,
  legend_title = NULL,
  x_lab = NULL,
  y_lab = NULL,
  viridis_color_option = "D"
)

Arguments

data

Data frame containing all input variables.

x

Variable in data whose values define the x-axis for counts.

bin_by

Numeric variable in data for which to bin observations.

stratify_by

(Optional) Stratification variable. If supplied, counts are computed for each combination of x, bin of bin_by and stratify_by, and separate panels are made for each level of stratify_by.

for_each

(Optional) Additional stratification variable. If supplied, separate plot windows are created per level of for_each.

n_bins

(Optional) Number of bins to create across bin_by; defaults to 8.

bin_width

(Optional) Width of the bins created across bin_by; defaults to NULL. Overrides n_bins if both are supplied.

title

(Optional) Plot title; defaults to "Observation counts".

subtitle

(Optional) Plot subtitle; defaults to NULL if for for_each is NULL, defaults to <name of for_each>: <level of for_each> for each plot window if for_each is supplied.

legend_title

(Optional) Legend title; defaults to name of bin_by + "bin".

x_lab

(Optional) Label for the x-axis; defaults to the name of x.

y_lab

(Optional) Label for the y-axis; defaults to the name of y.

viridis_color_option

(Optional) Option for color gradient; defaults to "D". Options are "A", "B", "C", "D", E", "F", "G", "H". See viridis for information, or experiment yourself.

Value

If for_each is not supplied, a ggplot object showing counts per x and bin groups, optionally faceted by stratify_by. If for_each is supplied, a named list of such plots.

See Also

plot_counts_1D, plot_counts_2D, plot_counts_with_mean, ggplot

Examples

data("toy_data")
# Counts by period, binned by age
plot_binned_counts(toy_data, x = period,
                   bin_by = age, n_bins = 4)
# Counts by period, binned by age, stratified by education levels
plot_binned_counts(toy_data, period,
                   bin_by = age, n_bins = 4,
                   stratify_by = education)
# Counts by period, binned by age, stratified by education levels, for each sex
plot_binned_counts(toy_data, period,
                   bin_by = age, n_bins = 4,
                   stratify_by = education, for_each = sex)

MAPCtools documentation built on June 25, 2025, 5:09 p.m.