-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRhizo_assembly_analysis.Rmd
2181 lines (1637 loc) · 101 KB
/
Rhizo_assembly_analysis.Rmd
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Rhizo_assembly_analysis"
author: "Abby Sulesky-Grieb"
date: "2023-09-10"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r packages, echo=FALSE, results='hide', message=FALSE, warning=FALSE}
library(microViz)
library(corncob)
library(ggraph)
library(DT)
library(phyloseq)
library(ggplot2)
library(tidyverse)
library(decontam)
library(scales)
library(vegan)
library(microbiome)
library(ComplexHeatmap)
library(dplyr)
library(patchwork)
library(pairwiseAdonis)
library(indicspecies)
library(VennDiagram)
library(stats)
library(rstatix)
library(ecotraj)
library(corrplot)
library(ComplexHeatmap)
library(paletteer)
library(ggthemes)
library(indicspecies)
library(reltools)
library(minpack.lm)
library(Hmisc)
library(stats4)
```
```{r load}
setwd("/Users/Abby/OneDrive - Michigan State University/Rhizo_assembly/")
# load phyloseq
RA_phyloseq <- readRDS("/Users/Abby/OneDrive - Michigan State University/Rhizo_assembly/RA_phyloseq_rarefied.rds")
# add group and timepoint categories in metadata for analysis
#write.csv(data.frame(sample_data(RA_phyloseq)), file="metadata_update.csv")
meta_update <- read.csv("/Users/Abby/OneDrive - Michigan State University/Rhizo_assembly/Rhizosphere_assembly_Common_Bean/R_Analysis_Files/metadata_update_edit.csv", sep=",", row.names = 1)
meta <- sample_data(meta_update)
tax <- tax_table(RA_phyloseq)
asv <- otu_table(RA_phyloseq)
RAphyloseq <- merge_phyloseq(meta, tax, asv)
RAsampledata <- data.frame(sample_data(RAphyloseq))
RA_ASVtable <- data.frame(otu_table(RAphyloseq))
RA_taxtable <- data.frame(tax_table(RAphyloseq))
RA_ASVtable$sum <- rowSums(RA_ASVtable)
RA_ASVtable_filtered <- filter(RA_ASVtable, RA_ASVtable$sum > 2)
RA_ASVtable_filtered <- RA_ASVtable[order(RA_ASVtable$sum, decreasing=TRUE),]
# write.csv(RAsampledata, file="/Users/Abby/OneDrive - Michigan State University/Rhizo_assembly/bean_assembly_metadata.csv")
# write.csv(RA_ASVtable_filtered, file="/Users/Abby/OneDrive - Michigan State University/Rhizo_assembly/bean_assembly_ASVtable.csv")
# write.csv(RA_taxtable, file="/Users/Abby/OneDrive - Michigan State University/Rhizo_assembly/bean_assembly_taxtable.csv")
#sample_names(RAphyloseq)
#sample_data(RAphyloseq)
# Subset compartments
rsphere_ps <- RAphyloseq %>% ps_filter(compartment == "rhizosphere")
rplane_ps <- RAphyloseq %>% ps_filter(compartment == "rhizoplane")
# code for background theme for ggplot so I don't have to have it in every plot
my_theme <- theme(panel.background = element_rect(fill = "white", colour = "white"),
panel.grid.major = element_line(size = 0.25, linetype = 'solid', colour = "light gray"),
panel.grid.minor = element_line(size = 0.25, linetype = 'solid', colour = "light gray"))
# Colors
colors <- c("#F8766D", "#00bfc4")
```
### rhizosphere
```{r alpha_rs}
# x-axis order
sampling_order_growth <- c("V1", "V2","V3","V4","R1","R4", "R7")
sampling_order_time <- c("day3", "day7", "day14", "day21", "day35", "day49", "day63")
sample_data(rsphere_ps)$timepoint <- as.factor(sample_data(rsphere_ps)$timepoint)
growth_rs <- rsphere_ps %>% ps_filter(series == "growth")
time_rs <- rsphere_ps %>% ps_filter(series == "time")
sampling_order <- c("V1", "V2","V3","V4","R1","R4", "R7", "day3", "day7", "day14", "day21", "day35", "day49", "day63")
#observed_alphaplot_all_rs <- plot_richness(rsphere_ps, x="timepoint", measures=c("Observed"), color="treatment") + geom_boxplot() + ylab("Number of ASVs") + stat_summary(fun=mean, colour="black", aes(group=group), geom="line", lwd=0.5, lty=1, na.rm=TRUE) + facet_grid(rows=vars(treatment), cols=vars(series), scales="free_x") + scale_x_discrete(labels=sampling_order_growth, breaks=waiver()) + theme(legend.position = "none") + labs(title="Rhizosphere") + scale_x_discrete(name ="Sample point", labels=sampling_order) #+ scale_color_manual(name = "Treatment Group", values = colors, labels = c("Control", "Zeb")) + xlab("Sample point")
#observed_alphaplot_all_rs
# alpha diversity, facet wrap by group
observed_alphaplot_growth_rs <- plot_richness(growth_rs, x="timepoint", measures=c("Observed"), color="treatment") + geom_boxplot(aes()) + xlab("Sample point") + ylab("Number of ASVs") + stat_summary(fun=mean, colour="black", aes(group=group), geom="line", lwd=0.5, lty=1, na.rm=TRUE) + facet_wrap(~group, nrow=2) + ylim(600, 2300) + scale_x_discrete(labels=sampling_order_growth, breaks=waiver()) + theme(legend.position = "none") + labs(title="Rhizosphere - Growth") #+ scale_color_manual(name = "Treatment Group", values = colors, labels = c("Control", "Zeb"))
observed_alphaplot_growth_rs
observed_alphaplot_time_rs <- plot_richness(time_rs, x="timepoint", measures=c("Observed"), color="treatment") + geom_boxplot(aes()) + xlab("Sample point") + ylab("Number of ASVs") + stat_summary(fun=mean, colour="black", aes(group=group), geom="line", lwd=0.5, lty=1, na.rm=TRUE) + facet_wrap(~group, nrow=2) + ylim(600, 2300) + scale_x_discrete(labels=sampling_order_time, breaks=waiver()) + theme(axis.title.y = element_blank()) + labs(title="Rhizosphere - Time")
observed_alphaplot_time_rs
alpha_panels_rs <- (observed_alphaplot_growth_rs | observed_alphaplot_time_rs) + plot_annotation(tag_levels = 'A')
alpha_panels_rs
#ggsave(alpha_panels_rs, file="/Users/Abby/OneDrive - Michigan State University/Rhizo_assembly/alpha_panels_rs.png", height = 6, width = 8, units = "in")
# will need to separate groups to get time point labels, do this later
# stats
alpha_counts_rs <- estimate_richness(rsphere_ps, measures=c("Observed"))
alpha_counts_rs <- cbind(rownames(alpha_counts_rs), alpha_counts_rs)
colnames(alpha_counts_rs)[1] <- "SampleID"
alpha_counts_rs
metadata_rs <- data.frame(sample_data(rsphere_ps))
alpha_data_rs <- left_join(alpha_counts_rs, metadata_rs, by="SampleID")
#write.csv(alpha_data_rs, file="alpha_data_rs.csv")
Control_growth_rs <- filter(alpha_data_rs, group == "Control_growth")
Zeb_growth_rs <- filter(alpha_data_rs, group == "Zeb_growth")
Control_time_rs <- filter(alpha_data_rs, group == "Control_time")
Zeb_time_rs <- filter(alpha_data_rs, group == "Zeb_time")
# control_growth (CG)
CG_alpha_aov_rs <- aov(Observed ~ stage, data=Control_growth_rs)
summary(CG_alpha_aov_rs)
CG_alpha_tukey_rs <- tukey_hsd(Control_growth_rs, Observed ~ stage)
CG_alpha_tukey_rs
# no significance
# Zeb_growth (ZG)
ZG_alpha_aov_rs <- aov(Observed ~ stage, data=Zeb_growth_rs)
summary(ZG_alpha_aov_rs)
ZG_alpha_tukey_rs <- tukey_hsd(Zeb_growth_rs, Observed ~ stage)
ZG_alpha_tukey_rs
# no significance
# Control_time (CT)
CT_alpha_aov_rs <- aov(Observed ~ stage, data=Control_time_rs)
summary(CT_alpha_aov_rs)
CT_alpha_tukey_rs <- tukey_hsd(Control_time_rs, Observed ~ stage)
CT_alpha_tukey_rs
# no significance
# Zeb_time (CT)
ZT_alpha_aov_rs <- aov(Observed ~ stage, data=Zeb_time_rs)
summary(ZT_alpha_aov_rs)
ZT_alpha_tukey_rs <- tukey_hsd(Zeb_time_rs, Observed ~ stage)
ZT_alpha_tukey_rs
# no significance
# between all groups
all_alpha_aov_rs <- aov(Observed ~ group_stage, data=alpha_data_rs)
summary(all_alpha_aov_rs)
all_alpha_tukey_rs <- tukey_hsd(alpha_data_rs, Observed ~ group_stage)
all_alpha_tukey_rs
# no significance
v1_ttest_rs <- t.test(Observed ~ treatment, data=subset(alpha_data_rs, stage == "V1"), na.action = "na.omit")
v1_ttest_rs
v2_ttest_rs <- t.test(Observed ~ treatment, data=subset(alpha_data_rs, stage == "V2"), na.action = "na.omit")
v2_ttest_rs
v3_ttest_rs <- t.test(Observed ~ treatment, data=subset(alpha_data_rs, stage == "V3"), na.action = "na.omit")
v3_ttest_rs
v4_ttest_rs <- t.test(Observed ~ treatment, data=subset(alpha_data_rs, stage == "V4"), na.action = "na.omit")
v4_ttest_rs
r1_ttest_rs <- t.test(Observed ~ treatment, data=subset(alpha_data_rs, stage == "R1"), na.action = "na.omit")
r1_ttest_rs
r4_ttest_rs <- t.test(Observed ~ treatment, data=subset(alpha_data_rs, stage == "R4"), na.action = "na.omit")
r4_ttest_rs
r7_ttest_rs <- t.test(Observed ~ treatment, data=subset(alpha_data_rs, stage == "R7"), na.action = "na.omit")
r7_ttest_rs
day3_ttest_rs <- t.test(Observed ~ treatment, data=subset(alpha_data_rs, stage == "day3"), na.action = "na.omit")
day3_ttest_rs
day7_ttest_rs <- t.test(Observed ~ treatment, data=subset(alpha_data_rs, stage == "day7"), na.action = "na.omit")
day7_ttest_rs
day14_ttest_rs <- t.test(Observed ~ treatment, data=subset(alpha_data_rs, stage == "day14"), na.action = "na.omit")
day14_ttest_rs
day21_ttest_rs <- t.test(Observed ~ treatment, data=subset(alpha_data_rs, stage == "day21"), na.action = "na.omit")
day21_ttest_rs
day35_ttest_rs <- t.test(Observed ~ treatment, data=subset(alpha_data_rs, stage == "day35"), na.action = "na.omit")
day35_ttest_rs
day49_ttest_rs <- t.test(Observed ~ treatment, data=subset(alpha_data_rs, stage == "day49"), na.action = "na.omit")
day49_ttest_rs
day63_ttest_rs <- t.test(Observed ~ treatment, data=subset(alpha_data_rs, stage == "day63"), na.action = "na.omit")
day63_ttest_rs
```
### rhizoplane
```{r alpha_rp}
# x-axis order
sampling_order_growth <- c("V1", "V2","V3","V4","R1","R4", "R7")
sampling_order_time <- c("day3", "day7", "day14", "day21", "day35", "day49", "day63")
sample_data(rplane_ps)$timepoint <- as.factor(sample_data(rplane_ps)$timepoint)
growth_rp <- rplane_ps %>% ps_filter(series == "growth")
time_rp <- rplane_ps %>% ps_filter(series == "time")
anno_growth_rp <- data.frame(xstar = c(6),
ystar = c(1750),
lab = c("*"),
group = c("Control_growth"))
anno_growth_rp
# alpha diversity, facet wrap by group
observed_alphaplot_growth_rp <- plot_richness(growth_rp, x="timepoint", measures=c("Observed"), color="treatment") + geom_boxplot(aes()) + xlab("Sample point") + ylab("Number of ASVs") + stat_summary(fun=mean, colour="black", aes(group=group), geom="line", lwd=0.5, lty=1, na.rm=TRUE) +
geom_text(inherit.aes=FALSE, data = anno_growth_rp, aes(x = xstar, y = ystar, label = lab), size=6) + facet_wrap(~group, nrow=2) + scale_x_discrete(labels=sampling_order_growth, breaks=waiver()) + theme(legend.position = "none") + labs(title="Rhizoplane - Growth") #+ scale_color_manual(name = "Treatment Group", values = colors, labels = c("Control", "Zeb")) #+ labs(title="Rhizoplane 16S Alpha Diversity")
observed_alphaplot_growth_rp
anno_time_rp <- data.frame(xstar = c(3, 5),
ystar = c(1750, 1750),
lab = c("*", "*"),
group = c("Control_time", "Control_time"))
anno_time_rp
# alpha diversity, facet wrap by group
observed_alphaplot_time_rp <- plot_richness(time_rp, x="timepoint", measures=c("Observed"), color = "treatment") + geom_boxplot() + xlab("Sample point") + stat_summary(fun=mean, colour="black", aes(group=group),
geom="line", lwd=0.5, lty=1, na.rm=TRUE) +
geom_text(inherit.aes=FALSE, data = anno_time_rp, aes(x = xstar, y = ystar, label = lab), size=6) + facet_wrap(~group, nrow=2) + scale_x_discrete(labels=sampling_order_time, breaks=waiver()) + theme(axis.title.y = element_blank()) + labs(title="Rhizoplane - Time")
observed_alphaplot_time_rp
alpha_panels_rp <- (observed_alphaplot_growth_rp | observed_alphaplot_time_rp) + plot_annotation(tag_levels = 'A')
alpha_panels_rp
#ggsave(alpha_panels_rp, file="/Users/Abby/OneDrive - Michigan State University/Rhizo_assembly/alpha_panels_rp.png", height = 6, width = 8, units = "in")
# stats
alpha_counts_rp <- estimate_richness(rplane_ps, measures=c("Observed"))
alpha_counts_rp <- cbind(rownames(alpha_counts_rp), alpha_counts_rp)
colnames(alpha_counts_rp)[1] <- "SampleID"
alpha_counts_rp
metadata_rp <- data.frame(sample_data(rplane_ps))
alpha_data_rp <- left_join(alpha_counts_rp, metadata_rp, by="SampleID")
write.csv(alpha_data_rp, file="alpha_data_rp.csv")
Control_growth_rp <- filter(alpha_data_rp, group == "Control_growth")
Zeb_growth_rp <- filter(alpha_data_rp, group == "Zeb_growth")
Control_time_rp <- filter(alpha_data_rp, group == "Control_time")
Zeb_time_rp <- filter(alpha_data_rp, group == "Zeb_time")
# control_growth (CG)
CG_alpha_aov_rp <- aov(Observed ~ stage, data=Control_growth_rp)
summary(CG_alpha_aov_rp)
CG_alpha_tukey_rp <- tukey_hsd(Control_growth_rp, Observed ~ stage)
CG_alpha_tukey_rp
# no significance
# Zeb_growth (ZG)
ZG_alpha_aov_rp <- aov(Observed ~ stage, data=Zeb_growth_rp)
summary(ZG_alpha_aov_rp)
ZG_alpha_tukey_rp <- tukey_hsd(Zeb_growth_rp, Observed ~ stage)
ZG_alpha_tukey_rp
# no significance
# Control_time (CT)
CT_alpha_aov_rp <- aov(Observed ~ stage, data=Control_time_rp)
summary(CT_alpha_aov_rp)
CT_alpha_tukey_rp <- tukey_hsd(Control_time_rp, Observed ~ stage)
CT_alpha_tukey_rp
# no significance
# Zeb_time (CT)
ZT_alpha_aov_rp <- aov(Observed ~ stage, data=Zeb_time_rp)
summary(ZT_alpha_aov_rp)
ZT_alpha_tukey_rp <- tukey_hsd(Zeb_time_rp, Observed ~ stage)
ZT_alpha_tukey_rp
# no significance
# between all groups
all_alpha_aov_rp <- aov(Observed ~ group_stage, data=alpha_data_rp)
summary(all_alpha_aov_rp)
all_alpha_tukey_rp <- tukey_hsd(alpha_data_rp, Observed ~ group_stage)
all_alpha_tukey_rp
v1_ttest_rp <- t.test(Observed ~ treatment, data=subset(alpha_data_rp, stage == "V1"), na.action = "na.omit")
v1_ttest_rp
v2_ttest_rp <- t.test(Observed ~ treatment, data=subset(alpha_data_rp, stage == "V2"), na.action = "na.omit")
v2_ttest_rp
v3_ttest_rp <- t.test(Observed ~ treatment, data=subset(alpha_data_rp, stage == "V3"), na.action = "na.omit")
v3_ttest_rp
v4_ttest_rp <- t.test(Observed ~ treatment, data=subset(alpha_data_rp, stage == "V4"), na.action = "na.omit")
v4_ttest_rp
r1_ttest_rp <- t.test(Observed ~ treatment, data=subset(alpha_data_rp, stage == "R1"), na.action = "na.omit")
r1_ttest_rp
r4_ttest_rp <- t.test(Observed ~ treatment, data=subset(alpha_data_rp, stage == "R4"), na.action = "na.omit")
r4_ttest_rp # * .03
r7_ttest_rp <- t.test(Observed ~ treatment, data=subset(alpha_data_rp, stage == "R7"), na.action = "na.omit")
r7_ttest_rp
day3_ttest_rp <- t.test(Observed ~ treatment, data=subset(alpha_data_rp, stage == "day3"), na.action = "na.omit")
day3_ttest_rp
day7_ttest_rp <- t.test(Observed ~ treatment, data=subset(alpha_data_rp, stage == "day7"), na.action = "na.omit")
day7_ttest_rp
day14_ttest_rp <- t.test(Observed ~ treatment, data=subset(alpha_data_rp, stage == "day14"), na.action = "na.omit")
day14_ttest_rp # * .02
day21_ttest_rp <- t.test(Observed ~ treatment, data=subset(alpha_data_rp, stage == "day21"), na.action = "na.omit")
day21_ttest_rp
day35_ttest_rp <- t.test(Observed ~ treatment, data=subset(alpha_data_rp, stage == "day35"), na.action = "na.omit")
day35_ttest_rp # * .02
day49_ttest_rp <- t.test(Observed ~ treatment, data=subset(alpha_data_rp, stage == "day49"), na.action = "na.omit")
day49_ttest_rp
day63_ttest_rp <- t.test(Observed ~ treatment, data=subset(alpha_data_rp, stage == "day63"), na.action = "na.omit")
day63_ttest_rp
```
# ful dataset ordination
```{r}
perm <- how(nperm = 999)
metadata <- data.frame(sample_data(RAphyloseq))
BCdist = vegdist(t(otu_table(RAphyloseq)), method = "bray", binary = FALSE)
set.seed(8)
BC_adonis <- adonis2(formula = BCdist ~ treatment*group*group_stage, permutations = perm, data = metadata)
BC_adonis
# treatment 1 0.604 0.01010 2.8671 0.012 *
# group 2 0.530 0.00885 1.2565 0.198
# group_stage 24 7.907 0.13217 1.5635 0.001 ***
# group_pairwise <- pairwise.adonis2(BCdist_rp ~ group, data = metadata, permutations = perm)
# group_pairwise
sample_data(RAphyloseq)$timepoint <- as.factor(sample_data(RAphyloseq)$timepoint)
# ordinate
full_ord <- ordinate(physeq = RAphyloseq, method="PCoA", distance= BCdist)
# plot ordination
full_PCoA <- plot_ordination(RAphyloseq, full_ord, color ="treatment", shape="series") + theme(aspect.ratio=1)
full_PCoA + geom_point(aes(size=timepoint))
```
# rhizoplane beta diversity
```{r beta_rp}
perm <- how(nperm = 999)
metadata_rp <- data.frame(sample_data(rplane_ps))
BCdist_rp = vegdist(t(otu_table(rplane_ps)), method = "bray", binary = FALSE)
set.seed(8)
BC_adonis_rp <- adonis2(formula = BCdist_rp ~ treatment*age_days*stage_all, permutations = perm, data = metadata_rp)
BC_adonis_rp
# adonis2(formula = BCdist_rp ~ treatment * age_days * stage_all, data = metadata_rp, permutations = perm)
# Df SumOfSqs R2 F Pr(>F)
# treatment 1 0.6600 0.02418 4.3122 0.001 ***
# age_days 1 2.9064 0.10648 18.9896 0.001 ***
# stage_all 16 5.6261 0.20613 2.2975 0.001 ***
# treatment:age_days 1 0.2381 0.00872 1.5558 0.077 .
# treatment:stage_all 8 1.7930 0.06569 1.4644 0.002 **
# Residual 105 16.0704 0.58879
# Total 132 27.2940 1.00000
rplane_growth <- rplane_ps %>% ps_filter(series == "growth")
metadata_rp_growth <- data.frame(sample_data(rplane_growth))
BCdist_rp_growth = vegdist(t(otu_table(rplane_growth)), method = "bray", binary = FALSE)
set.seed(8)
BC_adonis_rp_growth <- adonis2(formula = BCdist_rp_growth ~ treatment*age_days*stage_all, permutations = perm, data = metadata_rp_growth)
BC_adonis_rp_growth
# adonis2(formula = BCdist_rp_growth ~ treatment * age_days * stage_all, data = metadata_rp_growth, permutations = perm)
# Df SumOfSqs R2 F Pr(>F)
# treatment 1 0.4276 0.03284 2.6425 0.003 **
# age_days 1 1.2456 0.09566 7.6976 0.001 ***
# stage_all 6 1.9870 0.15259 2.0465 0.001 ***
# treatment:age_days 1 0.2188 0.01680 1.3521 0.138
# treatment:stage_all 4 0.7278 0.05589 1.1244 0.217
# Residual 52 8.4145 0.64621
# Total 65 13.0213 1.00000
g_s_pairwise_rp <- pairwise.adonis2(BCdist_rp ~ group_stage, data = metadata_rp, permutations = perm)
#g_s_pairwise_rp
sample_data(rplane_ps)$timepoint <- as.factor(sample_data(rplane_ps)$timepoint)
# ordinate
full_ord_rp <- ordinate(physeq = rplane_ps, method="PCoA", distance= BCdist_rp)
# plot ordination
full_PCoA_rp <- plot_ordination(rplane_ps, full_ord_rp, color ="treatment", shape="series") + theme(aspect.ratio=1)
full_PCoA_rp + geom_point(aes(size=timepoint))
# Separate in comparisons that we are interested in
Cgrowth_Zgrowth_rp <- rplane_ps %>% ps_filter(group != "Control_time") %>% ps_filter(group != "Zeb_time")
Ctime_Ztime_rp <- rplane_ps %>% ps_filter(group != "Control_growth") %>% ps_filter(group != "Zeb_growth")
control_groups_rp <- rplane_ps %>% ps_filter(group != "Zeb_growth") %>% ps_filter(group != "Zeb_time")
zeb_groups_rp <- rplane_ps %>% ps_filter(group != "Control_growth") %>% ps_filter(group != "Control_time")
# test comparisons
# control vs control, should be the same
perm <- how(nperm = 9999)
C_C_metadata_rp <- data.frame(sample_data(control_groups_rp))
C_C_BCdist_rp = vegdist(t(otu_table(control_groups_rp)), method = "bray", binary = FALSE)
set.seed(8)
C_C_BC_adonis_rp <- adonis2(formula = C_C_BCdist_rp ~ group, permutations = perm, data = C_C_metadata_rp)
C_C_BC_adonis_rp
# group = NS r2=0.02354 F=1.4944 p=0.1142
# zeb vs zeb, should be different?
perm <- how(nperm = 9999)
ZZ_metadata_rp <- data.frame(sample_data(zeb_groups_rp))
ZZ_BCdist_rp = vegdist(t(otu_table(zeb_groups_rp)), method = "bray", binary = FALSE)
set.seed(8)
ZZ_BC_adonis_rp <- adonis2(formula = ZZ_BCdist_rp ~ group, permutations = perm, data = ZZ_metadata_rp)
ZZ_BC_adonis_rp
# group df=1 sum sq=0.3830 r2=0.03051 F=2.1087 0.0188 *
# control_growth vs zeb_growth
perm <- how(nperm = 9999)
Cg_Zg_metadata_rp <- data.frame(sample_data(Cgrowth_Zgrowth_rp))
Cg_Zg_BCdist_rp = vegdist(t(otu_table(Cgrowth_Zgrowth_rp)), method = "bray", binary = FALSE)
set.seed(8)
Cg_Zg_BC_adonis_rp <- adonis2(formula = Cg_Zg_BCdist_rp ~ group*stage, permutations = perm, data = Cg_Zg_metadata_rp)
Cg_Zg_BC_adonis_rp
# Df SumOfSqs R2 F Pr(>F)
# group 1 0.4388 0.03567 2.9122 0.0015 **
# stage 6 2.7742 0.22551 3.0686 0.0001 ***
# group:stage 6 1.2538 0.10192 1.3868 0.0128 *
# Residual 52 7.8350 0.63690
# Total 65 12.3018 1.00000
Cg_Zg_pairwise_rp <- pairwise.adonis2(Cg_Zg_BCdist_rp ~ group_stage, data = Cg_Zg_metadata_rp, permutations = perm)
#Cg_Zg_pairwise_rp
# $Control_growth_R1_vs_Zeb_growth_R1
# Df SumOfSqs R2 F Pr(>F)
# group_stage 1 0.19062 0.198 1.7282 0.0401 *
#
# $Zeb_growth_V1_vs_Control_growth_V1
# Df SumOfSqs R2 F Pr(>F)
# group_stage 1 0.20092 0.18031 1.7598 0.0161 *
#
# $Zeb_growth_V2_vs_Control_growth_V2
# Df SumOfSqs R2 F Pr(>F)
# group_stage 1 0.25737 0.17024 1.6413 0.0172 *
#
# $Control_growth_V3_vs_Zeb_growth_V3
# Df SumOfSqs R2 F Pr(>F)
# group_stage 1 0.18770 0.17949 1.3125 0.3031
#
# $Zeb_growth_R4_vs_Control_growth_R4
# Df SumOfSqs R2 F Pr(>F)
# group_stage 1 0.34333 0.19559 1.9452 0.0159 *
#
# $Control_growth_V4_vs_Zeb_growth_V4
# Df SumOfSqs R2 F Pr(>F)
# group_stage 1 0.18781 0.12141 1.1055 0.3406
#
# $Zeb_growth_R7_vs_Control_growth_R7
# Df SumOfSqs R2 F Pr(>F)
# group_stage 1 0.32125 0.2024 1.7763 0.0726
Cg_Zg_grouppairwise_rp <- pairwise.adonis2(Cg_Zg_BCdist_rp ~ group, data = Cg_Zg_metadata_rp, permutations = perm)
#Cg_Zg_grouppairwise_rp
# ordinate
Cg_Zg_ord_rp <- ordinate(physeq = Cgrowth_Zgrowth_rp, method="PCoA", distance= Cg_Zg_BCdist_rp)
# save vectors
Cg_Zg_rp_vectors <- as.data.frame(Cg_Zg_ord_rp$vectors)
Cg_Zg_rp_vectors <- rownames_to_column(Cg_Zg_rp_vectors, "SampleID")
# join to metadata
Cg_Zg_rp_vectors_metadata <- dplyr::inner_join(Cg_Zg_rp_vectors, Cg_Zg_metadata_rp, by = "SampleID")
# check plot_ordination() figure for axis % explained
plot3 <- plot_ordination(Cgrowth_Zgrowth_rp, Cg_Zg_ord_rp, color ="group") + theme(aspect.ratio=1)
plot3
# plot ordination
Cg_Zg_PCoA_rp <- ggplot(Cg_Zg_rp_vectors_metadata, aes(x = Axis.1, y = Axis.2, color = group, shape = group, size=stage)) + geom_point(alpha=0.75) + theme(aspect.ratio=1) + scale_size_manual(name = "Growth stage", values = c(1, 2, 3, 4, 5, 6, 7), labels = c("V1", "V2","V3","V4","R1","R4", "R7")) + scale_shape_manual(name = "Treatment\nGroup", values = c(16, 17), labels = c("Control-growth", "Zeb-growth")) + scale_color_manual(name = "Treatment\nGroup", values = c("#F8766D", "#00bfc4"), labels = c("Control-growth", "Zeb-growth")) + labs(title= "Rhizoplane - Growth") + xlab("Axis.1 [19.5%]") + ylab("Axis.2 [13.6%]") + my_theme + guides(shape = guide_legend(override.aes = list(size=4)))
Cg_Zg_PCoA_rp
# control_time vs zeb_time
perm <- how(nperm = 9999)
Ct_Zt_metadata_rp <- data.frame(sample_data(Ctime_Ztime_rp))
Ct_Zt_BCdist_rp = vegdist(t(otu_table(Ctime_Ztime_rp)), method = "bray", binary = FALSE)
set.seed(8)
Ct_Zt_BC_adonis_rp <- adonis2(formula = Ct_Zt_BCdist_rp ~ group*stage, permutations = perm, data = Ct_Zt_metadata_rp)
Ct_Zt_BC_adonis_rp
# Df SumOfSqs R2 F Pr(>F)
# group 1 0.4435 0.03382 3.2968 0.0011 **
# stage 6 3.7329 0.28468 4.6248 0.0001 ***
# group:stage 6 1.8065 0.13777 2.2382 0.0001 ***
# Residual 53 7.1297 0.54373
# Total 66 13.1127 1.00000
Ct_Zt_pairwise_rp <- pairwise.adonis2(Ct_Zt_BCdist_rp ~ group_stage, data = Ct_Zt_metadata_rp, permutations = perm)
#Ct_Zt_pairwise_rp
# $Zeb_time_day21_vs_Control_time_day21
# Df SumOfSqs R2 F Pr(>F)
# group_stage 1 0.21636 0.17412 1.4758 0.0318 *
#
# $Zeb_time_day3_vs_Control_time_day3
# Df SumOfSqs R2 F Pr(>F)
# group_stage 1 0.20158 0.15675 1.4871 0.0148 *
#
# $Zeb_time_day63_vs_Control_time_day63
# Df SumOfSqs R2 F Pr(>F)
# group_stage 1 0.32498 0.21318 2.1675 0.0081 **
#
# $Zeb_time_day7_vs_Control_time_day7
# Df SumOfSqs R2 F Pr(>F)
# group_stage 1 0.24231 0.17966 1.7521 0.0391 *
#
# $Control_time_day14_vs_Zeb_time_day14
# Df SumOfSqs R2 F Pr(>F)
# group_stage 1 0.24532 0.24769 2.3047 0.0162 *
#
# $Control_time_day35_vs_Zeb_time_day35
# Df SumOfSqs R2 F Pr(>F)
# group_stage 1 0.43207 0.35432 3.8413 0.015 *
#
# $Control_time_day49_vs_Zeb_time_day49
# Df SumOfSqs R2 F Pr(>F)
# group_stage 1 0.57954 0.32921 3.9261 0.0085 **
# ordinate
Ct_Zt_ord_rp <- ordinate(physeq = Ctime_Ztime_rp, method="PCoA", distance= Ct_Zt_BCdist_rp)
# save vectors
Ct_Zt_rp_vectors <- as.data.frame(Ct_Zt_ord_rp$vectors)
Ct_Zt_rp_vectors <- rownames_to_column(Ct_Zt_rp_vectors, "SampleID")
# join to metadata
Ct_Zt_rp_vectors_metadata <- dplyr::inner_join(Ct_Zt_rp_vectors, Ct_Zt_metadata_rp, by = "SampleID")
# check plot_ordination() figure for axis % explained
plot4 <- plot_ordination(Ctime_Ztime_rp, Ct_Zt_ord_rp, color ="group") + theme(aspect.ratio=1)
plot4
# plot ordination
Ct_Zt_PCoA_rp <- ggplot(Ct_Zt_rp_vectors_metadata, aes(x = Axis.1, y = Axis.2, color = group, shape = group, size=stage)) + geom_point(alpha=0.75) + theme(aspect.ratio=1) + scale_size_manual(name = "Timepoint", values = c(1, 2, 3, 4, 5, 6, 7), labels = c("day3", "day7","day14","day21","day35","day49", "day63")) + scale_shape_manual(name = "Treatment\nGroup", values = c(16, 17), labels = c("Control-time", "Zeb-time")) + scale_color_manual(name = "Treatment\nGroup", values = c("#F8766D", "#00bfc4"), labels = c("Control-time", "Zeb-time")) + labs(title= "Rhizoplane - Time") + xlab("Axis.1 [23.1%]") + ylab("Axis.2 [13.0%]") + my_theme + guides(shape = guide_legend(override.aes = list(size=4)))
Ct_Zt_PCoA_rp
rplane_ords <- (Cg_Zg_PCoA_rp | Ct_Zt_PCoA_rp) + plot_annotation(tag_levels = "A")
#ggsave(plot=rplane_ords, filename = "rplane_ords.png", width=12, height=6, units = "in")
all4_ords <- ((Cg_Zg_PCoA_rs | Ct_Zt_PCoA_rs) / (Cg_Zg_PCoA_rp | Ct_Zt_PCoA_rp)) + plot_annotation(tag_levels = "A")
#ggsave(plot=all4_ords, filename = "all4_ords.png", width=12, height=10, units = "in")
```
# beta dispersion - rhizoplane
Functions for plotting data from betadisper() with ggplot2 were written by Daniel Padfield (https://gist.github.com/padpadpadpad/4201dc530b18a8d36363d37286edfc7c)
```{r betadisper}
# functions for plotting with ggplot ####
# getting distances from betadisper() object
betadisper_distances <- function(model){
temp <- data.frame(group = model$group)
temp2 <- data.frame(distances = unlist(model$distances))
temp2$sample <- row.names(temp2)
temp <- cbind(temp, temp2)
temp <- dplyr::select(temp, group, sample, dplyr::everything())
row.names(temp) <- NULL
return(temp)
}
# getting eigenvalues out of betadisper() object
betadisper_eigenvalue <- function(model){
temp <- data.frame(eig = unlist(model$eig))
temp$PCoA <- row.names(temp)
row.names(temp) <- NULL
return(temp)
}
# getting the eigenvectors out of a betadisper() object
betadisper_eigenvector <- function(model){
temp <- data.frame(group = model$group)
temp2 <- data.frame(unlist(model$vectors))
temp2$sample <- row.names(temp2)
temp <- cbind(temp, temp2)
temp <- dplyr::select(temp, group, sample, dplyr::everything())
row.names(temp) <- NULL
return(temp)
}
# get centroids
betadisper_centroids <- function(model){
temp <- data.frame(unlist(model$centroids))
temp$group <- row.names(temp)
temp <- dplyr::select(temp, group, dplyr::everything())
row.names(temp) <- NULL
return(temp)
}
# betadisper data
get_betadisper_data <- function(model){
temp <- list(distances = betadisper_distances(model),
eigenvalue = betadisper_eigenvalue(model),
eigenvector = betadisper_eigenvector(model),
centroids = betadisper_centroids(model))
return(temp)
}
# dispersion on rhizoplane - growth ####
# Cg_Zg_metadata_rp
# Cg_Zg_BCdist_rp
Cg_Zg_betadisper <- betadisper(Cg_Zg_BCdist_rp, group=as.factor(Cg_Zg_metadata_rp$group_stage), type = "median", bias.adjust=TRUE)
Cg_Zg_betadisper
# Cg_Zg_permute <- permutest(Cg_Zg_betadisper, pairwise=TRUE, permutations=9999)
# Cg_Zg_permute
# No significance between control and zeb at any sample point
# Df Sum Sq Mean Sq F N.Perm Pr(>F)
# Groups 13 0.11083 0.0085251 0.6468 9999 0.8072
boxplot(Cg_Zg_betadisper)
# get betadisper data #
Cg_Zg_betadisper_BDdata <- get_betadisper_data(Cg_Zg_betadisper)
Cg_Zg_betadisper_BDdata$eigenvalue <- mutate(Cg_Zg_betadisper_BDdata$eigenvalue, percent = eig/sum(eig))
Cg_Zg_betadisper_BDdata$chull <- group_by(Cg_Zg_betadisper_BDdata$eigenvector, group) %>%
do(data.frame(PCoA1 = .$PCoA1[c(chull(.$PCoA1, .$PCoA2), chull(.$PCoA1, .$PCoA2)[1])],
PCoA2 = .$PCoA2[c(chull(.$PCoA1, .$PCoA2), chull(.$PCoA1, .$PCoA2)[1])])) %>%
data.frame()
colnames(Cg_Zg_betadisper_BDdata$distances)[2] <- "SampleID"
Cg_Zg_metadata_rp_BDdata <- left_join(Cg_Zg_metadata_rp, Cg_Zg_betadisper_BDdata$distances, by="SampleID")
# Now the dataframes are ready to be customized in ggplot
Cg_Zg_rp_disper_plot <- ggplot(Cg_Zg_metadata_rp_BDdata, aes(x=stage, y=distances, color = treatment)) +
geom_boxplot(aes(x=stage, y=distances)) +
geom_point(aes(shape=treatment), size=3, position = position_dodge(width = 0.75)) +
my_theme + scale_color_manual(name = "Treatment\nGroup", values = c("#F8766D", "#00bfc4"), labels = c("Control", "Zeb")) + scale_shape_manual(name = "Treatment\nGroup", values = c(16, 17), labels = c("Control", "Zeb")) + scale_x_discrete(labels=sampling_order_growth, breaks=waiver()) + ylab('Distance to Spatial Median') + xlab("Growth Stage") + labs(title="Rhizoplane - Growth") + theme(legend.key = element_rect(fill = "white"), legend.position = "none")
Cg_Zg_rp_disper_plot
# dispersion on rhizoplane - time ####
# Ct_Zt_metadata_rp
# Ct_Zt_BCdist_rp
Ct_Zt_betadisper <- betadisper(Ct_Zt_BCdist_rp, group=as.factor(Ct_Zt_metadata_rp$group_stage), type = "median", bias.adjust=TRUE)
Ct_Zt_betadisper
# Ct_Zt_permute <- permutest(Ct_Zt_betadisper, pairwise=TRUE, permutations=9999)
# Ct_Zt_permute
# No significance between control and zeb at any sample point
# Df Sum Sq Mean Sq F N.Perm Pr(>F)
# Groups 13 0.12020 0.0092462 0.8017 9999 0.6512
boxplot(Ct_Zt_betadisper)
# get betadisper data #
Ct_Zt_betadisper_BDdata <- get_betadisper_data(Ct_Zt_betadisper)
Ct_Zt_betadisper_BDdata$eigenvalue <- mutate(Ct_Zt_betadisper_BDdata$eigenvalue, percent = eig/sum(eig))
Ct_Zt_betadisper_BDdata$chull <- group_by(Ct_Zt_betadisper_BDdata$eigenvector, group) %>%
do(data.frame(PCoA1 = .$PCoA1[c(chull(.$PCoA1, .$PCoA2), chull(.$PCoA1, .$PCoA2)[1])],
PCoA2 = .$PCoA2[c(chull(.$PCoA1, .$PCoA2), chull(.$PCoA1, .$PCoA2)[1])])) %>%
data.frame()
colnames(Ct_Zt_betadisper_BDdata$distances)[2] <- "SampleID"
Ct_Zt_metadata_rp_BDdata <- left_join(Ct_Zt_metadata_rp, Ct_Zt_betadisper_BDdata$distances, by="SampleID")
# Now the dataframes are ready to be customized in ggplot
Ct_Zt_rp_disper_plot <- ggplot(Ct_Zt_metadata_rp_BDdata, aes(x=stage, y=distances, color = treatment)) +
geom_boxplot(aes(x=stage, y=distances)) +
geom_point(aes(shape=treatment), size=3, position = position_dodge(width = 0.75)) +
my_theme + scale_color_manual(name = "Treatment\nGroup", values = c("#F8766D", "#00bfc4"), labels = c("Control", "Zeb")) + scale_shape_manual(name = "Treatment\nGroup", values = c(16, 17), labels = c("Control", "Zeb")) + scale_x_discrete(labels=sampling_order_time, breaks=waiver()) + ylab('Distance to Spatial Median') + xlab("Timepoint") + labs(title="Rhizoplane - Time") + theme(legend.key = element_rect(fill = "white"), legend.position = "none")
Ct_Zt_rp_disper_plot
```
# procrustes - rhizoplane
```{r}
# Separate by group
Ctime_rp <- rplane_ps %>% ps_filter(group == "Control_time")
Cgrowth_rp <- rplane_ps %>% ps_filter(group == "Control_growth")
Ztime_rp <- rplane_ps %>% ps_filter(group == "Zeb_time")
Zgrowth_rp <- rplane_ps %>% ps_filter(group == "Zeb_growth")
# need matching sample ids that convey the time point and replicates..... export metadata and try to make new ids?
# write.csv(data.frame(sample_data(Ctime_rp)), "Ctime_rp.csv")
# write.csv(data.frame(sample_data(Cgrowth_rp)), "Cgrowth_rp.csv")
# write.csv(data.frame(sample_data(Ztime_rp)), "Ztime_rp.csv")
# write.csv(data.frame(sample_data(Zgrowth_rp)), "Zgrowth_rp.csv")
# change IDs in OTU table
# write.csv(data.frame(otu_table(Ctime_rp)), "Ctime_rp_otu.csv")
# write.csv(data.frame(otu_table(Cgrowth_rp)), "Cgrowth_rp_otu.csv")
# write.csv(data.frame(otu_table(Ztime_rp)), "Ztime_rp_otu.csv")
# write.csv(data.frame(otu_table(Zgrowth_rp)), "Zgrowth_rp_otu.csv")
# growth
# read in otu tables
Cgrowth_rp_otu_proc <- read.csv("Cgrowth_rp_otu_proc.csv", header=TRUE, row.names = 1)
Zgrowth_rp_otu_proc <- read.csv("Zgrowth_rp_otu_proc.csv", header=TRUE, row.names = 1)
Cgrowth_rp_dist = vegdist(t(Cgrowth_rp_otu_proc), method = "bray")
Zgrowth_rp_dist = vegdist(t(Zgrowth_rp_otu_proc), method = "bray")
Cgrowth_rp_MDS <- monoMDS(Cgrowth_rp_dist,k=3, model="global", pc=TRUE)
Zgrowth_rp_MDS <- monoMDS(Zgrowth_rp_dist,k=3, model="global", pc=TRUE)
plot(Cgrowth_rp_MDS)
plot(Zgrowth_rp_MDS)
growth.proc <- procrustes(X=Cgrowth_rp_MDS, Y=Zgrowth_rp_MDS, scale=TRUE)
summary(growth.proc)
plot(growth.proc)
set.seed(27)
growth.protest <- protest(X=Cgrowth_rp_MDS, Y=Zgrowth_rp_MDS, scores = "sites", permutations = how(nperm = 999))
growth.protest
# Procrustes Sum of Squares (m12 squared): 0.8591
# Correlation in a symmetric Procrustes rotation: 0.3754
# Significance: 0.024 *
plot(growth.protest)
# time
# read in otu tables
Ctime_rp_otu_proc <- read.csv("Ctime_rp_otu_proc.csv", header=TRUE, row.names = 1)
Ztime_rp_otu_proc <- read.csv("Ztime_rp_otu_proc.csv", header=TRUE, row.names = 1)
Ctime_rp_dist = vegdist(t(Ctime_rp_otu_proc), method = "bray")
Ztime_rp_dist = vegdist(t(Ztime_rp_otu_proc), method = "bray")
Ctime_rp_MDS <- monoMDS(Ctime_rp_dist,k=3, model="global", pc=TRUE)
Ztime_rp_MDS <- monoMDS(Ztime_rp_dist,k=3, model="global", pc=TRUE)
plot(Ctime_rp_MDS)
plot(Ztime_rp_MDS)
time.proc <- procrustes(X=Ctime_rp_MDS, Y=Ztime_rp_MDS, scale=TRUE)
summary(time.proc)
plot(time.proc)
set.seed(27)
time.protest <- protest(X=Ctime_rp_MDS, Y=Ztime_rp_MDS, scores = "sites", permutations = how(nperm = 999))
time.protest
# Procrustes Sum of Squares (m12 squared): 0.9856
# Correlation in a symmetric Procrustes rotation: 0.12
# Significance: 0.989
plot(time.protest)
```
# rhizosphere beta diversity
```{r beta_rs}
perm <- how(nperm = 999)
metadata_rs <- data.frame(sample_data(rsphere_ps))
BCdist_rs = vegdist(t(otu_table(rsphere_ps)), method = "bray", binary = FALSE)
set.seed(8)
BC_adonis_rs <- adonis2(formula = BCdist_rs ~ treatment*age_days*stage_all, permutations = perm, data = metadata_rs)
BC_adonis_rs
# adonis2(formula = BCdist_rs ~ treatment * age_days * stage_all, data = metadata_rs, permutations = perm)
# Df SumOfSqs R2 F Pr(>F)
# treatment 1 0.3086 0.01599 2.3790 0.001 ***
# age_days 1 0.7503 0.03887 5.7848 0.001 ***
# stage_all 16 2.8984 0.15015 1.3966 0.001 ***
# treatment:age_days 1 0.2121 0.01099 1.6353 0.010 **
# treatment:stage_all 8 1.1255 0.05830 1.0846 0.134
# Residual 108 14.0084 0.72570
# Total 135 19.3032 1.00000
unique(sample_data(rplane_ps)$stage_all)
g_s_pairwise_rs <- pairwise.adonis2(BCdist_rs ~ group_stage, data = metadata_rs, permutations = perm)
#g_s_pairwise_rs
sample_data(rsphere_ps)$timepoint <- as.factor(sample_data(rsphere_ps)$timepoint)
# ordinate
full_ord_rs <- ordinate(physeq = rsphere_ps, method="PCoA", distance= BCdist_rs)
# plot ordination
full_PCoA_rs <- plot_ordination(rsphere_ps, full_ord_rs, color ="treatment", shape="series") + theme(aspect.ratio=1)
full_PCoA_rs + geom_point(aes(size=timepoint))
# Separate in comparisons that we are interested in
Cgrowth_Zgrowth_rs <- rsphere_ps %>% ps_filter(group != "Control_time") %>% ps_filter(group != "Zeb_time")
Ctime_Ztime_rs <- rsphere_ps %>% ps_filter(group != "Control_growth") %>% ps_filter(group != "Zeb_growth")
control_groups_rs <- rsphere_ps %>% ps_filter(group != "Zeb_growth") %>% ps_filter(group != "Zeb_time")
zeb_groups_rs <- rsphere_ps %>% ps_filter(group != "Control_growth") %>% ps_filter(group != "Control_time")
# test comparisons
# control vs control, should be the same
perm <- how(nperm = 9999)
C_C_metadata_rs <- data.frame(sample_data(control_groups_rs))
C_C_BCdist_rs = vegdist(t(otu_table(control_groups_rs)), method = "bray", binary = FALSE)
set.seed(8)
C_C_BC_adonis_rs <- adonis2(formula = C_C_BCdist_rs ~ group, permutations = perm, data = C_C_metadata_rs)
C_C_BC_adonis_rs
# Df SumOfSqs R2 F Pr(>F)
# group 1 0.1580 0.01909 1.2846 0.0697 .
# zeb vs zeb, should be different?
perm <- how(nperm = 999)
ZZ_metadata_rs <- data.frame(sample_data(zeb_groups_rs))
ZZ_BCdist_rs = vegdist(t(otu_table(zeb_groups_rs)), method = "bray", binary = FALSE)
set.seed(8)
ZZ_BC_adonis_rs <- adonis2(formula = ZZ_BCdist_rs ~ group, permutations = perm, data = ZZ_metadata_rs)
ZZ_BC_adonis_rs
# Df SumOfSqs R2 F Pr(>F)
# group 1 0.1491 0.01917 1.2897 0.038 *
# control_growth vs zeb_growth
perm <- how(nperm = 9999)
Cg_Zg_metadata_rs <- data.frame(sample_data(Cgrowth_Zgrowth_rs))
Cg_Zg_BCdist_rs = vegdist(t(otu_table(Cgrowth_Zgrowth_rs)), method = "bray", binary = FALSE)
set.seed(8)
Cg_Zg_BC_adonis_rs <- adonis2(formula = Cg_Zg_BCdist_rs ~ group*stage, permutations = perm, data = Cg_Zg_metadata_rs)
Cg_Zg_BC_adonis_rs
# Df SumOfSqs R2 F Pr(>F)
# group 1 0.2474 0.03035 2.2850 1e-04 ***
# stage 6 1.1804 0.14481 1.8168 1e-04 ***
# group:stage 6 0.9847 0.12080 1.5157 1e-04 ***
Cg_Zg_pairwise_rs <- pairwise.adonis2(Cg_Zg_BCdist_rs ~ group_stage, data = Cg_Zg_metadata_rs, permutations = perm)
#Cg_Zg_pairwise_rs
# $Control_growth_R1_vs_Zeb_growth_R1
# Df SumOfSqs R2 F Pr(>F)
# group_stage 1 0.16426 0.17631 1.7124 0.0067 **
#
# $Control_growth_V2_vs_Zeb_growth_V2
# Df SumOfSqs R2 F Pr(>F)
# group_stage 1 0.12958 0.13639 1.1055 0.2226
#
# $Control_growth_V3_vs_Zeb_growth_V3
# Df SumOfSqs R2 F Pr(>F)
# group_stage 1 0.20019 0.18365 1.7998 0.0092 **
#
# $Zeb_growth_R4_vs_Control_growth_R4
# Df SumOfSqs R2 F Pr(>F)
# group_stage 1 0.17196 0.15186 1.4324 0.018 *
#
# $Zeb_growth_R7_vs_Control_growth_R7
# Df SumOfSqs R2 F Pr(>F)
# group_stage 1 0.26716 0.24428 2.5859 0.0084 **
#
# $Zeb_growth_V1_vs_Control_growth_V1
# Df SumOfSqs R2 F Pr(>F)
# group_stage 1 0.13643 0.16989 1.2279 0.1115
#
# $Control_growth_V4_vs_Zeb_growth_V4
# Df SumOfSqs R2 F Pr(>F)
# group_stage 1 0.16125 0.16639 1.5968 0.0077 **
Cg_Zg_grouppairwise_rs <- pairwise.adonis2(Cg_Zg_BCdist_rs ~ group, data = Cg_Zg_metadata_rs, permutations = perm)
#Cg_Zg_grouppairwise_rs
# ordinate
Cg_Zg_ord_rs <- ordinate(physeq = Cgrowth_Zgrowth_rs, method="PCoA", distance= Cg_Zg_BCdist_rs)
# save vectors
Cg_Zg_rs_vectors <- as.data.frame(Cg_Zg_ord_rs$vectors)
Cg_Zg_rs_vectors <- rownames_to_column(Cg_Zg_rs_vectors, "SampleID")
# join to metadata
Cg_Zg_rs_vectors_metadata <- dplyr::inner_join(Cg_Zg_rs_vectors, Cg_Zg_metadata_rs, by = "SampleID")
# check plot_ordination() figure for axis % explained
plot1 <- plot_ordination(Cgrowth_Zgrowth_rs, Cg_Zg_ord_rs, color ="group") + theme(aspect.ratio=1)
plot1
# plot ordination
Cg_Zg_PCoA_rs <- ggplot(Cg_Zg_rs_vectors_metadata, aes(x = Axis.1, y = Axis.2, color = group, shape = group, size=stage)) + geom_point(alpha=0.75) + theme(aspect.ratio=1) + scale_size_manual(name = "Growth stage", values = c(1, 2, 3, 4, 5, 6, 7), labels = c("V1", "V2","V3","V4","R1","R4", "R7")) + scale_shape_manual(name = "Treatment\nGroup", values = c(16, 17), labels = c("Control-growth", "Zeb-growth")) + scale_color_manual(name = "Treatment\nGroup", values = c("#F8766D", "#00bfc4"), labels = c("Control-growth", "Zeb-growth")) + labs(title= "Rhizosphere - Growth") + xlab("Axis.1 [9.5%]") + ylab("Axis.2 [5.4%]") + my_theme + guides(shape = guide_legend(override.aes = list(size=4)))
Cg_Zg_PCoA_rs
# control_time vs zeb_time
perm <- how(nperm = 9999)
Ct_Zt_metadata_rs <- data.frame(sample_data(Ctime_Ztime_rs))
Ct_Zt_BCdist_rs = vegdist(t(otu_table(Ctime_Ztime_rs)), method = "bray", binary = FALSE)
set.seed(8)
Ct_Zt_BC_adonis_rs <- adonis2(formula = Ct_Zt_BCdist_rs ~ group*stage, permutations = perm, data = Ct_Zt_metadata_rs)
Ct_Zt_BC_adonis_rs
# Df SumOfSqs R2 F Pr(>F)
# group 1 0.1932 0.02404 1.7894 4e-04 ***
# stage 6 1.0950 0.13619 1.6899 1e-04 ***
# group:stage 6 0.8122 0.10102 1.2535 4e-04 ***
Ct_Zt_pairwise_rs <- pairwise.adonis2(Ct_Zt_BCdist_rs ~ group_stage, data = Ct_Zt_metadata_rs, permutations = perm)
#Ct_Zt_pairwise_rs
# $Control_time_day14_vs_Zeb_time_day14
# Df SumOfSqs R2 F Pr(>F)
# group_stage 1 0.12559 0.13481 1.2466 0.0298 *
#
# $Control_time_day21_vs_Zeb_time_day21
# Df SumOfSqs R2 F Pr(>F)
# group_stage 1 0.15609 0.14827 1.3926 0.0069 **
#
# $Control_time_day35_vs_Zeb_time_day35
# Df SumOfSqs R2 F Pr(>F)
# group_stage 1 0.16751 0.16108 1.5361 0.0083 **
#
# $Control_time_day63_vs_Zeb_time_day63
# Df SumOfSqs R2 F Pr(>F)
# group_stage 1 0.19904 0.16579 1.5899 0.0086 **
#
# $Zeb_time_day3_vs_Control_time_day3
# Df SumOfSqs R2 F Pr(>F)
# group_stage 1 0.10228 0.11854 1.0758 0.324
#
# $Zeb_time_day7_vs_Control_time_day7
# Df SumOfSqs R2 F Pr(>F)
# group_stage 1 0.08906 0.10415 0.93 0.7113
#
# $Control_time_day49_vs_Zeb_time_day49
# Df SumOfSqs R2 F Pr(>F)
# group_stage 1 0.16252 0.1627 1.3602 0.0423 *
# ordinate
Ct_Zt_ord_rs <- ordinate(physeq = Ctime_Ztime_rs, method="PCoA", distance= Ct_Zt_BCdist_rs)
# save vectors