mosaic_2020/boxplot.R

171 lines
5.1 KiB
R

#!/usr/bin/Rscript
getwd()
setwd("~/git/mosaic_2020/")
getwd()
############################################################
# TASK: boxplots at T1
# FIXME: currently not rendering, problem with NAs for stats?
############################################################
my_sample_type = "serum"
#=============
# Input
#=============
source("data_extraction_formatting.R")
# check: adult variable and age variable discrepancy!
metadata_all$mosaic[metadata_all$adult==1 & metadata_all$age<=18]
#=============
# Output:
#=============
outfile_bp = paste0("boxplots_", my_sample_type, ".pdf")
output_boxplot = paste0(outdir_plots, outfile_bp); output_boxplot
#===============================
# data assignment for stats
#================================
wf = serum_wf[serum_wf$flustat == 1,]
lf = serum_lf[serum_lf$flustat == 1,]
lf$timepoint = paste0("t", lf$timepoint)
#==================
# Data for plotting
#==================
# convert these to factor
lf$obesity = as.factor(lf$obesity)
lf$timepoint = as.factor(lf$timepoint)
########################################################################
my_comparisons <- list( c("0", "1") )
########################################################################
lf_test = lf[lf$mediator == "eotaxin",]
str(lf_test)
p = ggplot(lf_test, aes(x = timepoint, y = value, fill = obesity )) +
geom_boxplot(width = 0.5)
p
# see default ggplot palette
ggplot_build(p)$data
p + stat_compare_means(comparisons = my_comparisons
, method = "wilcox.test"
, paired = F
, label = "p.format")
########################################################################
library(ggpubr)
library(rstatix)
# stats
stat.test <- lf_test %>%
group_by(timepoint) %>%
wilcox_test(value ~ obesity) %>%
adjust_pvalue(method = "bonferroni") %>%
add_significance("p.adj")
stat.test
# add stats to grouped boxplots
str(lf_test)
lf_test$obesity = as.factor(lf_test$obesity) # ensure factor
bxp <- ggboxplot(lf_test, x = "timepoint", y = "value",
color = "obesity", palette = c("#00BFC4", "#F8766D"))
bxp
stat.test <- stat.test %>%
add_xy_position(x = "timepoint", dodge = 0.8)
bxp + stat_pvalue_manual(
stat.test, label = "p", tip.length = 0
)
################################
lf_test2 = lf[order(lf$mediator),] # 2 meds
lf_test2 = lf_test2[1:798,]
table(lf_test2$mediator)
str(lf_test2)
stat.test <- lf_test2 %>%
group_by(timepoint, mediator) %>%
wilcox_test(value ~ obesity, paired = F) %>%
#adjust_pvalue(method = "bonferroni") %>%
#add_significance("p.adj")
add_significance("p")
stat.test
bxp <- ggboxplot(lf_test2, x = "timepoint", y = "value",
color = "obesity", palette = c("#00BFC4", "#F8766D")) +
facet_wrap(~mediator, scales = "free_y")
bxp
stat.test <- stat.test %>%
add_xy_position(x = "timepoint", dodge = 0.8)
bxp + stat_pvalue_manual(stat.test, label = "p.signif", tip.length = 0)
########################################################################
lf_comp = lf[-which(is.na(lf$value)),]
lf_test_comp = lf_comp[lf_comp$mediator == "eotaxin",]
str(lf_comp)
p2 = ggplot(lf_test_comp, aes(x = timepoint, y = value, fill = obesity )) +
geom_boxplot(width = 0.5)
p2
########################################################################
# Output plots as one pdf
cat("Output plots will be in:", output_boxplot)
pdf(output_boxplot, width = 25, height = 15)
#stats data
str(lf)
stat.test <- lf %>%
group_by(timepoint, mediator) %>%
wilcox_test(value ~ obesity, paired = F) %>%
#adjust_pvalue(method = "bonferroni") %>%
#add_significance("p.adj")
add_significance("p")
stat.test
bxp <- ggboxplot(lf, x = "timepoint", y = "value",
color = "obesity", palette = c("#00BFC4", "#F8766D")) +
facet_wrap(~mediator, nrow = 7, ncol = 5, scales = "free_y", shrink = T)+
scale_y_log10()
bxp
bxp + stat.test <- stat.test %>%
add_xy_position(x = "timepoint", dodge = 0.8)
bxp + stat_pvalue_manual(stat.test, label = "p.signif", tip.length = 0)
dev.off()
######output
# Output plots as one pdf
cat("Output plots will be in:", output_boxplot)
pdf(output_boxplot, width = 20, height = 15)
my_title1 = "Boxplots: serum"
bxp <- ggboxplot(lf, x = "timepoint", y = "value",
color = "obesity", palette = c("#00BFC4", "#F8766D")) +
facet_wrap(~mediator, nrow = 7, ncol = 5, scales = "free_y", shrink = T)+
scale_y_log10()
bxp + theme(axis.text.x = element_text(size = 15)
, axis.text.y = element_text(size = 15
, angle = 0
, hjust = 1
, vjust = 0)
, axis.title.x = element_text(size = 15)
, axis.title.y = element_text(size = 15)
, plot.title = element_text(size = 20, hjust = 0.5)
, strip.text.x = element_text(size = 15, colour = "black")
, legend.title = element_text(color = "black", size = 20)
, legend.text = element_text(size = 15)
, legend.direction = "horizontal") +
labs(title = my_title1
, x = ""
, y = "Levels (Log10)")
#shift_legend2(bxp)
dev.off()