jj_plot_features: plot features

View source: R/jj_plot_features.R

jj_plot_featuresR Documentation

plot features

Description

Extension of the functionalities provided by Seurats FeaturePlot/DimPlot functions Can plot scatterplots of multiple features provided under features (from Seurat assay) or meta_features (from Seurat meta.data or data.frame) Embeddings from Seurat need to be specified under reduction. Alternatively, a data.frame with the embedding coordinates in the first two columns can be passed to reduction and seurat_obj can be set to NULL. Loads ggplot2 and dplyr libraries.

Usage

jj_plot_features(
  obj,
  features = NULL,
  reduction = NULL,
  assay_order = c("meta.data|cellColData", "DefaultAssay", "RNA", "GeneScoreMatrix",
    "PeakMatrix"),
  slot = "data",
  color_scale = c("viridis", "wbr", "gbr", "bry", "seurat", "spectral")[1],
  facet_by = NULL,
  area_by = NULL,
  area_thres = 0.98,
  n_facet_rows = NULL,
  facet_subset = NULL,
  cap_top = NULL,
  cap_bottom = NULL,
  custom_colors = NULL,
  custom_theme = theme_minimal(),
  pt_size = "a",
  return_gg_object = FALSE,
  use_pointdensity = FALSE,
  show_background_cells = FALSE,
  foreground_subset_bool = NULL,
  cont_or_disc = "a",
  order_type = c("shuffle", "order", "none")[1],
  convert_factors = FALSE,
  my_title = NULL,
  label_type = NULL,
  label_subset = NULL,
  fill_colors = NULL,
  use_no_legend = FALSE,
  xlabel = NULL,
  ylabel = NULL,
  shape = 16,
  alpha = 1
)

Arguments

features

If Seurat object is provided, extract features from the specified assay and slot

reduction

either a data.frame with embeddings in column 1+2 and further meta data or a string specifying a dimensionality reduction from the Seurat object

slot

slot to use from the Seurat object, default: data

color_scale

color scale to use for continuous features, one of 'wbr', 'bry', 'seurat', 'viridis'

facet_by

string specifying a meta_feature to facet by

n_facet_rows

number of rows for facetted plots

facet_subset

Only used when facet_by not NULL Only plot the facets for the groups supplied here. Background cells are still shown for the whole dataset

cap_top

upper value threshold passed to jj_cap_vals

cap_bottom

lower value threshold passed to jj_cap_vals

custom_colors

named vector of colours used to colour categorical features

custom_theme

custom theme to be used, default: theme_minimal()

pt_size

size of points, default: 'auto'

return_gg_object

return ggplot object instead of plotting, default: FALSE

use_pointdensity

colour by pointdensity using the ggpointdensity package.

show_background_cells

bool, works if foreground_subset_bool is specified. Also when using facets, include the cells not part of the facet as grey background

foreground_subset_bool

boolean vector with same length as number of cells. If show_background_cells=TRUE, show all cells with FALSE as grey background. Do also exlcude those cells for pointdensity calculation, if use_pointdensity=TRUE. Currently not working with do_label=T

cont_or_disc

string of length 1 or length n features indicating whether the features are continuous 'c' or discrete 'd'. Try to set this manually, when the function fails. Otherwise, set to 'a' to automatically determine c or d for each feature.

my_title

optional title for the ggplot

label_type

one of geom_text, geom_text_repel, geom_label, geom_label_repel

fill_colors

colour to fill boxes or areas, if label_type or area_by is not NULL. If NULL, use colours from the custom_colors argument or grey if this is NULL as well

use_no_legend

omit legend, default: FALSE

xlabel

x axis label

ylabel

y axis label

shape

point shape, default: 16

alpha

alpha value, default: 1

seurat_obj

optional Seurat object

meta_features

if Seurat object is provided, extract features from the meta.data. Otherwise extract features from the data.frame provided in reduction

assay

assay to use from the Seurat object, default: DefaultAssay(seurat_obj)

do_order

bool, order points so that largest values are on top

do_shuffle

bool with default = TRUE, randomly shuffle the plotting order. Supersedes do_order.

Details

jj_plot_scatter is a wrapper function that calls jj_plot_features and allows 2D scatterplots with an optional linear regression line. jj_add_labels can be used to add custom labels to a ggplot

Examples

set.seed(1)
df = df = data.frame(x=c(rnorm(380, 5), rnorm(70,2), rnorm(50, 2.5, 2)), y=c(rnorm(380, 5), rnorm(70,-2), rnorm(50,2.5,2)), mock_gene = sample(0:10, size = 500, replace = T, prob = 11:1), clusterIdent = c(rep('gr1', 380), rep('gr2', 70), rep('gr3', 50)), batch = c(rep('A', 380), rep('B', 120)))
jj_plot_features(obj=df, features=c('mock_gene', 'clusterIdent'))
jj_plot_features(obj=df, facet_by = 'batch', use_pointdensity = T, pt_size=4, custom_theme = theme_bw())
jj_plot_features(obj=df, facet_by = 'batch', use_pointdensity = T, foreground_subset_bool = df$clusterIdent %in% c('gr1','gr2'), show_background_cells=T, pt_size=4, custom_theme = theme_bw())
jj_plot_features(obj=df, features = 'batch', facet_by = 'clusterIdent', show_background_cells=T, pt_size=4)
jj_plot_features(obj=df, features = 'batch', facet_by = 'clusterIdent', facet_subset = c('gr1', 'gr3'), pt_size=4)
jj_plot_features(obj = df, features = 'clusterIdent', facet_by = 'batch', label_type = 'geom_label_repel', label_subset = c('gr1','gr2'), custom_colors=structure(c('red','green', 'darkblue'), names = c('gr1','gr2','gr3')))
jj_plot_features(obj = df, features = 'clusterIdent', label_type = 'geom_label_repel', fill_colors = 'white', return_gg_object = T)[[1]] + labs(colour='new\ncolourtitle')
jj_plot_features(obj = df, features = 'clusterIdent', area_by = 'batch', custom_colors = c(gr1='red', gr2='green', gr3='darkblue'))
#only colour specific subsets
jj_plot_features(obj = df, features = 'clusterIdent', area_by = 'batch', custom_colors = c(gr1='red', gr2='green'), fill_colors = c(A = 'yellow'))
#if both area_by and label_type are specified, fill_colors will overwrite the label colours and set them to grey
jj_plot_features(obj = df, features = 'clusterIdent', area_by = 'batch', label_type = 'geom_label', custom_colors = c(gr3='purple'), fill_colors = c(gr1 = 'yellow', A='orange'))

jj_plot_features(obj= pbmc_small, reduction ='tsne', features=c('MS4A1', 'CD79A'), cap_top='q95', color_scale='wbr', xlabel='tsne_1', ylabel='tsne_2',my_title=c('tnse: MS4A1','tsne: CD79A'))
jj_arrange_ggplots(jj_plot_features(obj= pbmc_small, reduction ='tsne', features=c('MS4A1', 'CD79A', 'CD8A'), cap_top='auto', return_gg_object = TRUE))
jj_plot_features(pbmc_small, reduction ='tsne', features= 'nCount_RNA', cap_top='q95', foreground_subset_bool = pbmc_small$groups == 'g1', show_background_cells = T)
jj_plot_features(pbmc_small, features= 'nCount_RNA', cap_top='q95', area_by = 'letter.idents', area_thres = 0.9)
jj_plot_features(pbmc_small, features= 'CD8A', cap_top='q95', area_by = 'letter.idents', area_thres = 0.9, facet_by = 'groups')

mathosi/jj documentation built on Feb. 25, 2024, 2:29 p.m.