plot_counts_1D: Plot counts of observations across a single variable,...

View source: R/export-ObservationCounts.R

plot_counts_1DR Documentation

Plot counts of observations across a single variable, optionally stratified

Description

Computes the number of observations at each value of a specified variable and creates a line plot of these counts 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_counts_1D(
  data,
  x,
  stratify_by = NULL,
  for_each = 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.

stratify_by

(Optional) Stratification variable. If supplied, counts are computed for each combination of x and stratify_by, and separate lines are drawn 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 "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 stratify_var if it 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.

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

A ggplot object displaying counts across the variable supplied in x, optionally stratified by stratify_by. If for_each is supplied, separate plots are created in separate windows for each level. Visuals can be modified with ggplot2.

See Also

plot_counts_2D, plot_binned_counts, plot_counts_with_mean, ggplot

Examples

data("toy_data")
# Counts by age
plot_counts_1D(toy_data, x = age)
# Counts by age, stratified by education level
plot_counts_1D(toy_data, x = age,
               stratify_by = education)
# Count by age, stratified by education level, for each sex
plot_counts_1D(toy_data, x = age,
               stratify_by = education, for_each = sex)

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