From c8e21b928ca27eac0fa20c481bd057acb0fc05fc Mon Sep 17 00:00:00 2001 From: Tanushree Tunstall Date: Fri, 27 Aug 2021 14:05:00 +0100 Subject: [PATCH] added functions for bp with stat and tested them --- scripts/functions/lf_bp_stability.R | 100 --------------------- scripts/functions/lf_bp_with_stats.R | 68 ++++++++++++++ scripts/functions/test_lf_bp_with_stats.R | 28 ++++++ scripts/functions/test_lf_unpaired_stats.R | 12 +-- 4 files changed, 103 insertions(+), 105 deletions(-) delete mode 100644 scripts/functions/lf_bp_stability.R create mode 100644 scripts/functions/lf_bp_with_stats.R create mode 100644 scripts/functions/test_lf_bp_with_stats.R diff --git a/scripts/functions/lf_bp_stability.R b/scripts/functions/lf_bp_stability.R deleted file mode 100644 index 8ca4a7f..0000000 --- a/scripts/functions/lf_bp_stability.R +++ /dev/null @@ -1,100 +0,0 @@ -library(ggpubr) -################################################################### - -my_unpaired_stats <- function(lf_data - , lf_stat_value = "param_value" - , lf_stat_group = "mutation_info" - , lf_col_statvars = "param_type" - , my_paired = FALSE - , stat_adj = "none"){ - - stat_formula = as.formula(paste0(lf_stat_value, "~", lf_stat_group)) - - my_stat_df = compare_means(stat_formula - , group.by = lf_col_statvars - , data = lf_data - , paired = my_paired - , p.adjust.method = stat_adj) - - - return(my_stat_df) -} - -##################### -# call stat function -##################### -stat_results_df <- my_unpaired_stats(lf_data = lf_duet - , lf_stat_value = "param_value" - , lf_stat_group = "mutation_info" - , lf_col_statvars = "param_type" - , my_paired = FALSE - , stat_adj = "none" -) - -y_value = "param_value" - -################################# -my_comparisons <- list( c("DM", "OM") ) - -my_ats = 22 # axis text size -my_als = 20 # axis label size -my_fls = 20 # facet label size -my_pts = 22 # plot title size - -#################################### - - -stat_bp_mut <- function(df - , x_bp_cateog = "mutation_info" - , y_var = "param_value" - , facet_var = "param_type" - , scales = "free_y" - , title = "" - , col_categ = "duet_outcome" - , grp_comp = "my_comparisons" - , stat_method = "wilcox.test" - , my_paired = FALSE - #, stat_label = "p.format") - , stat_label = "p.signif" ) - - -############################# -y_value = "param_value" - - -p1 = ggplot(lf_duet, aes(x = mutation_info - , y = eval(parse(text = y_value)) )) + - facet_wrap(~ param_type - , nrow = 1 - , scales = "free_y") + - geom_boxplot(fill = "white", outlier.colour = NA - #, position = position_dodge(width = 0.9) - , width = 0.2) + - geom_point(position = position_jitterdodge(dodge.width=0.01) - , alpha = 0.5 - , show.legend = FALSE - , aes(colour = factor(duet_outcome))) + - theme(axis.text.x = element_text(size = my_ats) - , axis.text.y = element_text(size = my_ats - , angle = 0 - , hjust = 1 - , vjust = 0) - , axis.title.x = element_text(size = my_ats) - , axis.title.y = element_text(size = my_ats) - , plot.title = element_text(size = my_pts , hjust = 0.5, colour = "black", face = "bold") - , strip.background = element_rect(fill = "khaki2") - , strip.text.x = element_text(size = my_fls, colour = "black") - , legend.title = element_text(color = "black", size = my_als) - , legend.text = element_text(size = my_ats) - , legend.direction = "vertical") + - labs(title = "DUET" - , x = "" - , y = "")+ - stat_compare_means(comparisons = my_comparisons - , method = "wilcox.test" - , paired = FALSE - #, label = "p.format") - , label = "p.signif") - -p1 - diff --git a/scripts/functions/lf_bp_with_stats.R b/scripts/functions/lf_bp_with_stats.R new file mode 100644 index 0000000..336c270 --- /dev/null +++ b/scripts/functions/lf_bp_with_stats.R @@ -0,0 +1,68 @@ +library(ggpubr) +################################################################### + +#################################### +lf_bp_with_stats <- function(lf_df + , x_grp = "mutation_info" + , y_var = "param_value" + , facet_var = "param_type" + , n_facet_row = 1 + , y_scales = "free_y" + , p_title = "" + , colour_categ = "" + , colour_bp_strip = "khaki2" + , stat_grp_comp = c("DM", "OM") + , stat_method = "wilcox.test" + , my_paired = FALSE + #, stat_label = "p.format") + , stat_label = c("p.format", "p.signif") + , my_ats = 22 # axis text size + , my_als = 20 # axis label size + , my_fls = 20 # facet label size + , my_pts = 22 # plot title size +) { + my_comparisonsL <- list( stat_grp_comp ) + + bp_statP <- ggplot(lf_df, aes(x = eval(parse(text = x_grp)) + , y = eval(parse(text = y_var)) )) + + + facet_wrap(~ eval(parse(text = facet_var)) + , nrow = n_facet_row + , scales = y_scales) + + + geom_boxplot(fill = "white", outlier.colour = NA + #, position = position_dodge(width = 0.9) + , width = 0.2) + + + geom_point(position = position_jitterdodge(dodge.width = 0.01) + , alpha = 0.5 + , show.legend = FALSE + , aes(colour = factor(eval(parse(text = colour_categ))) )) + + + theme(axis.text.x = element_text(size = my_ats) + , axis.text.y = element_text(size = my_ats + , angle = 0 + , hjust = 1 + , vjust = 0) + , axis.title.x = element_text(size = my_ats) + , axis.title.y = element_text(size = my_ats) + , plot.title = element_text(size = my_pts , hjust = 0.5, colour = "black", face = "bold") + , strip.background = element_rect(fill = colour_bp_strip) + , strip.text.x = element_text(size = my_fls, colour = "black") + , legend.title = element_text(color = "black", size = my_als) + , legend.text = element_text(size = my_ats) + , legend.direction = "vertical") + + + labs(title = p_title + , x = "" + , y = "")+ + + stat_compare_means(comparisons = my_comparisonsL + , method = stat_method + , paired = my_paired + #, label = "p.format") + , label = stat_label[1]) + + return(bp_statP) + +} diff --git a/scripts/functions/test_lf_bp_with_stats.R b/scripts/functions/test_lf_bp_with_stats.R new file mode 100644 index 0000000..4cfff9d --- /dev/null +++ b/scripts/functions/test_lf_bp_with_stats.R @@ -0,0 +1,28 @@ +setwd("~/git/LSHTM_analysis/scripts/plotting/") + +source("../functions/lf_bp_with_stats.R") + +###################### +# call function +###################### +# Note: Data +# run other_plots_data.R +# to get the long format data to test this function + +lf_bp_with_stats(lf_df = lf_dynamut2 + , x_grp = "mutation_info" + , y_var = "param_value" + , facet_var = "param_type" + , n_facet_row = 1 + , y_scales = "free_y" + , p_title = "Dynamut2" + , colour_categ = "ddg_dynamut2_outcome" + , stat_grp_comp = c("DM", "OM") + , stat_method = "wilcox.test" + , my_paired = FALSE + #, stat_label = "p.format") + , stat_label = c("p.format", "p.signif") + , my_ats = 22 # axis text size + , my_als = 20 # axis label size + , my_fls = 20 # facet label size + , my_pts = 22 )# plot title size diff --git a/scripts/functions/test_lf_unpaired_stats.R b/scripts/functions/test_lf_unpaired_stats.R index 9ec4aac..a1ff9d1 100644 --- a/scripts/functions/test_lf_unpaired_stats.R +++ b/scripts/functions/test_lf_unpaired_stats.R @@ -1,13 +1,15 @@ setwd("~/git/LSHTM_analysis/scripts/functions") source("lf_unpaired_stats.R") +##################### +# call stat function() +# a useful way to check stats +# for any lf data +##################### +# Note: Data # run other_plots_data.R -# to get the df you want to test this function +# to get the long format data to test this function - -##################### -# call stat function -##################### stat_results_df <- lf_unpaired_stats(lf_data = lf_duet , lf_stat_value = "param_value" , lf_stat_group = "mutation_info"