plotDimRed: plotDimRed

View source: R/plotDimRed.R

plotDimRedR Documentation

plotDimRed

Description

Plotting functions for spatial transcriptomics data.

Usage

plotDimRed(
  spe,
  plot_type = c("UMAP", "PCA"),
  annotate = NULL,
  feature_names = NULL,
  assay_name = "counts",
  update_dimnames = TRUE,
  pal = NULL,
  point_size = 0.3,
  legend_point_size = 3,
  text_by = NULL,
  text_by_size = 5,
  text_by_color = "black"
)

Arguments

spe

Input data, assumed to be a SpatialExperiment or SingleCellExperiment object.

plot_type

Type of reduced dimension plot. Possible options are "UMAP", "PCA", or any other set of reduced dimensions stored in the input object. Default = "UMAP".

annotate

Variable to show as annotations. This may be discrete or continuous. For a discrete variable (e.g. cluster labels), this should be the name of a column in colData containing a character vector or factor. For a continuous variable (e.g. a gene name), this should be an entry in feature_names. Default = NULL.

feature_names

Name of column in rowData containing names of continuous features to plot (e.g. gene names). For example, set to feature_names = "gene_name" if gene names are stored in a column named "gene_name". This argument is used if annotate is a continuous variable. Default = NULL, in which case the row names of the input object will be used.

assay_name

Name of assay in input object containing values to plot for a continuous variable. Default = "counts".

update_dimnames

Whether to update column names of reducedDims to default values for plotting. Default = TRUE.

pal

Color palette for annotations. Options for discrete values are "libd_layer_colors", "Okabe-Ito", or any vector of color names or hex values. For continuous values, provide a vector of length 2 for the low and high range, e.g. c("gray90", "navy").

point_size

Point size. Default = 0.3.

legend_point_size

Legend point size for discrete annotations. Default = 3.

text_by

Column name of annotation labels to display over each cluster of points. This will usually be the same as annotate. Alternatively, another column may be used (e.g. with more readable classes or shorter strings). Only used for discrete annotate. Default = NULL.

text_by_size

Text size for annotation labels over each cluster. Default = 5.

text_by_color

Color name or hex code for annotation labels. Default = "black".

Details

Function to create reduced dimension plot (e.g. PCA or UMAP) with additional optional annotations such as cluster labels, expression of a gene, or quality control metrics.

Value

Returns a ggplot object, which may be further modified using ggplot functions.

Author(s)

Lukas M. Weber and Yixing E. Dong

Examples

library(STexampleData)
spe <- Visium_humanDLPFC()

# select spots over tissue
spe <- spe[, colData(spe)$in_tissue == 1]

# use small subset of data for this example
n <- 200
set.seed(123)
spe <- spe[, sample(seq_len(ncol(spe)), n)]

# calculate logcounts
library(scran)
spe <- logNormCounts(spe)

# identify top highly variable genes (HVGs)
is_mito <- grepl("(^MT-)|(^mt-)", rowData(spe)$gene_name)
spe <- spe[!is_mito, ]
dec <- modelGeneVar(spe)
top_hvgs <- getTopHVGs(dec, prop = 0.1)

# run dimensionality reduction
library(scater)
set.seed(123)
spe <- runPCA(spe, subset_row = top_hvgs)
set.seed(123)
spe <- runUMAP(spe, dimred = "PCA")
colnames(reducedDim(spe, "UMAP")) <- paste0("UMAP", 1:2)

# generate plot
plotDimRed(spe, plot_type = "UMAP", annotate = "ground_truth")


lmweber/ggspavis documentation built on April 4, 2024, 3:21 a.m.