plot_mean_response_2D: Plot mean of a response variable across two dimensions,...

View source: R/export-ObservationCounts.R

plot_mean_response_2DR Documentation

Plot mean of a response variable across two dimensions, optionally stratified

Description

Computes the mean of a specified response variable for each combination of two variables and displays it as a heatmap using ggplot2. If a stratification variable is provided, means are calculated 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_mean_response_2D(
  data,
  response,
  x,
  y,
  stratify_by = NULL,
  for_each = NULL,
  color_gradient = c("blue", "beige", "red"),
  title = NULL,
  subtitle = NULL,
  x_lab = NULL,
  y_lab = NULL
)

Arguments

data

Data frame containing all input variables.

response

Numeric variable in data whose mean to compute.

x

Variable in data for the horizontal axis.

y

Variable in data for the vertical axis.

stratify_by

(Optional) Stratification variable. If supplied, means are computed for each combination of x, y and 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.

color_gradient

(Optional) Color gradient for the heatmap. Specified as a character vector of three colors, representing: c(<low_counts>, <middle_counts>, <high_counts>). Defaults to c("blue", "beige", "red"). Colors must be recognized by ggplot.

title

Plot title; defaults to NULL, in which case a title of the form "Mean <response>" is used.

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.

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.

Value

A ggplot object showing the mean of response across x and y, optionally faceted by facet_row and/or facet_col.

See Also

plot_mean_response_1D, ggplot

Examples

data("toy_data")
# Mean count by age and period
plot_mean_response_2D(toy_data, response = count, x = period, y = age)
# Mean count by age and period, stratified by education level
plot_mean_response_2D(toy_data, response = count, x = period, y = age,
                      stratify_by = education)
# Mean count by age and period, stratified by education level, for each sex
plot_mean_response_2D(toy_data, response = count, x = period, y = age,
                      stratify_by = education, for_each = sex)

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