-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXX_combined_plots_alpha.R
266 lines (212 loc) · 8.28 KB
/
XX_combined_plots_alpha.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# COMBINED PLOTS
# Combine results from different scripts into single
# plots to have consistent layouts (for publication).
#
# This an the other "XX_*.R" are largely redundant with previous
# scripts wrt the code for plotting. But I share them anyways in
# in case other fellas would be interested in ggplot tricks
# for multi-panel plots :)
#
# Used rarefied x 1000 data!
#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
library(tidyverse)
library(here)
# Palette
source("colors_GfasMS.R")
# ALPHA diversity + accumulation curve -------------------------
# Import data
# Alpha diversity metrics
alphas <- readRDS("./out/RDS_files/rarefied1000_alpha_div_metrics.rds ")
# OLD: rarefied_alpha_div_metrics.rds
# Accumulation curve
acc_curve <- readRDS("./out/RDS_files/ASV_cumsum_bind.rds")
# acc_curve_ggplot <- readRDS("./out/RDS_files/ggplot_acccurve_5gr.rds")
## Alpha - statistical tests -----------------------
# Test the difference between symbiotic and bleached, for each alpha div index
# Based on checks of assumptions (see '10_alpha_diversity.R') I can use
# - t-test for Observed richness and Faith_pd
# - wilcox.test for Simpson and Shannon
# Here re-do tests so I have the data to insert in the plots
# t-test Observed
tt_obs <- rstatix::t_test(data = alphas, Observed ~ state,
alternative = "greater", # "two.sided"
paired = F,
var.equal = T)
tt_obs$statistic
tt_obs$df
tt_obs$p
# t-test Faith pd
tt_fpd <- rstatix::t_test(data = alphas, Faith_PD ~ state,
alternative = "greater", # "two.sided"
paired = F,
var.equal = T)
tt_fpd$statistic
tt_fpd$df
tt_fpd$p
# wilcox.test Simpson
wr_sim <- stats::wilcox.test(data = alphas,
Shannon ~ state,
alternative = "greater") # W = 66.5, p-value = 0.244
wr_sim$method
wr_sim$statistic
wr_sim$p.value
# wilcox.test Shannon
wr_sha <- stats::wilcox.test(data = alphas,
Simpson ~ state,
alternative = "greater") # W = 66, p-value = 0.2318
wr_sha$method
wr_sha$statistic
wr_sha$p.value
## Plots: alpha div -------------------
alpha_plots <- function(data, y, ylab, test, Pval) {
y <- enquo(y)
ggplot(data = data,
aes(x = fct_rev(state),
y = !!y)) +
geom_boxplot(aes(group = state),
outlier.shape = NA,
width = 0.4,
color = "gray33") +
ggbeeswarm::geom_quasirandom(
aes(color = colony_ms, shape = state, size = state), # group = origin_ms,
width = 0.2,
stroke = 1.3 ) +
scale_color_manual(values = palette_GfasMS) +
scale_shape_manual(values = c(16, 1),
breaks = c("symbiotic", "bleached"),
labels = c("Symbiotic", "Bleached")) +
scale_size_manual(values = c(2, 1.5),
breaks = c("symbiotic", "bleached"),
labels = c("Symbiotic", "Bleached"),
guide = "none") +
scale_x_discrete(breaks = c("symbiotic", "bleached"),
labels = c("Symbiotic", "Bleached")) +
labs(
y = ylab,
subtitle = paste0(test, "<br>*P* = ", round(Pval, 3)),
shape = "State",
color = "Colony"
) +
theme_classic() +
theme(
axis.title.x = element_blank(),
axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1),
# axis.title.y = element_text(size = 15, face = "bold"),
# text = element_text(size = 13),
# axis.ticks = element_blank(),
panel.grid = element_blank(),
legend.position = "bottom",
# legend.box = "vertical", # works here but not after patchwork
legend.text = element_text(size = 7, vjust = 0.6),
legend.title = element_text(size = 7.5, vjust = 0.6, face = "bold"),
# legend.title = element_blank(),
legend.spacing = unit(0.05, 'cm'),
legend.key.size = unit(0.3, 'cm'),
aspect.ratio = 3,
plot.margin = margin(t = 0, r = 0, l = 0.7, b = 0, "cm"),
plot.subtitle = ggtext::element_markdown(size = 6, vjust = 0, hjust = 0)
) +
guides(
shape = guide_legend(
override.aes = list(
size = c(1.9, 1.3)) ) #
# color = guide_legend(
# override.aes = list(
# size = 1.2) )
)
}
alpha_plots(data = alphas, y = Faith_PD, ylab = "Faith PD", test = "t-test", Pval = tt_fpd$p)
obs <- alpha_plots(data = alphas, y = Observed, ylab = "Obs. nr of ASVs", test = "t-test", Pval = tt_obs$p)
sha <- alpha_plots(data = alphas, y = Shannon, ylab = "Shannon diversity", test = "Mann-Whitney U-test", Pval = wr_sha$p.value)
sim <- alpha_plots(data = alphas, y = Simpson, ylab = "Simpson evenness", test = "Mann-Whitney U-test", Pval = wr_sim$p.value)
fpd <- alpha_plots(data = alphas, y = Faith_pd, ylab = "Faith PD", test = "t-test", Pval = tt_fpd$p)
### Patch alphas ------------------------
library(patchwork)
patch <- (obs + sha + sim)
(alpha3 <- patch + plot_layout(guides = "collect") &
theme(legend.position = "bottom"
# legend.box = 'vertical'
)
)
## Plot: Accumulation curve plot --------------
(
acccurve <- ggplot(data = acc_curve, #ASV_cumsum_bind,
aes(x = rank, y = rel_ab_cumsum,
color = origin_state,
fill = origin_state)) +
geom_line() +
geom_point(shape = 21,
size = 1) +
scale_color_manual(breaks = c("Red Sea symbiotic", "Red Sea bleached",
"Hong Kong symbiotic", "Hong Kong bleached",
"Overall"),
values = c(Red_Sea, Red_Sea,
Hong_Kong, Hong_Kong,
"black")) +
scale_fill_manual(breaks = c("Red Sea symbiotic", "Red Sea bleached",
"Hong Kong symbiotic", "Hong Kong bleached",
"Overall"),
values = c(Red_Sea, "white", # "transparent", #"white",
Hong_Kong, "white", #"transparent", #
"black")) +
scale_y_continuous(limits = c(0, 100),
labels = scales::percent_format(scale = 1),
# suffix = " %", # Added recently - try
expand = c(0, 0)) +
scale_x_continuous(limits = c(0, 75),
breaks = seq(0, 150, 10),
expand = c(0, 0)) +
xlab("nr of ASVs") +
ylab("Cumulative abundance") +
theme_classic() +
theme(
axis.ticks = element_blank(),
panel.grid.major.y = element_line(colour = "grey96"), # "grey" linetype = "dotted"),
panel.grid.major.x = element_line(colour = "grey96"), # "grey75" linetype = "dotted"),
# legend.position = "none"
legend.text = element_text(size = 7, vjust = 0.6),
legend.spacing = unit(0.05, 'cm'),
legend.key.size = unit(0.3, 'cm'),
legend.title = element_blank(),
legend.position = "bottom",
# legend.justification = "centre", # doesn't do anything
aspect.ratio = 1
) +
guides(
color = guide_legend(nrow = 2, byrow = F)
)
)
## Patchwork --------------------
layout <- "
ABCD
"
(
patch <- (alpha3 + acccurve) +
plot_layout(
# guides = "collect",
design = layout,
nrow = 1
) +
plot_annotation(tag_levels = 'A') &
theme(
axis.title = element_text(size = 9),
axis.text = element_text(size = 8),
plot.tag = element_text(size = 10),
# plot.background = element_rect(fill = "yellow"),
plot.margin = margin(t = 0, r = 0.1, l = 0, b = 0, "cm")
)
)
## Export .PNG -----------------------------------
ggsave("./out/Gfas_16S/alpha_diversity/rar1000_alphas3_acccurve.png",
bg = "white",
dpi = 330,
units = "cm", width = 21, height = 11)
## Export .SVG -----------------------------------
library(svglite)
ggsave("./out/Gfas_16S/alpha_diversity/rar1000_alphas3_acccurve.svg",
bg = "white",
# dpi = 330,
units = "cm",
width = 21, height = 11)