PlotModulePreservation.Rd
Plotting function for Module Preservation statistics
PlotModulePreservation(
seurat_obj,
name,
statistics = "summary",
plot_labels = TRUE,
label_size = 4,
mod_point_size = 4,
wgcna_name = NULL
)
A Seurat object
The name of the module preservation analysis to plot given in ModulePreservation
Which module preservation statistics to plot? Choices are summary, all, or a custom list
logical determining whether to plot the module labels
the size of the module labels
the size of the points in each plot
The name of the hdWGCNA experiment in the seurat_obj@misc slot
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>