Skip to contents

Show factor activity across samples as a heatmap, points, distributions or a structure plot, optionally grouped by a metadata column. Samples before long-table expansion. Structure proportions require explicit row_sum normalization and nonnegative scores; zero-sum rows are excluded and recorded. Display normalization never changes inferential activities. order_within_group = "mds1" reorders displayed samples within each group by 1D classical MDS of the displayed row-sum-normalized factor proportions (Euclidean distance); display-only, never an inferential result.

Usage

plot_factor_activity(
  fit,
  metadata = NULL,
  group = NULL,
  factors = NULL,
  style = "heatmap",
  display_normalization = "none",
  max_samples = 2000L,
  seed = 1L,
  order_within_group = c("input", "mds1"),
  group_order = NULL,
  ...
)

Arguments

fit

Native fit or a view from standardize_factors().

metadata

Optional data frame with a sample column matching the activity row names; needed only with group.

group

Optional name of a metadata column to group samples by.

factors

Optional character factor IDs to display. Default all.

style

"heatmap" (default), "point", "distribution" or "structure" (stacked proportions, nonnegative activities only).

display_normalization

"none" (default) or "row_sum" (structure style only).

max_samples

Maximum samples drawn for display (default 2000).

seed

Integer seed for the sample draw (default 1), recorded in the metadata.

order_within_group

"input" (default, current order) or "mds1". mds1 requires style = "structure" and a group column; groups keep their first displayed appearance, singleton and zero-distance groups keep input order, and remaining ties keep original displayed order. The MDS sign is canonical: the first nonzero coordinate in displayed order is positive.

group_order

Optional character vector naming every displayed group once, in the order the structure plot should show them (default first appearance). Requires style = "structure" and a group column.

...

Optional palette and limits.

Value

A ggplot object with included/excluded IDs and display metadata attributes.

Details

Origin: ADAPTED / none. Credits MyStructurePlot / ImmGenT display workflow; use factor_provenance("plot_factor_activity") for audited sources and runtime versions. Input units and basis are retained; display/conditional results do not establish biological replication.

Author

David Zemmour. Full source and dependency credits: https://github.com/mdmanurung/flashier-utils/blob/main/CREDITS.md.

Examples

set.seed(3)
X <- tcrossprod(matrix(rnorm(48), 24, 2), matrix(rnorm(32), 16, 2)) +
  matrix(rnorm(384, sd = 0.2), 24, 16)
dimnames(X) <- list(paste0("s", 1:24), paste0("g", 1:16))
fit <- fit_ebmf(X, "rows",
  max_factors = 2, seed = 4, verbose = 0,
  ebnm_fn = list(ebnm::ebnm_normal, ebnm::ebnm_point_laplace),
  nullcheck = FALSE, backfit = TRUE, feature_scale = "simulated_centered_intensity"
)
view <- standardize_factors(fit)
metadata <- data.frame(
  sample = rownames(X), donor = rownames(X),
  group = rep(c("A", "B"), each = 12)
)
plot_factor_activity(view, metadata = metadata, group = "group", max_samples = 12, seed = 4)