Plotting function for Module Preservation statistics

PlotModuleTraitCorrelation(
  seurat_obj,
  high_color = "red",
  mid_color = "grey90",
  low_color = "blue",
  label = NULL,
  label_symbol = "stars",
  plot_max = NULL,
  text_size = 2,
  text_color = "black",
  text_digits = 3,
  combine = TRUE,
  wgcna_name = NULL
)

Arguments

seurat_obj

A Seurat object

high_color

color for positive correlation

mid_color

color for zero correlation

low_color

color for negative correlation

label

logical determining whether to add p-val label in each cell of the heatmap

label_symbol

show the labels as 'stars' or as 'numeric'

plot_max

maximum value of correlation to show on the colorbar

text_size

size of the labels

text_color

color of the text labels

text_digits

how many digits to show in the text labels

combine

logical determining whether to plot as one combined plot (TRUE) or to return individual plots as a list (FALSE)

Examples

PlotModulePreservation
#> function (seurat_obj, name, statistics = "summary", plot_labels = TRUE, 
#>     label_size = 4, mod_point_size = 4, wgcna_name = NULL) 
#> {
#>     if (is.null(wgcna_name)) {
#>         wgcna_name <- seurat_obj@misc$active_wgcna
#>     }
#>     mod_pres <- GetModulePreservation(seurat_obj, name, wgcna_name)
#>     obs_df <- mod_pres$obs
#>     Z_df <- mod_pres$Z
#>     modules <- GetModules(seurat_obj, wgcna_name)
#>     module_colors <- modules %>% dplyr::select(c(module, color)) %>% 
#>         distinct
#>     mods <- rownames(Z_df)
#>     mod_colors <- module_colors$color[match(mods, module_colors$module)]
#>     mod_colors = ifelse(is.na(mod_colors), "gold", mod_colors)
#>     if (statistics == "summary") {
#>         stat_list <- c("Zsummary.qual", "Zsummary.pres")
#>     }
#>     else if (statistics == "rank") {
#>         stat_list <- colnames(obs_df[, -1])[grepl("Rank", colnames(obs_df[, 
#>             -1]))]
#>     }
#>     else if (statistics == "all") {
#>         stat_list <- c(colnames(obs_df[, -1])[grepl("Rank", colnames(obs_df[, 
#>             -1]))], colnames(Z_df[, -1]))
#>     }
#>     else {
#>         stat_list <- statistics
#>     }
#>     stat_list <- stat_list[stat_list != "moduleSize"]
#>     plot_list <- list()
#>     for (statistic in stat_list) {
#>         print(statistic)
#>         if (statistic %in% colnames(obs_df)) {
#>             values <- obs_df[, statistic]
#>         }
#>         else if (statistic %in% colnames(Z_df)) {
#>             values <- Z_df[, statistic]
#>         }
#>         else {
#>             stop("Invalid name for statistic.")
#>         }
#>         plot_df <- data.frame(module = mods, color = mod_colors, 
#>             value = values, size = Z_df$moduleSize)
#>         plot_df <- plot_df %>% subset(!(module %in% c("grey", 
#>             "gold")))
#>         if (grepl("Rank", statistic)) {
#>             cur_p <- plot_df %>% ggplot(aes(x = size, y = value, 
#>                 fill = module, color = module)) + geom_point(size = mod_point_size, 
#>                 pch = 21, color = "black") + scale_y_reverse()
#>         }
#>         else {
#>             cur_p <- plot_df %>% ggplot(aes(x = size, y = value, 
#>                 fill = module, color = module)) + geom_rect(data = plot_df[1, 
#>                 ], aes(xmin = 0, xmax = Inf, ymin = -Inf, ymax = 2), 
#>                 fill = "grey75", alpha = 0.8, color = NA) + geom_rect(data = plot_df[1, 
#>                 ], aes(xmin = 0, xmax = Inf, ymin = 2, ymax = 10), 
#>                 fill = "grey92", alpha = 0.8, color = NA) + geom_point(size = mod_point_size, 
#>                 pch = 21, color = "black")
#>         }
#>         cur_p <- cur_p + scale_fill_manual(values = plot_df$color) + 
#>             scale_color_manual(values = plot_df$color) + scale_x_continuous(trans = "log10") + 
#>             ylab(statistic) + xlab("Module Size") + ggtitle(statistic) + 
#>             NoLegend() + theme(plot.title = element_text(hjust = 0.5))
#>         if (plot_labels) {
#>             cur_p <- cur_p + ggrepel::geom_text_repel(label = plot_df$module, 
#>                 size = label_size, max.overlaps = Inf, color = "black")
#>         }
#>         plot_list[[statistic]] <- cur_p
#>     }
#>     if (length(plot_list) == 1) {
#>         return(plot_list[[1]])
#>     }
#>     plot_list
#> }
#> <bytecode: 0x7f77f0389fe8>
#> <environment: namespace:hdWGCNA>