Skip to contents

Makes barplots from RunEnrichr output.

Usage

EnrichrBarPlot(
  seurat_obj,
  outdir = "enrichr_plots",
  n_terms = 25,
  plot_size = c(6, 15),
  logscale = FALSE,
  plot_bar_color = NULL,
  plot_text_color = NULL,
  wgcna_name = NULL,
  ...
)

Arguments

seurat_obj

A Seurat object

outdir

directory to place output .pdf files

n_terms

the number of terms to plot in each barplot

plot_size

the size of the output .pdf files (width, height)

logscale

logical controlling whether to plot the enrichment on a log scale

plot_bar_color

The color of the bars in the bar plots. Default option (NULL) makes the bars colored using the module's assigned color.

plot_text_color

The color of the text labels on the bar plots

wgcna_name

The name of the hdWGCNA experiment in the seurat_obj@misc slot

Examples

EnrichrBarPlot
#> function (seurat_obj, outdir = "enrichr_plots", n_terms = 25, 
#>     plot_size = c(6, 15), logscale = FALSE, plot_bar_color = NULL, 
#>     plot_text_color = NULL, wgcna_name = NULL, ...) 
#> {
#>     if (is.null(wgcna_name)) {
#>         wgcna_name <- seurat_obj@misc$active_wgcna
#>     }
#>     modules <- GetModules(seurat_obj, wgcna_name)
#>     mods <- levels(modules$module)
#>     mods <- mods[mods != "grey"]
#>     enrichr_df <- GetEnrichrTable(seurat_obj, wgcna_name)
#>     wrapText <- function(x, len) {
#>         sapply(x, function(y) paste(strwrap(y, len), collapse = "\n"), 
#>             USE.NAMES = FALSE)
#>     }
#>     if (!dir.exists(outdir)) {
#>         dir.create(outdir)
#>     }
#>     for (i in 1:length(mods)) {
#>         cur_mod <- mods[i]
#>         cur_terms <- subset(enrichr_df, module == cur_mod)
#>         print(cur_mod)
#>         cur_color <- modules %>% subset(module == cur_mod) %>% 
#>             .$color %>% unique %>% as.character
#>         if (!is.null(plot_bar_color)) {
#>             cur_color <- plot_bar_color
#>         }
#>         if (nrow(cur_terms) == 0) {
#>             next
#>         }
#>         cur_terms$wrap <- wrapText(cur_terms$Term, 45)
#>         plot_list <- list()
#>         for (cur_db in dbs) {
#>             plot_df <- subset(cur_terms, db == cur_db) %>% top_n(n_terms, 
#>                 wt = Combined.Score)
#>             if (is.null(plot_text_color)) {
#>                 if (cur_color == "black") {
#>                   text_color = "grey"
#>                 }
#>                 else {
#>                   text_color = "black"
#>                 }
#>             }
#>             else {
#>                 text_color <- plot_text_color
#>             }
#>             if (logscale) {
#>                 plot_df$Combined.Score <- log(plot_df$Combined.Score)
#>                 lab <- "Enrichment log(combined score)"
#>                 x <- 0.2
#>             }
#>             else {
#>                 lab <- "Enrichment (combined score)"
#>                 x <- 5
#>             }
#>             plot_list[[cur_db]] <- ggplot(plot_df, aes(x = Combined.Score, 
#>                 y = reorder(wrap, Combined.Score))) + geom_bar(stat = "identity", 
#>                 position = "identity", color = "white", fill = cur_color) + 
#>                 geom_text(aes(label = wrap), x = x, color = text_color, 
#>                   size = 3.5, hjust = "left") + ylab("Term") + 
#>                 xlab(lab) + ggtitle(cur_db) + theme(panel.grid.major = element_blank(), 
#>                 panel.grid.minor = element_blank(), legend.title = element_blank(), 
#>                 axis.ticks.y = element_blank(), axis.text.y = element_blank(), 
#>                 plot.title = element_text(hjust = 0.5))
#>         }
#>         pdf(paste0(outdir, "/", cur_mod, ".pdf"), width = plot_size[1], 
#>             height = plot_size[2])
#>         for (plot in plot_list) {
#>             print(plot)
#>         }
#>         dev.off()
#>     }
#> }
#> <bytecode: 0x7f962fdbb2d0>
#> <environment: namespace:hdWGCNA>