diff --git a/boxplot_linear.R b/boxplot_linear.R new file mode 100644 index 0000000..5a41b01 --- /dev/null +++ b/boxplot_linear.R @@ -0,0 +1,159 @@ +#!/usr/bin/Rscript +getwd() +setwd("~/git/mosaic_2020/") +getwd() +############################################################ +# TASK: boxplots at T1 +# FIXME: currently not rendering, problem with NAs for stats? +############################################################ +my_samples = "npa_sam_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_linear_", my_samples, ".pdf") +output_boxplot = paste0(outdir_plots, outfile_bp); output_boxplot + +#=============================== +# data assignment for plots +#================================ +#wf_fp_npa = npa_wf[npa_wf$flustat == 1,] +lf_fp_npa = npa_lf[npa_lf$flustat == 1,] +lf_fp_npa$timepoint = paste0("t", lf_fp_npa$timepoint) +lf_fp_npa$timepoint = as.factor(lf_fp_npa$timepoint) +lf_fp_npa$obesity = as.factor(lf_fp_npa$obesity) + +table(lf_fp_npa$mediator) +head(lf_fp_npa$value[lf_fp_npa$mediator == "vitd"]) +lf_fp_npa = lf_fp_npa[!lf_fp_npa$mediator == "vitd",] + + +#wf_fp_sam = samm_wf[samm_wf$flustat == 1,] +lf_fp_sam = sam_lf[sam_lf$flustat == 1,] +lf_fp_sam$timepoint = paste0("t", lf_fp_sam$timepoint) +lf_fp_sam$timepoint = as.factor(lf_fp_sam$timepoint) +lf_fp_sam$obesity = as.factor(lf_fp_sam$obesity) + +table(lf_fp_sam$mediator) +head(lf_fp_sam$value[lf_fp_sam$mediator == "vitd"]) +lf_fp_sam = lf_fp_sam[!lf_fp_sam$mediator == "vitd",] + + +#wf_fp_serum = serum_wf[serum_wf$flustat == 1,] +lf_fp_serum = serum_lf[serum_lf$flustat == 1,] +lf_fp_serum$timepoint = paste0("t", lf_fp_serum$timepoint) +lf_fp_serum$timepoint = as.factor(lf_fp_serum$timepoint) +lf_fp_serum$obesity = as.factor(lf_fp_serum$obesity) + +######################################################################## +cat("Output plots will be in:", output_boxplot) +pdf(output_boxplot, width = 20, height = 15) + +#======================================================================= +# NPA +#======================================================================= +if (is.factor(lf_fp_npa$timepoint) && is.factor(lf_fp_npa$timepoint)){ + cat ("PASS: required groups are factors") +} +#------------------------------------------ +title_npa_linear = "Boxplot: NPA (Linear)" +#----------------------------------------- +bxp_npa_linear <- ggboxplot(lf_fp_npa, 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() + + 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 = title_npa_linear + , x = "" + , y = "Levels") + +bxp_npa_linear + + +#======================================================================= +# SAM +#======================================================================= +if (is.factor(lf_fp_sam$timepoint) && is.factor(lf_fp_sam$timepoint)){ + cat ("PASS: required groups are factors") +} + +#------------------------------------------ +title_sam_linear = "Boxplot: sam (Linear)" +#----------------------------------------- +bxp_sam_linear <- ggboxplot(lf_fp_sam, 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() + + 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 = title_sam_linear + , x = "" + , y = "Levels") + +bxp_sam_linear + +#======================================================================= +# SERUM +#======================================================================= +if (is.factor(lf_fp_serum$timepoint) && is.factor(lf_fp_serum$timepoint)){ + cat ("PASS: required groups are factors") +} + +table(lf_fp_serum$mediator) + +#------------------------------------------ +title_serum_linear = "Boxplot: serum (Linear)" +#----------------------------------------- +bxp_serum_linear <- ggboxplot(lf_fp_serum, 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() + + 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 = title_serum_linear + , x = "" + , y = "Levels") + +bxp_serum_linear + +#========================================================================== +dev.off() +############################################################################ \ No newline at end of file