-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboxplot_t1.R
executable file
·162 lines (142 loc) · 5.62 KB
/
boxplot_t1.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/Rscript
getwd()
setwd('~/git/covid_analysis/')
getwd()
############################################################
# TASK: boxplots at T1
# FIXME: currently not rendering, problem with NAs for stats?
############################################################
# source data
source("read_data.R")
# clear unwanted variables
rm(my_data)
############################################################
#=============
# Output: boxplot at T1 for all mediators
# currently ununsed
#=============
output_boxplot_t1 = paste0(outdir, "boxplot_t1_v3.pdf")
#%%========================================================
# read file
# data assignment for plots
wf = wf_data
lf = lf_data
#=====================
# data for plots: LF@T1
#=====================
table(lf$timepoint)
lf_t1 = lf[lf$timepoint == "t1",]
dim(lf_t1); str(lf_t1)
levels(factor(lf_t1$mediator)); table(lf_t1$mediator)
lf_t1$outcome_category = as.factor(lf_t1$outcomes)
levels(lf_t1$outcome_category)
lf_t1$outcomes = as.factor(lf_t1$outcomes)
str(lf_t1$outcomes)
#%%========================================================
# count na in mediator col
if (table(is.na(lf_t1$mediator)) == nrow(lf_t1)){
print("PASS: No NAs detected, lf data is good for plotting")
}else{
print("FAIL: NAs detected in mediator columna. Check source formatting for lf data")
#lf <- lf[!is.na(lf$mediator),]
#head(lf); str(lf)
quit()
}
# FIXME : stats on log 10 value
# ===========
# stats
# ===========
# with adjustment: also gives unadjusted P-values (BH and fdr are the same)
my_stat = compare_means(value~outcomes, group.by = "mediator"
, data = lf_t1
, paired = FALSE
, p.adjust.method = "BH")
#, p.adjust.method = "bonferroni")
# !! check: satsified!!
wf_death = wf_data[wf_data$outcomes == 0,]; table(wf_death$outcomes)
wf_recovered = wf_data[wf_data$outcomes == 1,]; table(wf_recovered$outcomes)
wilcox.test(wf_death$Angiopoietin2_pgmL_t1, wf_recovered$Angiopoietin2_pgmL_t1, paired = F)
wilcox.test(wf_death$sICAM1_ngmL_t1, wf_recovered$sICAM1_ngmL_t1, paired = F)
my_comparisons <- list( c(0, 1) )
my_comparisons <- list( c("0", "1") )
#====================================
# Output plots as one pdf
cat("Output plots will be in:", output_boxplot_t1)
pdf(output_plots, width=15, height=12)
# ====================================
# 1) Boxplot with facet wrap:
# x = time
# y = Linear (Levels)
# coloured: outcome
# ====================================
my_comparisons <- list( c("t1", "t2"), c("t2", "t3"), c("t1", "t3") )
y_value = "value"
my_title1 = "Boxplots of mediators at t1: linear scale"
p1 = ggplot(lf_t1, aes(x = factor(outcomes), y = eval(parse(text=y_value)) )) +
facet_wrap(~ mediator, nrow = 2, scales = "free_y") +
geom_boxplot(fill = "white", outlier.colour = NA
, width = 0.5) +
geom_point(position = position_jitterdodge(dodge.width=0.01)
, aes(colour = factor(outcomes))) +
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 = "vertical") +
labs(title = my_title1
, x = ""
, y = "Levels") +
scale_colour_discrete(name = "Patient outcome", labels = c("Non-survivors", "Survivors"))+
#guides(fill = guide_legend(reverse = T))
#scale_colour_manual(values = my_outcome_cols)
stat_compare_means(comparisons = my_comparisons
, method = "wilcox.test"
, paired = F
, label = "p.format")
shift_legend2(p1)
# ====================================
# 2) Boxplot with facet wrap:
# x = time
# y = Log(Levels)
# coloured: outcome
# ====================================
#y_value = "log10(value)" # shows log transformed scale
y_value = "value"
my_title2 = "Boxplots of mediators at t1: log scale"
p2 = ggplot(lf_t1, aes(x = factor(outcomes), y = eval(parse(text=y_value)) )) +
facet_wrap(~ mediator, nrow = 2, scales = "free_y") +
scale_y_log10()+
geom_boxplot(fill = "white", outlier.colour = NA
, width = 0.5) +
geom_point(position = position_jitterdodge(dodge.width=0.01)
, aes(colour = factor(outcomes))) +
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 = "vertical") +
labs(title = my_title2
, x = ""
, y = "Levels (Log10)") +
scale_colour_discrete(name = "Patient outcome", labels = c("Non-survivors", "Survivors"))+
#scale_colour_manual(values = my_outcome_cols)
stat_compare_means(comparisons = my_comparisons
, method = "wilcox.test"
, paired = F
, label = "p.format")
shift_legend2(p2)
dev.off()