knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
library(SCseqtools)

Create sample data frame

A sample data frame from an experiment containing

set.seed(123)

dat <- data.frame(
  sample = factor(
    rep(c("control", "drug1", "drug2", "combo"), each=250),
    levels = c("control", "drug1", "drug2", "combo")),
  cluster= rep(c("t_cell", "b_cell", "nk_cell", "monocyte", "macrophage"), each=50),
  CD8A = sample(seq(0,1, by=0.2),1000, replace = T ),
  CD4 = sample(seq(0,1, by=0.2),1000, replace = T ),
  IFNG = rnorm(1000, 5, 5), GZMB = rnorm(1000, 5, 5),
  ARG1 = rnorm(1000, 5, 5), NOS2 = rnorm(1000, 5, 5))


dat[dat$sample == "drug1", 5:8 ] <- dat[dat$sample == "drug1", 5:8 ] + 1
dat[dat$sample == "drug2", 5:8 ] <- dat[dat$sample == "drug2", 5:8 ] + 3

dat[dat$sample == "combo", 5:8 ] <- dat[dat$sample == "combo", 5:8 ] + 15

head(dat)  

Example plots

Plot genes for t_cell cluster

gene_grapher(dat, 
             genes_to_plot = c("IFNG", "GZMB"), 
             colors_to_use = c("orange", "gray", "red", "steelblue"),
             clusters_to_plot = "t_cell",
             image_columns = 2, image_rows = 1, 
             add_mean = T, mean_color = "white",
             add_median=F)

Plot genes for CD8a+CD4- cells

gene_grapher(dat, 
             genes_to_plot = c("IFNG", "GZMB"), 
             pos_marker = "CD8A",neg_marker = "CD4",
             colors_to_use = c("orange", "gray", "red", "steelblue"),
             image_columns = 2, image_rows = 1, 
             add_mean = T, mean_color = "white",
             add_median=F)

Plot genes for CD8a+CD4+ cells

gene_grapher(dat, 
             c("IFNG", "GZMB"), 
             pos_marker = c("CD8A", "CD4"),
             colors_to_use = c("orange", "gray", "red", "steelblue"),
             image_columns = 2, image_rows = 1, 
             add_mean = T, mean_color = "white",
             add_median=F)

Plot genes for unsubsetted data

# Use regex matching to find genes in columns

genes <- grep("cd4|arg", colnames(dat), ignore.case = T, value = T)

gene_grapher(dat, 
             genes_to_plot = genes, 
             colors_to_use = c("orange", "gray", "red", "steelblue"),
             image_columns = 2, image_rows = 1, 
             add_mean = T, mean_color = "white",
             add_median=F)

Flexible graphing options

Specify comparisons to show

gene_grapher(dat, 
             genes_to_plot = c("ARG1", "NOS2"), 
             plot_type = "box",
             comparisons = list(c("control", "drug1"), 
                                c("control", "drug2"), 
                                c("control", "combo")),
             colors_to_use = c("orange", "gray", "red", "steelblue"),
             image_columns = 2, image_rows = 1, 
             add_mean = T, mean_color = "white",
             add_median=F)

Violin plots

gene_grapher(dat, 
             genes_to_plot = c("ARG1", "NOS2"), 
             plot_type = "violin",
             colors_to_use = c("orange", "gray", "red", "steelblue"),
             image_columns = 2, image_rows = 1, 
             add_mean = T, mean_color = "white",
             add_median=F)

Bar plots

gene_grapher(dat, 
             genes_to_plot = c("ARG1", "NOS2"), 
             plot_type = "bar",
             colors_to_use = c("orange", "gray", "red", "steelblue"),
             image_columns = 2, image_rows = 1, 
             add_mean = T, mean_color = "white",
             add_median=F)

Message which gene is being plotted

This could be helpful to track progress if a long lists of genes are being plotted. Also, it can help pinpoint where the plotting encounters a problem (e.g. nothing to plot with the current subsetting approach)

gene_grapher(dat, 
             genes_to_plot = c("ARG1", "NOS2"), 
             plot_type = "box",show_progress = T, 
             colors_to_use = c("orange", "gray", "red", "steelblue"),
             image_columns = 2, image_rows = 1, 
             add_mean = T, mean_color = "white",
             add_median=F)

Remove stats

gene_grapher(dat, 
             genes_to_plot = genes, 
             colors_to_use = c("orange", "gray", "red", "steelblue"),
             image_columns = 2, image_rows = 1, 
             add_mean = T, mean_color = "white",
             add_median=F,
             show_stats = F)

Plot gene expression per cluster

gene_grapher(dat, 
             x_variable = "cluster",
             genes_to_plot = genes, 
             colors_to_use = c("orange", "gray", "red", "steelblue", "purple"),
             image_columns = 2, image_rows = 1, 
             add_mean = T, mean_color = "white",
             add_median=F,
             show_stats = F) 

Assign global plot object for further manipulations

suppressPackageStartupMessages(library(ggplot2))

gene_grapher(dat, 
             genes_to_plot = c("ARG1", "NOS2"), 
             plot_type = "violin", assign_global_plotlist = T,
             colors_to_use = c("orange", "gray", "red", "steelblue"),
             image_columns = 2, image_rows = 1, output_plot = F,
             add_mean = T, mean_color = "white",
             add_median=F)


plot_list[[1]] + 
  theme(axis.text.x = element_text(color = "red"),
        axis.text.y = element_text(color="blue")) + 
  labs(title = paste(names(plot_list)[1], "New plot title"))


atakanekiz/SCseqtools documentation built on April 18, 2023, 12:55 a.m.