added boxplot.R
This commit is contained in:
parent
ad537a3ace
commit
2634aeeb84
1 changed files with 160 additions and 0 deletions
160
boxplot.R
Normal file
160
boxplot.R
Normal file
|
@ -0,0 +1,160 @@
|
|||
#!/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 + stat_compare_means(comparisons = my_comparisons
|
||||
, method = "wilcox.test"
|
||||
, paired = F
|
||||
, label = "p.format")
|
||||
|
||||
|
||||
########################################################################
|
||||
# Output plots as one pdf
|
||||
cat("Output plots will be in:", output_boxplot)
|
||||
pdf(output_boxplot, width=15, height=12)
|
||||
|
||||
y_value = "value"
|
||||
my_title1 = "Boxplots"
|
||||
|
||||
p1 = ggplot(lf, aes(x = factor(timepoint), y = eval(parse(text=y_value)), fill = factor(obesity) )) +
|
||||
scale_fill_manual(values=c("#00BFC4", "#F8766D")) +
|
||||
facet_wrap(~ mediator, nrow = 9, ncol = 4, scales = "free_y") +
|
||||
geom_boxplot(width = 0.5, outlier.colour = NA) +
|
||||
#geom_point(position = position_jitterdodge(dodge.width=0.5)
|
||||
# , aes(colours = factor(obesity))) +
|
||||
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 = 10)
|
||||
, legend.text = element_text(size = 15)
|
||||
, legend.direction = "vertical") +
|
||||
labs(title = my_title1
|
||||
, x = ""
|
||||
, y = "Levels")+
|
||||
stat_compare_means(comparisons = my_comparisons
|
||||
, method = "wilcox.test"
|
||||
, paired = F
|
||||
, label = "p.format")
|
||||
#p1
|
||||
shift_legend2(p1)
|
||||
dev.off()
|
Loading…
Add table
Add a link
Reference in a new issue