plot_counts_with_mean: Plot heatmap of observation counts with mean overlay,...

View source: R/export-ObservationCounts.R

plot_counts_with_meanR Documentation

Plot heatmap of observation counts with mean overlay, optionally stratified

Description

Computes counts of observations for each combination of two variables and displays them as a heatmap, with an overlaid line showing the mean of the second variable across the first. If a stratification variable is provided, observations are counted per strata and strata-specific heatmaps are displayed in individual panels. If an additional stratification variable is provided, separate plot windows are created for each level.

Usage

plot_counts_with_mean(
  data,
  x,
  y,
  stratify_by = NULL,
  for_each = NULL,
  title = NULL,
  subtitle = NULL,
  heatmap_legend = "Count",
  mean_legend = "Mean",
  viridis_color_option = "D",
  mean_color = "coral"
)

Arguments

data

Data frame containing all input variables.

x

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

y

Variable in data whose values define the y-axis for counts, and for which the mean is computed for each value for x.

stratify_by

(Optional) Stratification variable. If supplied, counts and means are computed for each level of stratify_by, and separate heatmaps are generated per level of stratify_by.

for_each

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

title

(Optional) Plot title; defaults to NULL, in which case a title of the form "\<Y\> distribution across \<X\>" is used.

subtitle

(Optional) Plot subtitle; defaults to NULL, or to "\<for_each\>: \<level\>" when for_each is supplied.

heatmap_legend

(Optional) Label for the heatmap legend; defaults to "Count".

mean_legend

(Optional) Label for the overlay mean line legend; defaults to "Mean".

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.

mean_color

(Optional) Color for the overlay mean line; defaults to "coral".

Value

If for_each is not supplied, a ggplot object showing a heatmap of counts with a mean overlay line, 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_binned_counts, ggplot

Examples

data("toy_data")
# Heatmap of counts by age vs. period with mean age overlay
plot_counts_with_mean(toy_data, x = period, y = age)
# Heatmap of counts by age vs. period with mean age overlay, stratified by education
plot_counts_with_mean(toy_data, x = period, y = age,
                      stratify_by = education)
# Heatmap of counts by age vs. period with mean age overlay, stratified by education, for each sex
plot_counts_with_mean(toy_data, x = period, y = age,
                      stratify_by = education, for_each = sex)

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