| pathway_pca | R Documentation |
This function performs PCA analysis on pathway abundance data and creates an informative visualization that includes a scatter plot of the first two principal components (PC1 vs PC2) with density plots for both PCs. The plot helps to visualize the clustering patterns and distribution of samples across different groups.
pathway_pca(abundance, metadata, group, colors = NULL, show_marginal = TRUE)
abundance |
A numeric matrix or data frame containing pathway abundance data.
Rows represent pathways, columns represent samples.
Data frames may also provide a leading non-numeric feature ID column
(for example |
metadata |
A data frame containing sample information. Must include a column for grouping samples (specified by the 'group' parameter). Sample identifiers are auto-detected from columns named sample_name, Sample_ID, SampleID, etc., or from rownames. |
group |
A character string specifying the column name in metadata that contains group information for samples (e.g., "treatment", "condition", "group"). Every sample retained after abundance/metadata alignment must have a non-missing, non-empty group label, and at least two groups must remain. |
colors |
Optional. A character vector of colors for different groups. Length must match the number of unique groups. If NULL, default colors will be used. |
show_marginal |
Logical. Whether to show marginal density plots for PC1 and PC2. Default is TRUE. Set to FALSE to show only the PCA scatter plot. |
PCA centers and scales each feature across samples before decomposing the transposed input matrix. It does not perform library-size or compositional normalization. Apply any study-specific preprocessing before calling the function. Visual separation is descriptive, not a hypothesis test or evidence of causation.
The function automatically aligns samples between abundance data and metadata,
supporting various sample identifier formats. Pathways with zero variance
across samples are filtered before PCA because they are variables in
prcomp(t(abundance)) and cannot be scaled. Sample profiles are kept
as observations, even if a sample has zero variance across pathways. PCA
confidence ellipses are drawn only for groups with at least four samples;
smaller groups remain in the scatter plot but are skipped for ellipse
estimation. Marginal densities require at least two samples per group;
singleton groups are omitted from those panels, and the scatter plot is
returned alone when no group has enough observations for density estimation.
A ggplot object showing:
Center: PCA scatter plot with confidence ellipses (95
Top: Density plot for PC1
Right: Density plot for PC2
# Create example abundance data
abundance_data <- matrix(rexp(30), nrow = 3, ncol = 10)
colnames(abundance_data) <- paste0("Sample", 1:10)
rownames(abundance_data) <- c("PathwayA", "PathwayB", "PathwayC")
# Create example metadata
metadata <- data.frame(
sample_name = paste0("Sample", 1:10),
group = factor(rep(c("Control", "Treatment"), each = 5))
)
# Basic PCA plot with default colors
pca_plot <- pathway_pca(abundance_data, metadata, "group")
# PCA plot with custom colors
pca_plot <- pathway_pca(
abundance_data,
metadata,
"group",
colors = c("blue", "red") # One color per group
)
# PCA plot without marginal density plots
pca_plot <- pathway_pca(
abundance_data,
metadata,
"group",
show_marginal = FALSE
)
# Example with real data
data("metacyc_abundance") # Load example pathway abundance data
data("metadata") # Load example metadata
# Generate PCA plot
# Prepare abundance data
abundance_data <- as.data.frame(metacyc_abundance)
rownames(abundance_data) <- abundance_data$pathway
abundance_data <- abundance_data[, -which(names(abundance_data) == "pathway")]
# Create PCA plot
pathway_pca(
abundance_data,
metadata,
"Environment",
colors = c("green", "purple")
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.