#' Summary plot
#'
#' Generate a plot summarising the cell type-phenotype enrichment
#' results generated by \link[MSTExplorer]{gen_results}.
#' @param count_var Variable to get counts for per \code{group_var}.
#' @param group_var Variable to group counts by.
#' @param keywords Keywords supplied to search for phenotypes.
#' Will be used to generate the plot title.
#' @inheritParams ggnetwork_plot_full
#' @inheritParams HPOExplorer::filter_descendants
#' @inheritParams HPOExplorer::make_network_plot
#' @inheritParams ggplot2::scale_fill_viridis_c
#' @inheritParams KGExplorer::filter_dt
#' @returns ggplot or plotly object
#'
#' @export
#' @import ggplot2
#' @importFrom plotly ggplotly
#' @importFrom scales pretty_breaks
#' @examples
#' keep_descendants <- "Neurodevelopmental delay"
#' plt_pheno_count <- plot_bar_summary(count_var = "hpo_name",
#' group_var = "CellType",
#' keep_descendants = keep_descendants)
#' plt_cell_count <- plot_bar_summary(count_var = "CellType",
#' group_var = "hpo_name",
#' keep_descendants = keep_descendants)
plot_bar_summary <- function(results = load_example_results(),
count_var = "hpo_name",
group_var = "CellType",
keywords = NULL,
q_threshold = 0.0005,
effect_threshold = 1,
filters = NULL,
keep_descendants = NULL,
hpo = HPOExplorer::get_hpo(),
option = "magma",
interactive = TRUE,
show_plot=TRUE,
verbose = TRUE){
requireNamespace("ggplot2")
#### Subset results ####
phenos <- subset_phenos(keep_descendants = keep_descendants,
results = results,
hpo = hpo,
filters = filters,
q_threshold = q_threshold,
effect_threshold = effect_threshold,
verbose = verbose)
phenos <- HPOExplorer::add_hpo_id(phenos = phenos)
phenos <- HPOExplorer::add_hpo_name(phenos = phenos)
#### Aggregate counts ####
counts_df <- agg_results(phenos = phenos,
count_var = count_var,
group_var = group_var)
#### Make subtitle ####
## plotly doesn't inherit subtitle arg from ggplot,
## so need to append it to the title.
subtitle <- paste0(
if(!is.null(keywords)){
paste0("keywords: ",shQuote(paste(keywords,collapse = ", ")),";")
},
" ",formatC(length(unique(phenos[[count_var]])),big.mark = ","),
" ",tolower(count_var),"(s)"
)
n_count_var <- paste("n",paste0(tolower(count_var),"s"),sep="_")
#### Sort celltypes by counts ####
data.table::setorderv(counts_df, n_count_var, -1L)
counts_df[[group_var]] <- factor(counts_df[[group_var]],
levels = unique(counts_df[[group_var]]),
ordered = TRUE)
#### Make plot ####
plt <- ggplot2::ggplot(counts_df,
ggplot2::aes(x = !!ggplot2::sym(group_var),
y = !!ggplot2::sym(n_count_var),
fill = !!ggplot2::sym("mean_effect"),
mean_q = !!ggplot2::sym("mean_q"),
mean_sd_from_mean = !!ggplot2::sym("mean_sd_from_mean"),
values = !!ggplot2::sym(tolower(count_var))
)
) +
ggplot2::geom_col() +
ggplot2::labs(title = paste0("Enrichment results associated with:\n",
subtitle
),
x = group_var,
y = paste(count_var,"count"),
fill = "Mean fold change") +
ggplot2::scale_y_continuous(breaks = scales::pretty_breaks(),
expand = ggplot2::expansion(mult = c(0, .1))) +
ggplot2::scale_fill_viridis_c(option = option) +
ggplot2::theme_bw() +
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 45,
vjust = 0.5, hjust = 1,
size = 10))
if(isTRUE(interactive)){
plt <- plotly::ggplotly(plt) |>
plotly::layout(hoverlabel = list(align = "left"),
margin = list(l = 0, r = 0,
b = 0, t = 70,
pad = 0) )
}
if(show_plot) methods::show(plt)
return(plt)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.