mosaic_2020/boxplot_stat_log.R
2020-11-20 13:14:10 +00:00

176 lines
7 KiB
R
Executable file

#!/usr/bin/Rscript
getwd()
setwd("~/git/mosaic_2020/")
getwd()
###############################################################
# TASK: boxplots with stats for NPA, SAM and SERUM but with log scale
# for each mediator, at each timepoint, by group
# TODO: check boxplot_stat_function.R
# output plots even if stats fail due to data containing only LLODs
# however for a given mediator, if there is error for ANY timepoint
# stats will not be added for the entire plot! To be handled in
# next iteration!
###############################################################
#=============
# Input
#=============
source("boxplot_stat_function.R")
source("plot_data_na.R")
#=============
# Output
#=============
outfile_bp = paste0("boxplots_stats_all_NA_LOG-%03d.svg")
output_boxplot_stats = paste0(outdir_plots, outfile_bp); output_boxplot_stats
svg(output_boxplot_stats, width=22, height=16)
###############################################################
#===============================
# data assignment for plots
#===============================
#----------------------------------------
#-------
# NPA
#-------
# vl data juggling with 0!
vl_temp_fix = lf_fp_npa[lf_fp_npa$mediator=="vl_pfu_ul",]
vl_temp_fix$value[1:20]
vl_temp_fix$value[vl_temp_fix$value == 0] <- 1
vl_temp_fix$value[1:20]
orig_nrow = nrow(lf_fp_npa); orig_nrow; table(lf_fp_npa$mediator)
lf_fp_npa = lf_fp_npa[!lf_fp_npa$mediator=="vl_pfu_ul",]
temp_nrow = nrow(lf_fp_npa); temp_nrow; table(lf_fp_npa$mediator)
# add vl_temp_fix back
lf_fp_npa = rbind(lf_fp_npa, vl_temp_fix)
revised_nrow = nrow(lf_fp_npa); revised_nrow
orig_nrow == revised_nrow
head(lf_fp_npa$value)
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
# log all values and reassign
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
lf_fp_npa$value = log10(lf_fp_npa$value)
head(lf_fp_npa$value)
vl_temp_fix2 = lf_fp_npa[lf_fp_npa$mediator=="vl_pfu_ul",]
vl_temp_fix2$value[1:20]
#------------------------------------------
title_npa = "NPA (non-asthmatics)"
fp_npa = length(unique(lf_fp_npa$mosaic)); fp_npa
cat("\nPlotting boxplots with stats for:", title_npa
, "\n========================================================\n")
plots_npa = doMyPlotsStats(lf_fp_npa)
npa_plot = ggpubr::ggarrange(plotlist = plots_npa
, align = "hv"
, ncol = 7
, nrow = 5
, common.legend = T)
#npa_plot
npa_plot_annot = annotate_figure(npa_plot
, top = text_grob(title_npa
, color = "blue"
, face = "bold"
, size = 18)
, bottom = text_grob(paste0("Mosaic data\nFP non-asthmatic adults (n=", fp_npa, ")")
, color = "blue"
, hjust = 1
, x = 0.98
, face = "italic"
, size = 10)
, left = text_grob("Levels (pg/ml)"
, color = "black"
, rot = 90
, size = 18))
npa_plot_annot
#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
#-------------
# SAM
#-------------
head(lf_fp_sam$value)
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
# log all values and reassign
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
lf_fp_sam$value = log10(lf_fp_sam$value)
head(lf_fp_sam$value)
title_sam = "SAM (non-asthmatics)"
fp_sam = length(unique(lf_fp_sam$mosaic)); fp_sam
cat("\nPlotting boxplots with stats for:", title_sam
, "\n========================================================\n")
plots_sam = doMyPlotsStats(lf_fp_sam)
sam_plot = ggpubr::ggarrange(plotlist = plots_sam
, align = "hv"
, ncol = 7
, nrow = 5
, common.legend = T)
#sam_plot
sam_plot_annot = annotate_figure(sam_plot
, top = text_grob(title_sam
, color = "blue"
, face = "bold"
, size = 18)
, bottom = text_grob(paste0("Mosaic data\nFP non-asthmatic adults (n=", fp_sam, ")")
, color = "blue"
, hjust = 1
, x = 0.98
, face = "italic"
, size = 10)
, left = text_grob("Levels (pg/ml)"
, color = "black"
, rot = 90
, size = 18))
sam_plot_annot
#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
#------------------------------------------
#-------------
# SERUM
#-------------
head(lf_fp_serum$value)
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
# log all values and reassign
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
lf_fp_serum$value = log10(lf_fp_serum$value)
head(lf_fp_serum$value)
title_serum = "SERUM (non-asthmatics)"
fp_serum = length(unique(lf_fp_serum$mosaic)); fp_serum
cat("\nPlotting boxplots with stats for:", title_serum
, "\n========================================================\n")
plots_serum = doMyPlotsStats(lf_fp_serum)
serum_plot = ggpubr::ggarrange(plotlist = plots_serum
, align = "hv"
, ncol = 7
, nrow = 5
, common.legend = T)
#serum_plot
serum_plot_annot = annotate_figure(serum_plot
, top = text_grob(title_serum
, color = "blue"
, face = "bold"
, size = 18)
, bottom = text_grob(paste0("Mosaic data\nFP non-asthmatic adults (n=", fp_serum, ")")
, color = "blue"
, hjust = 1
, x = 0.98
, face = "italic"
, size = 10)
, left = text_grob("Levels (pg/ml)"
, color = "black"
, rot = 90
, size = 18))
serum_plot_annot
dev.off()
###############################################################
# END OF SCRIPT #
###############################################################