Plotting function to show genes by kME value in each module

PlotKMEs(
  seurat_obj,
  n_hubs = 10,
  text_size = 2,
  ncol = 5,
  plot_widths = c(3, 2),
  wgcna_name = NULL
)

Arguments

seurat_obj

A Seurat object

n_hubs

number of hub genes to display

text_size

controls the size of the hub gene text

ncol

number of columns to display individual plots

plot_widths

the relative width between the kME rank plot and the hub gene text

wgcna_name

the name of the WGCNA experiment in the seurat object

Examples

PlotKMEs
#> function (seurat_obj, n_hubs = 10, text_size = 2, ncol = 5, plot_widths = c(3, 
#>     2), wgcna_name = NULL) 
#> {
#>     if (is.null(wgcna_name)) {
#>         wgcna_name <- seurat_obj@misc$active_wgcna
#>     }
#>     modules <- GetModules(seurat_obj, wgcna_name) %>% subset(module != 
#>         "grey")
#>     mods <- levels(modules$module)
#>     mods <- mods[mods != "grey"]
#>     mod_colors <- modules %>% subset(module %in% mods) %>% select(c(module, 
#>         color)) %>% distinct
#>     hub_df <- do.call(rbind, lapply(mods, function(cur_mod) {
#>         print(cur_mod)
#>         cur <- subset(modules, module == cur_mod)
#>         cur <- cur[, c("gene_name", "module", paste0("kME_", 
#>             cur_mod))]
#>         names(cur)[3] <- "kME"
#>         cur <- dplyr::arrange(cur, kME)
#>         top_genes <- cur %>% dplyr::top_n(n_hubs, wt = kME) %>% 
#>             .$gene_name
#>         cur$lab <- ifelse(cur$gene_name %in% top_genes, cur$gene_name, 
#>             "")
#>         cur
#>     }))
#>     head(hub_df)
#>     plot_list <- lapply(mods, function(x) {
#>         print(x)
#>         cur_color <- subset(mod_colors, module == x) %>% .$color
#>         cur_df <- subset(hub_df, module == x)
#>         top_genes <- cur_df %>% dplyr::top_n(n_hubs, wt = kME) %>% 
#>             .$gene_name
#>         p <- cur_df %>% ggplot(aes(x = reorder(gene_name, kME), 
#>             y = kME)) + geom_bar(stat = "identity", width = 1, 
#>             color = cur_color, fill = cur_color) + ggtitle(x) + 
#>             theme(axis.ticks.x = element_blank(), axis.text.x = element_blank(), 
#>                 plot.title = element_text(hjust = 0.5), axis.title.x = element_blank(), 
#>                 axis.line.x = element_blank())
#>         p_anno <- ggplot() + annotate("label", x = 0, y = 0, 
#>             label = paste0(top_genes, collapse = "\n"), size = text_size, 
#>             fontface = "italic", label.size = 0) + theme_void()
#>         patch <- p + p_anno + plot_layout(widths = plot_widths)
#>         patch
#>     })
#>     wrap_plots(plot_list, ncol = ncol)
#> }
#> <bytecode: 0x7f77d5e73bf8>
#> <environment: namespace:hdWGCNA>