added functions for bp with stat and tested them

This commit is contained in:
Tanushree Tunstall 2021-08-27 14:05:00 +01:00
parent edb409baef
commit 826d3c72b7
4 changed files with 103 additions and 105 deletions

View file

@ -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

View file

@ -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)
}

View file

@ -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

View file

@ -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"