R/nice_spatial_feature_plot.R

Defines functions nice_spatial_feature_plot

Documented in nice_spatial_feature_plot

#' @title nice_spatial_feature_plot
#' @description nicer looking version of the Seurat function SpatialFeaturePlot.
#' @param seurat_obj Seurat object.
#' @param features genes to plot.
#' @param im_alpha set to 1 to plot the tissue image, 0 otherwise.
#' @param pt.size size of plotted points.
#' @param stroke linewidth to outline plotted points in black (default = no outline).
#' @param new_cmap if T, change the colourmap from Seurat default.
#' @param cmap colourmap to use if new_cmap = T.
#' @param n_col if plotting multiple genes, number of columns in final plot_grid object.
#' @param diverging_cmap if T, will fill points with diverging colours between blue and red. otherwise, will use a linear colourmap.
#' @param alpha if T, will fill points such that lower expression values are more transparent.
#' @param cols optional: gradient vector of colours to use for plotting.
#' @param crop Crop tissue array to focus on region with spots?
#' @param min.cutoff Minimum value to bin expression data within
#' @param max.cutoff Max value to bin expression data within
#' @export
#' @return SpatialFeaturePlot.

nice_spatial_feature_plot <- function(seurat_obj, features, im_alpha = 0, pt_size = 1.3, 
                                        stroke = 0, new_cmap = T, cmap = "inferno", n_col = 3, 
                                        diverging_cmap = F, alpha = T, cols = NULL, crop = T, min.cutoff = 0, max.cutoff = NA) 
{
  
  require(Seurat)
  require(pals)
  
  if (length(features) == 1 & is.null(n_col)) {
    n_col <- 1
  }
  if (alpha) {
    alpha <- c(0.1, 1)
  }
  else {
    alpha <- c(1, 1)
  }
  if (new_cmap) {
    if (!is.null(cols)) {
      cols <- cols
    }
    else {
      if (diverging_cmap) {
        cols <- pals::brewer.rdbu(n = 100) %>% rev()
      }
      else {
        cols <- pals::magma(n = 100)
      }
    }
    plots <- Seurat::SpatialPlot(seurat_obj, 
                                 features = features, 
                                 alpha = alpha, 
                                 image.alpha = im_alpha, 
                                 stroke = stroke, 
                                 pt.size.factor = pt_size, 
                                 combine = F, 
                                 crop = crop,
                                 min.cutoff = min.cutoff,
                                 max.cutoff = max.cutoff)
    plots <- lapply(plots, function(x) x + scale_fill_gradientn(colours = cols) + 
                      theme(legend.title = element_text(face = "bold", 
                                                        size = 16)))
  }
  else if (new_cmap == F) {
    plots <- Seurat::SpatialPlot(seurat_obj, 
                                 features = features, 
                                 alpha = alpha, 
                                 image.alpha = im_alpha, 
                                 stroke = stroke, 
                                 pt.size.factor = pt_size, 
                                 combine = F, 
                                 crop = crop,
                                 min.cutoff = min.cutoff,
                                 max.cutoff = max.cutoff)
    plots <- lapply(plots, function(x) x + theme(legend.title = element_text(face = "bold", 
                                                                             size = 16)))
  }
  plots <- plot_grid(plotlist = plots, ncol = n_col)
  return(plots)
}
mvhunter1/mvhfunctions documentation built on May 31, 2024, 3:36 p.m.