From 17719f3c9f22751936eb7d7fd770a04534be943a Mon Sep 17 00:00:00 2001 From: delfarahalireza Date: Thu, 14 Mar 2024 20:20:38 -0700 Subject: [PATCH] issue-24 fixing bug for volcano labels --- R/differential_expression.R | 41 +++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/R/differential_expression.R b/R/differential_expression.R index 97ae99f..7db0ceb 100644 --- a/R/differential_expression.R +++ b/R/differential_expression.R @@ -292,11 +292,10 @@ diffex_fdr <- function(term_data) { #' #' @export plot_volcano <- function( - regression_significance, - max_p_trans = 10, - FDR_cutoff = 0.1, - feature_labels = NULL - ) { + regression_significance, + max_p_trans = 10, + FDR_cutoff = 0.1, + feature_labels = NULL) { checkmate::assertDataFrame(regression_significance) stopifnot("term" %in% colnames(regression_significance)) @@ -311,24 +310,30 @@ plot_volcano <- function( stop("volcano plot cannot be generated due to unknown test") } - regression_significance %>% - dplyr::filter(!is.na(p.value)) %>% - dplyr::mutate( - p.value.trans = trans_pvalues(p.value, max_p_trans = max_p_trans), - is_discovery = qvalue < FDR_cutoff - ) %>% - ggplot(aes_string(x = effect_var)) + - {if ("compoundName" %in% colnames(regression_significance)) { - geom_point(aes(y = p.value.trans, color = is_discovery, name = compoundName)) + - geom_text(aes(label = ifelse(compoundName %in% feature_labels, compoundName, ""), y = p.value.trans, vjust = -0.75)) - } - else {geom_point(aes(y = p.value.trans, color = is_discovery))} - } + + grob <- ggplot( + regression_significance %>% + dplyr::filter(!is.na(p.value)) %>% + dplyr::mutate( + p.value.trans = trans_pvalues(p.value, max_p_trans = max_p_trans), + is_discovery = qvalue < FDR_cutoff + ), + aes_string(x = effect_var) + ) + facet_wrap(~term, scales = "free_x") + scale_x_continuous("Effect size") + scale_y_continuous(expression(-log[10] ~ "pvalue")) + scale_color_manual(values = c("FALSE" = "gray50", "TRUE" = "RED")) + theme_bw() + + if ("compoundName" %in% colnames(regression_significance)) { + grob <- grob + + geom_point(aes(y = p.value.trans, color = is_discovery, name = compoundName)) + + geom_text(aes(label = ifelse(compoundName %in% feature_labels, compoundName, ""), y = p.value.trans, vjust = -0.75)) + } else { + grob <- grob + + geom_point(aes(y = p.value.trans, color = is_discovery)) + } + return(grob) } trans_pvalues <- function(p, max_p_trans = 10) {