Overview of applications of the different functions in the jj package.

library(jj)
library(tidyverse)
library(Seurat)
knitr::opts_chunk$set(fig.width=6, fig.height=4) 

Tutorial

In this tutorial, we will use the pbmc_small example object provided together with the Seurat package.

The function j can be used to get a quick overview on any R object.

j(pbmc_small)
j(pbmc_small@meta.data)
j(GetAssayData(pbmc_small))

jj_plot_features {.tabset}

Overview

jj_plot_features is the core function from the jj package and includes many plotting options. We can get a first impression of the dataset by ploting a dimensionality reduction without colouring by any feature:

jj_plot_features(pbmc_small, reduction = 'tsne')

Colour by meta.data

jj_plot_features(pbmc_small, reduction = 'tsne', features = 'letter.idents')

Colour by features

jj_plot_features(pbmc_small, reduction = 'tsne', features = c('CD19', 'CD8A'), cap_top = 'auto')

Use facets

We can specify a column from the (meta.data) data.frame to facet the plot:

jj_plot_features(pbmc_small, reduction = 'tsne', features = 'letter.idents', facet_by = 'groups')

Show background cells

Optionally, points not part of the facet can be shown in grey as background. It is also possible to only show selected facets by specifying facet_subset.

jj_plot_features(pbmc_small, reduction = 'tsne', features = 'letter.idents', facet_by = 'groups', show_background_cells = T, facet_subset = 'g2')

Density

Instead of colouring by a feature, the number of neighbours for each point can be shown

jj_plot_features(pbmc_small, reduction = 'tsne', use_pointdensity = T)

Define custom background cells

The parameter foreground_subset_bool takes a logical vector of the same length as observations in the object that defines, which cells are seen as background. Background cells are excluded for the neigbhour calculation.

jj_plot_features(pbmc_small, reduction = 'tsne', use_pointdensity = T, foreground_subset_bool = pbmc_small$letter.idents == 'A', show_background_cells = T, my_title = 'Letter.ident A neigbour count')

Add labels

At the mean position for each subgroup, we can add a label for an easier identification in the plot

jj_plot_features(pbmc_small, reduction = 'tsne', features = 'RNA_snn_res.1', label_type = 'geom_label', fill_colors = 'white')

Show area capturing density

Instead of colouring by a categorical feature, its density can be plotted as areas allowing the specification of another feature in the plot.

jj_plot_features(pbmc_small, reduction = 'tsne', features = 'nCount_RNA', area_by = 'RNA_snn_res.1', )

Change colours and graphical parameters

Many graphical parameters can be easily adapted.

jj_plot_features(pbmc_small, reduction = 'tsne', features = 'RNA_snn_res.1', custom_colors = c('0' = 'orange', '1' = 'darkblue', '2' = "#FF0011"), pt_size = 5, custom_theme = theme_classic(), my_title = 'Seurat Clusters', xlabel = 'Dim. 1', ylabel = 'Dim. 2', shape = 18, alpha = 0.8)

Change point order and cap values

Instead of shuffeling the points (default), points can be ordered by their value as well. Also, the range for continuous variables can be capped at the top or bottom by specifying a threshold, a quantile, or 'auto' to determine a useful threshold automatically.

jj_plot_features(pbmc_small, reduction = 'tsne', features = 'nCount_RNA', color_scale = 'spectral', pt_size = 5, order_type = 'order', cap_top = 'q90', cap_bottom = 200)

modify gg plot and arrange in a grid

When return_gg_object = T, ggplot objects are returned as a list that can be modified. Multiple plots can be arranged in a grid using jj_arrange_ggplots()

gg_list = jj_plot_features(pbmc_small, reduction = 'tsne', features = c('letter.idents', 'nCount_RNA', 'RNA_snn_res.1'), return_gg_object = T)
gg_list[[2]] = gg_list[[2]] + theme_dark() + scale_colour_gradient2()
jj_arrange_ggplots(gg_list, nplots =3, cols = 1)

Violin / boxplots {.tabset}

jj_plot_categorical_by_group

Calculate the fractions of a categorical variable by another group variable

jj_plot_categorical_by_group(pbmc_small@meta.data, feature_column = 'letter.idents', group_column = 'RNA_snn_res.1')

Or plot absolute counts and optionally add text. The coordinates can be swapped if needed.

jj_plot_categorical_by_group(pbmc_small@meta.data, feature_column = 'letter.idents', group_column = 'RNA_snn_res.1', absolute_numbers = T, add_text = T, flip_coordinates = T)

jj_plot_numeric_by_group

jj_plot_numeric_by_group(pbmc_small@meta.data, feature_column = 'nCount_RNA', group_column = 'RNA_snn_res.1', order = T)

jj_plot_sparse_by_group

Useful to plot sparse features. Can additionally show the fraction of cells expressing the feature as piechart below the groups.

jj_plot_sparse_by_group_seurat(pbmc_small, gene_plot = 'CD3E', group_column = 'RNA_snn_res.1', plot_cell_sample = T)

Heatmap / Dotplot {.tabset}

jj_plot_heatmap

jj_plot_heatmap(pbmc_small, features_use = c('CD8A','KLRG1','CD79A','CD79B','CD3E'), group_vec = pbmc_small$RNA_snn_res.1)

jj_plot_dotplot

jj_plot_dotplot(pbmc_small, features_use = c('CD8A','KLRG1','CD79A','CD79B','CD3E'), group_vec = pbmc_small$RNA_snn_res.1)

Histogram {.tabset}

coloured by threshold

jj_plot_hist(pbmc_small$nCount_RNA, thres = 400)

coloured by categorical feature and split by group

jj_plot_hist(pbmc_small@meta.data, feature = 'nCount_RNA', group = 'RNA_snn_res.1', fill = 'letter.idents', nbins = 30)

coloured by categorical feature and with border colours

jj_plot_hist(pbmc_small@meta.data, feature = 'nCount_RNA', colour = 'letter.idents', fill = 'RNA_snn_res.1', nbins = 30, size = 1.2) + 
  scale_colour_manual(values =  c('A'='black','B'='grey40')) + scale_fill_manual(values = jj_get_jj_colours(pbmc_small$RNA_snn_res.1))

Volcano / Fold-change plot {.tabset}

Use Seurat to find marker genes separating different clusters.

marker_df = FindMarkers(pbmc_small, group.by = 'RNA_snn_res.1', ident.1 = '0', ident.2 = '1', verbose = F)
marker_df$symbol = rownames(marker_df)
print(head(marker_df))

marker_df2 = FindMarkers(pbmc_small, group.by = 'RNA_snn_res.1', ident.1 = '0', ident.2 = '2', verbose = F)
marker_df2$symbol = rownames(marker_df2)

markers_joined = dplyr::inner_join(marker_df, marker_df2, by = 'symbol', suffix = c('.0_vs_1', '.0_vs_2'))
head(markers_joined)

Volcano plot with marker threshold

Specify the fold-change, p-value and gene symbol columns to plot a volcano plot. Optionally, text can be printed for markers above/below a threshold. The labs_range argument defines the axis range that is plotted.

jj_plot_volcano(marker_df, logfc_column = 'avg_log2FC', pval_column = 'p_val', symbol_column = 'symbol', use_text = T, labs_range = c(-7,7), marker_thres = 5)

Custom highlights

Instead of highlighting everything above/below a threshold, specific markers can be highlighted.

jj_plot_volcano(marker_df, logfc_column = 'avg_log2FC', pval_column = 'p_val', symbol_column = 'symbol',
               labs_range = c(-8, 8),  markers_highlight = marker_df$symbol[1:4], pt.size = 1.5, marker_thres = Inf,
               only_highlight = F, col_by_highlight = T)

Fold-change vs fold-change

Compare the fold-changes from two comparisons against each other with jj_plot_fc_fc.

jj_plot_fc_fc(markers_joined, logfc_column1 = 'avg_log2FC.0_vs_1', logfc_column2 = 'avg_log2FC.0_vs_2',symbol_column = 'symbol', labs_range = c(-8,8, -8,8), marker_thres = 5, use_text = T) 


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