R/ClusterDimplot.R

Defines functions ClusterDimplot

Documented in ClusterDimplot

#' Cluster Dimplot
#'
#' This function plots UMAPs for selected clusters of interest
#'
#' @param seurat_object Seurat object
#' @param group_by seurat_object@meta.data column to group cells by in DimPlots
#' @param clusters array of clusters to subset and plot
#' @param cluster_col seurat_object@meta.data column for which clusters are found in
#' @param nrow number of rows to pass to grid.arrange
#' @param shuffle randomly plot cells in order to prevent hiding certain populations when overplotting
#' @param seed set seed for plotting consistency when shuffle is TRUE
#' @return plot generated by grid arrange
#' @export
ClusterDimplot <- function(seurat_object, group_by = 'seurat_clusters', clusters, cluster_col = 'seurat_clusters', plot_title = NA, pt_size = 0.2, xlim = c(-15,15), ylim = c(-15,15), nrow = 1, shuffle = TRUE, seed = 123){
  
  plots = list()
  plots[['all cells']] <- DimPlot(seurat_object, group.by = group_by, pt.size = pt_size, shuffle = shuffle, seed = seed) + xlim(xlim) + ylim(ylim) + labs(title = 'all cells')

  # set colours to the same as when plotting all cells
  colours <- factor(seurat_object[[cluster_col, drop = T]])
  
  dat <- filter(seurat_object@meta.data, (!!sym(cluster_col)) %in% clusters)

  plots[['cluster_subset']] <- DimPlot(seurat_object, group.by = group_by, cells = rownames(dat), pt.size = pt_size, shuffle = shuffle, seed = seed,
          cols = setNames(ggPlotColours(n = length(levels(colours))), levels(colours))) + xlim(xlim) + ylim(ylim) + labs(title = ifelse(is.na(plot_title), 'cluster subset', plot_title))
  
  do.call("grid.arrange", c(plots, nrow=nrow))
}
alexthiery/scHelper documentation built on Aug. 26, 2023, 3:42 p.m.