plot_tsne: t-SNE Plot

View source: R/plot_tsne.R

plot_tsneR Documentation

t-SNE Plot

Description

This function plots a low-dimensional projection of an omic data matrix using t-distributed stochastic neighbor embedding.

Usage

plot_tsne(
  dat,
  group = NULL,
  covar = NULL,
  dist = "euclidean",
  p = 2L,
  top = NULL,
  filter_method = "pairwise",
  center = FALSE,
  dims = c(1L, 2L),
  perplexity = ncol(dat)/4L,
  theta = 0.1,
  max_iter = 1000L,
  label = FALSE,
  pal_group = "npg",
  pal_covar = "Blues",
  size = NULL,
  alpha = NULL,
  title = "t-SNE",
  legend = "right",
  hover = FALSE,
  D3 = FALSE,
  ...
)

Arguments

dat

Omic data matrix or matrix-like object with rows corresponding to probes and columns to samples. It is strongly recommended that data be filtered and normalized prior to plotting. Raw counts stored in DGEList or DESeqDataSet objects are automatically extracted and transformed to the log2-CPM scale, with a warning. Alternatively, an object of class dist which can be directly input to the t-SNE algorithm.

group

Optional character or factor vector of length equal to sample size, or up to two such vectors organized into a list or data frame. Supply legend title(s) by passing a named list or data frame.

covar

Optional continuous covariate. If non-NULL, then plot can render at most one group variable. Supply legend title by passing a named list or data frame.

dist

Distance measure to be used. Supports all methods available in dist, Rfast::Dist, and vegdist, as well as those implemented in the bioDist package. See Details.

p

Power of the Minkowski distance.

top

Optional number (if > 1) or proportion (if < 1) of top probes to be used for t-SNE.

filter_method

String specifying whether to apply a "pairwise" or "common" filter if top is non-NULL.

center

Center each probe prior to computing distances?

dims

Vector specifying which dimensions to plot. Must be of length two unless D3 = TRUE.

perplexity

How many nearest neighbors should the algorithm consider when building projections?

theta

Speed/accuracy tradeoff of the Barnes-Hut algorithm. See Details.

max_iter

Maximum number of iterations over which to minimize the loss function.

label

Label data points by sample name? Defaults to FALSE unless group and covar are both NULL. If TRUE, then plot can render at most one phenotypic feature.

pal_group

String specifying the color palette to use if group is non-NULL, or a vector of such strings with length equal to the number of vectors passed to group. Options include "ggplot", all qualitative color schemes available in RColorBrewer, and the complete collection of ggsci palettes. Alternatively, a character vector of colors with length equal to the cumulative number of levels in group.

pal_covar

String specifying the color palette to use if covar is non-NULL, or a vector of such strings with length equal to the number of vectors passed to covar. Options include the complete collection of viridis palettes, as well as all sequential color schemes available in RColorBrewer. Alternatively, a character vector of colors representing a smooth gradient, or a list of such vectors with length equal to the number of continuous variables to visualize.

size

Point size.

alpha

Point transparency.

title

Optional plot title.

legend

Legend position. Must be one of "bottom", "left", "top", "right", "bottomright", "bottomleft", "topleft", or "topright".

hover

Show sample name by hovering mouse over data point? If TRUE, the plot is rendered in HTML and will either open in your browser's graphic display or appear in the RStudio viewer.

D3

Render plot in three dimensions?

...

Additional arguments to be passed to Rtsne.

Details

t-SNE is a popular machine learning method for visualizing high-dimensional datasets. It is designed to preserve local structure and aids in revealing unsupervised clusters.

plot_tsne relies on a C++ implementation of the Barnes-Hut algorithm, which vastly accelerates the original t-SNE projection method. An exact t-SNE plot may be rendered by setting theta = 0. Briefly, the algorithm computes samplewise similarities based on distances in the original p-dimensional space (where p = the number of probes); generates a low-dimensional embedding of the samples based on the user-defined perplexity parameter; and iteratively minimizes the Kullback-Leibler divergence between these two distributions using an efficient tree search. See Rtsne for more details. A thorough introduction to and explication of the original t-SNE method and the Barnes-Hut approximation may be found in the references below.

The Rtsne function can operate directly on a distance matrix. Available distance measures include: "euclidean", "maximum", "manhattan", "canberra", "minkowski", "cosine", "pearson", "kendall", "spearman", "bray", "kulczynski", "jaccard", "gower", "altGower", "morisita", "horn", "mountford", "raup", "binomial", "chao", "cao", "mahalanobis", "MI", or "KLD". Some distance measures are unsuitable for certain types of data. See dist_mat for more details on these methods and links to documentation on each.

The top argument optionally filters data using either probewise variance (if filter_method = "common") or the leading fold change method of Smyth et al. (if filter_method = "pairwise"). See plot_mds for more details.

References

van der Maaten, L.J.P. (2014). Accelerating t-SNE using Tree-Based Algorithms. Journal of Machine Learning Research, 15: 3221-3245.

van der Maaten, L.J.P. & Hinton, G.E. (2008). Visualizing High-Dimensional Data Using t-SNE. Journal of Machine Learning Research, 9: 2579-2605.

See Also

Rtsne, plot_pca, plot_mds

Examples

mat <- matrix(rnorm(1000 * 5), nrow = 1000, ncol = 5)
plot_tsne(mat)

library(DESeq2)
dds <- makeExampleDESeqDataSet()
dds <- rlog(dds)
plot_tsne(dds, group = colData(dds)$condition)


dswatson/bioplotr documentation built on March 3, 2023, 9:43 p.m.