
Generate Taxonomic Change Heatmap for Longitudinal Data
Source:R/generate_taxa_change_heatmap_long.R
generate_taxa_change_heatmap_long.RdCreates heatmaps showing taxa abundance changes from baseline across multiple time points. Uses pheatmap with hierarchical clustering and group annotations.
Usage
generate_taxa_change_heatmap_long(
data.obj,
subject.var,
time.var,
t0.level = NULL,
ts.levels = NULL,
group.var = NULL,
strata.var = NULL,
feature.level,
feature.change.func = "relative change",
feature.dat.type = c("count", "proportion", "other"),
features.plot = NULL,
top.k.plot = NULL,
top.k.func = NULL,
prev.filter = 0.01,
abund.filter = 0.01,
base.size = 10,
palette = NULL,
cluster.rows = NULL,
cluster.cols = NULL,
pdf = TRUE,
file.ann = NULL,
pdf.wid = 11,
pdf.hei = 8.5,
...
)Arguments
- data.obj
A MicrobiomeStat data object, which is a list containing at minimum the following components:
feature.tab: A matrix of feature abundances (taxa/genes as rows, samples as columns)meta.dat: A data frame of sample metadata (samples as rows)
Optional components include:
feature.ann: A matrix/data frame of feature annotations (e.g., taxonomy)tree: A phylogenetic tree object (class "phylo")feature.agg.list: Pre-aggregated feature tables by taxonomy
Data objects can be created using converters like
mStat_convert_phyloseq_to_data_objor importers likemStat_import_qiime2_as_data_obj.- subject.var
Character string specifying the column name in meta.dat that uniquely identifies each subject or sample unit. Required for longitudinal and paired designs to track repeated measurements.
- time.var
Character string specifying the column name in meta.dat containing the time variable. Required for longitudinal and paired analyses. Supports character/factor labels (e.g., "baseline", "week4") and numeric values. Some trend/volatility methods require numeric or coercible-to-numeric time values.
- t0.level
Character or numeric value specifying the baseline time point for longitudinal or paired analyses. Should match a value in the time.var column.
- ts.levels
Character vector specifying the follow-up time points for longitudinal or paired analyses. Should match values in the time.var column.
- group.var
Character string specifying the column name in meta.dat containing the grouping variable (e.g., treatment, condition, phenotype). Used for between-group comparisons.
- strata.var
Character string specifying the column name in meta.dat for stratification. When provided, analyses and visualizations will be performed separately within each stratum (e.g., by site, batch, or sex).
- feature.level
Character vector specifying the taxonomic or annotation level(s) for analysis. Should match column names in feature.ann, such as "Phylum", "Family", "Genus", etc. Use "original" to analyze at the original feature level without aggregation.
- feature.change.func
Method for calculating change: "relative change", "log fold change", "absolute change", or a custom function.
- feature.dat.type
Character string specifying the data type of feature.tab. One of:
"count": Raw count data (will be normalized)
"proportion": Relative abundance data (should sum to 1 per sample)
"other": Pre-transformed data (no transformation applied)
- features.plot
Character vector of specific feature IDs to plot.
- top.k.plot
Integer specifying number of top features to plot.
- top.k.func
Function for selecting top features (e.g., "mean", "sd").
- prev.filter
Numeric value between 0 and 1. Features with prevalence (proportion of non-zero samples) below this threshold will be excluded from analysis. Default is usually 0 (no filtering).
- abund.filter
Numeric value. Features with mean abundance below this threshold will be excluded from analysis. Default is usually 0 (no filtering).
- base.size
Numeric value specifying the base font size for plot text elements. Default is typically 16.
- palette
Character vector of colors or a named palette for the plot. If NULL, uses default MicrobiomeStat color scheme. Can be:
A vector of color codes (e.g., c("#E41A1C", "#377EB8"))
A palette name recognized by the plotting function
- cluster.rows
Logical, whether to cluster rows. Default TRUE.
- cluster.cols
Logical, whether to cluster columns. Default FALSE.
Logical. If TRUE, saves the plot(s) to PDF file(s) in the current working directory. Default is TRUE.
- file.ann
Character string for additional annotation to append to output filenames. Useful for distinguishing multiple outputs.
- pdf.wid
Numeric value specifying the width of PDF output in inches. Default is typically 11.
- pdf.hei
Numeric value specifying the height of PDF output in inches. Default is typically 8.5.
- ...
Additional arguments passed to underlying functions.
Examples
if (FALSE) { # \dontrun{
library(pheatmap)
data(ecam.obj)
generate_taxa_change_heatmap_long(
data.obj = ecam.obj,
subject.var = "studyid",
time.var = "month",
t0.level = unique(ecam.obj$meta.dat$month)[1],
ts.levels = unique(ecam.obj$meta.dat$month)[-1],
group.var = "antiexposedall",
strata.var = "diet",
feature.level = c("Family","Class"),
feature.change.func = "log fold change",
feature.dat.type = "proportion",
features.plot = NULL,
top.k.plot = 10,
top.k.func = "sd",
palette = NULL,
prev.filter = 0.01,
abund.filter = 0.01,
pdf = TRUE,
file.ann = NULL
)
generate_taxa_change_heatmap_long(
data.obj = ecam.obj,
subject.var = "studyid",
time.var = "month",
t0.level = unique(ecam.obj$meta.dat$month)[1],
ts.levels = unique(ecam.obj$meta.dat$month)[-1],
group.var = "antiexposedall",
strata.var = "diet",
feature.level = c("Family","Class"),
feature.change.func = "log fold change",
feature.dat.type = "proportion",
features.plot = NULL,
cluster.rows = FALSE,
top.k.plot = 10,
top.k.func = "sd",
palette = NULL,
prev.filter = 0.01,
abund.filter = 0.01,
pdf = TRUE,
file.ann = NULL
)
data(subset_T2D.obj)
generate_taxa_change_heatmap_long(
data.obj = subset_T2D.obj,
subject.var = "subject_id",
time.var = "visit_number",
t0.level = unique(subset_T2D.obj$meta.dat$visit_number)[1],
ts.levels = unique(subset_T2D.obj$meta.dat$visit_number)[-1],
group.var = "subject_gender",
strata.var = "subject_race",
feature.level = c("Phylum"),
feature.change.func = "log fold change",
feature.dat.type = "count",
features.plot = NULL,
top.k.plot = 50,
top.k.func = "mean",
prev.filter = 0.01,
abund.filter = 0.01,
pdf = TRUE,
file.ann = NULL,
pdf.wid = 11,
pdf.hei = 8.5
)
generate_taxa_change_heatmap_long(
data.obj = subset_T2D.obj,
subject.var = "subject_id",
time.var = "visit_number",
t0.level = unique(subset_T2D.obj$meta.dat$visit_number)[1],
ts.levels = unique(subset_T2D.obj$meta.dat$visit_number)[-1],
group.var = "subject_gender",
strata.var = "subject_race",
feature.level = c("Phylum"),
feature.change.func = "log fold change",
feature.dat.type = "count",
features.plot = NULL,
cluster.rows = FALSE,
top.k.plot = 50,
top.k.func = "mean",
prev.filter = 0.01,
abund.filter = 0.01,
pdf = TRUE,
file.ann = NULL,
pdf.wid = 11,
pdf.hei = 8.5
)
} # }