Skip to contents

Creates statistical violin plots for one or more features from a Seurat object, using ggstatsplot::ggbetweenstats() for between-group comparisons with built-in statistical testing.

This function requires the ggstatsplot package to be installed. For standard violin plots without statistical testing, use do_ViolinPlot instead.

For a single feature, returns a bare ggplot2 object. For multiple features, returns a patchwork composition (requires the patchwork package).

Usage

do_StatsViolinPlot(
  object,
  features,
  group.by = NULL,
  group.levels = NULL,
  layer = "data",
  assay = NULL,
  type = "nonparametric",
  p.adjust.method = "holm",
  pairwise.display = "significant",
  colors.use = NULL,
  ncol = NULL,
  ...
)

Arguments

object

Seurat object.

features

character. Gene names or numeric metadata column names to plot (required). Can mix both types freely — each gets its own panel. Examples: c("CD3D", "nCount_RNA", "percent.mt").

group.by

character. Metadata column for grouping. If NULL (default), uses active Idents. Must have 2 or more levels for statistical comparison.

group.levels

character. Subset of group levels to include (default: NULL = all levels). Useful for comparing specific clusters or conditions, e.g. group.levels = c("0", "2", "5").

layer

character. Layer to extract expression from (default: "data"). Only applies to gene features.

assay

character. Assay to use (default: NULL = DefaultAssay).

type

character. Type of statistical test to use. One of "nonparametric" (default), "parametric", "robust", or "bayes". Default is "nonparametric" (Kruskal-Wallis + Dunn's) because single-cell expression data is typically non-normally distributed. Passed through to ggstatsplot::ggbetweenstats().

p.adjust.method

character. Method for p-value adjustment (default: "holm"). Passed through to ggstatsplot::ggbetweenstats().

pairwise.display

character. Which pairwise comparisons to display. One of "significant" (default), "non-significant", "all", or "none". Passed through to ggstatsplot::ggbetweenstats().

colors.use

character. Named vector of colors for groups. If NULL, generates colors automatically with generate_badranseq_colors.

ncol

numeric. Number of columns for patchwork layout when plotting multiple features (default: NULL = auto).

...

Additional arguments passed to ggstatsplot::ggbetweenstats().

Value

A ggplot2 object (single feature) or patchwork object (multiple features).

Examples

if (FALSE) { # \dontrun{
# Single gene statistical violin plot
do_StatsViolinPlot(seurat_obj, features = "CD3D",
                   group.by = "seurat_clusters")

# Multiple genes
do_StatsViolinPlot(seurat_obj, features = c("CD3D", "CD8A"),
                   group.by = "seurat_clusters")

# Compare specific clusters only
do_StatsViolinPlot(seurat_obj, features = "CD3D",
                   group.by = "seurat_clusters",
                   group.levels = c("0", "2", "5"))

# Parametric test
do_StatsViolinPlot(seurat_obj, features = "CD3D",
                   group.by = "condition", type = "parametric")
} # }