mosaic_2020/boxplot_stat_function.R

101 lines
3.2 KiB
R
Executable file

#!/usr/bin/Rscript
getwd()
setwd("~/git/mosaic_2020/")
getwd()
############################################################
# TASK: Boxplots with stats
# for each mediator, at each timepoint by group
# Handles some error: i.e will continue plotting even if there
# is stats error. However, needs fixing see below point 1):
# FIXME:
# 1) For a given mediator, if there is error for ANY timepoint
# stats will not be added for the entire plot!
# 2) make the function more generic to take the following inputs
# df, column, group
# this way you can directly pass the log_calculated value column
# To be handled in
# next iteration!
############################################################
is.error <- function(x) inherits(x, "try-error")
doMyPlotsStats <- function(df, custom_order=NULL) {
if(!is.null(custom_order)){
mediators = custom_order
} else{
mediators = levels(as.factor(df$mediator))
}
plots <- list()
for (i in mediators) {
cat("Creating plot for:", i, "\n")
single=df[df$mediator==i,]
max_y = max(single$value, na.rm = T)
#cat("Plotting:", i, "max_y:", max_y, "\n")
#----------
# boxplot
#----------
p = ggplot(single)+ geom_boxplot(aes(x = timepoint
, y = value
, color = obesity
#, palette = c("#00BFC4", "#F8766D")
))+
scale_colour_manual(values=c("#00BFC4", "#F8766D")) +
theme(axis.text.x = element_text(size = 10)
, axis.text.y = element_text(size = 15
, angle = 0
, hjust = 1
, vjust = 0)
, axis.title.x = element_blank()
, axis.title.y = element_blank()
, legend.position = "none"
, plot.subtitle = element_text(size = 14, hjust = 0.5)
, plot.title = element_text(size = 14, hjust = 0.5)
#, panel.grid.major = element_blank()
, panel.grid.minor = element_blank()
) +
labs(title = i)
#--------
# stats
#---------
stats_state <- try({
cat('Calculating stats for:', i, "\n")
stat_df <- single %>%
group_by(timepoint, mediator) %>%
wilcox_test(value ~ obesity, paired = F) %>%
add_significance("p")
stat_df$p_format = round(stat_df$p, digits = 2)
stat_df <- stat_df %>%
add_xy_position(x = "timepoint", dodge = 0.8)
}, silent=TRUE)
#print(stats_state)
if ( is.error(stats_state)) {cat("⚠️ Stats ERROR:", i, "\n")}
if ( ! is.error(stats_state)) {
cat('Adding stats to plot for:', i, "\n")
p = p + stat_pvalue_manual(stat_df
#, y.position = max_y
, label = "{p_format} {p.signif}"
, hide.ns = T
, tip.length = 0)+
scale_y_continuous(expand = expansion(mult = c(0.05, 0.25)))
}
plots[[i]] <- p
}
return(plots)
}
###############################################################