Plot a reduced dimensionality embedding for a Seurat object, colouring by cluster or arbitrary variables.

plot_dr(seurat, reduction = "tsne", colour_by = NULL, colours = NULL,
  colour_by_type = "discrete", label = TRUE,
  point_size = ifelse(length(seurat@cell.names) > 300, 0.6, 1.3),
  alpha = 0.8, legend = ifelse((is.null(colour_by)) && (label), FALSE,
  TRUE), label_repel = TRUE, label_size = 4, highlight_cells = NULL,
  show_all_cells = TRUE, order_by = NULL, clusters_to_label = NULL,
  hide_ticks = TRUE, title = NULL, label_short = FALSE,
  na_colour = "gray80", limits = NULL, constrain_scale = TRUE,
  hide_axes = FALSE, dim1 = 1, dim2 = 2, border_colour = NULL,
  border_size = NULL)

tsne(seurat, ...)

pca(seurat, ...)

umap(seurat, ...)

phate(seurat, ...)

Arguments

seurat

Seurat object, where Seurat::RunTSNE() has been applied

reduction

String, specifying a lot of seurat@dr, which indicates which embedding to plot. Default: "tsne". (Can also take "umap" or "pca").

colour_by

(Optional) String, specifying the column in seurat@meta.data by which to colour cells. Default: NULL, colour cells by cluster (in seurat@ident).

colours

(Optional) Character vector of colours for points. If colour_by is NULL, cells will be coloured by cluster; this should be a named character vector of colours for points. Names should correspond to cluster names (e.g. levels(seurat@ident)). If specifying colour_by, and the variable is discrete, this should be a character vector, with either names or order corresponding to categorical values in the column of seurat@meta.data specified. Otherwise, if the variable is continuous, pass the gradient to use, or a few colours (from low to high) from which a gradient should be created, and specify colour_by_type = "continuous". The default is to use ggplot2 colours.

colour_by_type

(Optional) String, one of "discrete" or "continuous". If specifying colour_by and providing colours to the colours argument, specify whether the colour_by variable is discrete or continuous. Default: discrete. The function is pretty good at setting the palette correctly on its own, but try modifying this in case of errors.

label

Logical, whether to plot cluster labels. Default: TRUE

point_size

Numeric, size of points in scatter plot. Default: 0.6 for datasets with < 300 cells, and 1.3 for datasets with otherwise.

alpha

Numeric, fixed alpha value for points: Default: 0.8

legend

Logical, whether to plot legend. Default: FALSE if colour_by is NULL and label is TRUE, true otherwise.

label_repel

Logical, if label is TRUE, whether to plot cluster labels repelled from the center, on a slightly transparent white background and with an arrow pointing to the cluster center. If FALSE, simply plot the cluster label at the cluster center. Default: TRUE.

label_size

Numeric, controls the size of text labels. Default: 4.

highlight_cells

Character vector of cell names if only a subset of cells should be coloured in the plot (these should correspond to seurat@cell.names). Default: Plot all cells. See the argument clusters_to_label for only labelling certain clusters. See the constrain_scale argument for controlling the scales of the plot.

show_all_cells

Logical. When passing cells to highlight_cells, if TRUE, plot the remaining cells in na_colour, if FALSE, only plot the highlighted cells. Default: TRUE.

order_by

String, corresponding to a column in seurat@meta.data, specifying a variable to control the order in which cells are plot. (Thus, you can manually specify the order, add it as a new column in seurat@meta.data, and pass that). If numeric, cells with high values are plot on top. If not, the column must be a factor, and cells will be ordered according to the levels, with cells in the first level plot on top. Default: if a numeric column is specified to colour_by, sort by that variable, otherwise, use the ordering of the cells in the Seurat object.

clusters_to_label

(Optional.) If label is TRUE, clusters for which labels should be plot (if only a subset of clusters should be labelled). Default: NULL (Label all clusters).

hide_ticks

Logical, whether to hide axis ticks, i.e. both the text and the small lines indicating the breaks along the x- and y-axes. Default: FALSE

title

(Optional) String specifying title.

label_short

(Optional/Experimental!!) Logical, if TRUE, assumes cluster names (at seurat@ident) consist of a prefix and a suffix separated by a non-alpha numeric character ("[^[:alnum:]]+"), and tries to separate these names and only plot the prefix, for shorter labels and a cleaner plot. Default: FALSE.

na_colour

String, specifying the colour (built-in or hex code) to use to plot points which have an NA value, for example in the variable specified in colour_by. Default: light gray ("gray80), change to "white" to purposely hide those cells. If you do not want to plot certain cells at all, pass names of cells to plot to the cells argument.

limits

Numeric vector of length two providing the lower and upper limits of the colour scale, if colouring by a continuous variable. Default: min and max of the values the variable takes on in the data.

constrain_scale

Logical, if plotting a subset of cells, whether to use the limits of the tSNE embedding computed on the whole dataset (useful for constraining scales across plots while only plotting specific cells). Default: TRUE

hide_axes

Logical, whether to hide axis labels. Default: TRUE

dim1

Numeric, dimension of embedding to use for x-axis. Default = 1.

dim2

Numeric, dimension of embedding to use for y-axis. Default = 2.

Value

A ggplot2 object

Functions

  • tsne: Plot a tSNE embedding

  • pca: Plot a PCA embedding

  • umap: Plot a UMAP embedding

  • phate: Plot a PHATE embedding

Examples

tsne(pbmc)
# Demonstrate label_short: # Set cluster IDs to be too long pbmc2 <- pbmc levels(pbmc2@ident) <- c("1-Cell type A", "2-Cell type B", "3-Cell type C", "4-Cell type D") tsne(pbmc2)
# Plot the prefixes only tsne(pbmc2, label_short = TRUE)
# Only colour cells in cluster 3 plot_dr(pbmc, highlight_cells = whichCells(pbmc, clusters = 3))
# Plot the # of genes for only cells in cluster 3, but display all cells on the plot tsne(pbmc, highlight_cells = whichCells(pbmc, clusters = 3), colour_by = "nGene", show_all_cells = FALSE, colours = viridis::viridis(100), colour_by_type = "continuous")