
Faceted bar chart summarizing metadata distributions across patients
PlotMetaSummary.RdDeduplicates cell-level metadata to donor / patient level and plots a
multi-panel bar chart: one column per metadata variable, one row per
level of an optional row-splitting variable (e.g. Sex). Bars are stacked
by fill_variable and each segment is optionally annotated with its
count.
Usage
PlotMetaSummary(
data,
id_column = NULL,
variables,
fill_variable = NULL,
row_variable = NULL,
column_variable = NULL,
exclude = NULL,
percent = FALSE,
count_unit = c("auto", "donors", "cells"),
show_counts = TRUE,
min_count_to_label = 1L,
label_size = 3,
alpha = 0.85,
colors = NULL,
y_label = NULL,
output_dir = NULL,
object_name = "",
file_name = NULL,
return_data = FALSE,
return_flextable = FALSE,
numeric_stat = c("mean_sd", "median_iqr"),
cat_display = c("n_pct", "pct_n", "fraction", "n"),
separate_tables = FALSE,
stats.method = c("nonparametric", "parametric", "auto", "none"),
pdf.width = NULL,
pdf.height = NULL,
timestamp = FALSE
)Arguments
- data
A Seurat object or a plain data frame.
- id_column
Character. The column that uniquely identifies a donor or patient (e.g.
"Donor.ID"). Set toNULLto count cells directly without deduplication.- variables
Character vector. Metadata columns to display as column facets on the x-axis (e.g.
c("Braak", "APOE.Genotype", "CERAD.score")).- fill_variable
Character. Metadata column used to color-fill the stacked bars (e.g.
"Dementia.AD"). WhenNULL, bars are shown in a single neutral color with no legend.- row_variable
Character. Metadata column used to create row facets (e.g.
"Sex").NULL= no row splitting.- column_variable
Character or
NULL. Metadata column used to create column facets within each panel. When supplied, one plot is produced per entry invariables(multi-page PDF), and each plot is faceted by this variable.NULL= no column faceting (default).- exclude
Named list of values to exclude before plotting. Each name is a column name and each value is a character vector of levels to drop. Example:
list(Cognitive.Status = "Reference").- percent
Logical. If
TRUE, convert counts to percentages before plotting. Whenfill_variableis set, each bar sums to 100\NULL, bars show the percentage of the panel total. Segment labels automatically switch to"X%"format. DefaultFALSE.- count_unit
One of
"auto"(default),"donors", or"cells"."auto"counts donors whenid_columnis provided and more than one unique donor exists per group; otherwise counts cells.- show_counts
Logical. Annotate each bar segment with its count. Default
TRUE.- min_count_to_label
Integer. Suppress the count label on segments with fewer than this many observations. Default
1.- label_size
Numeric. Text size for count labels (passed to
geom_text). Default3.- alpha
Numeric. Fill transparency for bars (0-1). Default
0.85.- colors
Named character vector of colors for
fill_variablelevels.NULLauto-resolves fromPrepObject(if a Seurat object is passed) orSelectColors().- y_label
Character or
NULL. Override the y-axis label.NULLderives it fromcount_unit.- output_dir
Character or
NULL. Directory to save a PDF.NULLreturns the plot without saving; for a Seurat object it first walks up to theoutput_dirstored byPrepObject(unlessAutoSavePlots = FALSE).- object_name
Character. Prefix for the auto-generated file name. Falls back to the
object_namestored byPrepObject.- file_name
Character or
NULL. Base name (no extension) for the saved PDF.NULL(default) auto-deduces fromobject_name,fill_variable, andvariables.- return_data
Logical. Also return the deduplicated donor-level data frame. Default
FALSE.- return_flextable
Logical. Also return a list of formatted
flextableobjects:$donor_table(wide donor-level data),$crosstab(n per category level perfill_variablegroup), and$numeric_table(mean ± SD per numeric variable perfill_variablegroup, with each donor weighted equally). Requires the flextable package. DefaultFALSE.- stats.method
Character. Statistical test to add as a
p.valuecolumn in the flextable outputs (requiresreturn_flextable = TRUEandfill_variable)."nonparametric"(default)Fisher's exact for categorical, Wilcoxon / Kruskal-Wallis for numeric.
"parametric"Chi-squared for categorical, t-test / ANOVA for numeric.
"auto"Parametric when Shapiro-Wilk passes (numeric) or expected counts >= 5 (categorical); otherwise non-parametric.
"none"No statistics added.
- timestamp
Logical. When
TRUE, append a_YYYYMMDD-HHMMSSstamp to the saved file name so repeated runs are versioned instead of overwriting the previous PDF. DefaultFALSE.
Value
When only the plot is requested (default), a ggplot2 object.
When return_data or return_flextable are TRUE, a
named list with elements $plot, optionally $data, and
optionally $flextable.
Examples
if (FALSE) { # \dontrun{
# Basic usage
PlotMetaSummary(SeuratObj,
id_column = "Donor.ID",
variables = c("Braak", "APOE.Genotype", "CERAD.score"),
fill_variable = "Dementia.AD",
row_variable = "Sex",
exclude = list(Cognitive.Status = "Reference"))
# Also return the donor table and a cross-tab flextable
out <- PlotMetaSummary(SeuratObj,
id_column = "Donor.ID",
variables = c("Braak", "CERAD.score"),
fill_variable = "Dementia.AD",
return_data = TRUE,
return_flextable = TRUE)
out$plot
out$data
out$flextable$crosstab
} # }