-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2018_analysis.Rmd
More file actions
1707 lines (1442 loc) · 70.8 KB
/
Copy path2018_analysis.Rmd
File metadata and controls
1707 lines (1442 loc) · 70.8 KB
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: "Chaper 2 Analysis"
author: "Ricardo Silva"
date: "19/10/2020"
output: html_document
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,
eval=FALSE,
message=FALSE,
warning=FALSE)
```
```{r CLEAR EVERYTHING, eval=FALSE, include=FALSE}
# unload all non-base packages
lapply(names(sessionInfo()$otherPkgs), function(pkgs)
detach(
paste0('package:', pkgs),
character.only = T,
unload = T,
force = T
))
rm(list=ls())
```
## Aims
Coastal environments are facing fast changes around the world and the knowledge on how microbial communities are be affected still in debate. There is lack of understanding on how microbial communities respond to nutrient gradients and human activity in Macquarie Harbour. This research attempted to improve the knowledge on it by exploring the relationships among, microbial diversity and networks, and nutrient gradients. The main focus of this study is to analyze the spatial variation and environmental parameters that drive microbial communities during the late spring of 2018.
This study seeks to provide more insights on the microbial community of the Macquarie Harbour. We explored how communities change along different habitats and between the surface and intermediate waters (below the surface where depth varied between 4.46 and 11.80 meters) of the harbor. We examined how they differentiate along an environmental gradient from the river to the ocean. Also, we analyzed the relationship among microbial communities and changing environmental conditions due to aquaculture activities. It was assumed that microbial community would be associated to among and within the habitats analyzed (ocean, Macquarie Harbour and river). Also, the environmental parameters changed by the fish farms within Macquarie Harbour would decouple the relationship between microbial community and Macquarie Harbour surface and subsurface with respect to natural local factors driving the community structure. For that, 16S and 18S rRNA genes high-throughput sequences and microbial network analyses were used to identify variations in key microbial taxa related to changes in environmental conditions.
# Packages and initial steps
```{r packages and functions}
# dir.create("~/R/git_hub/MH-2018/output")
# set working directory and seed
setwd("~/R/git_hub/MH_2018/data")
folder_path <- "~/R/git_hub/MH_2018/output/"
seed = 1234
# load the packages
# main packs to start with
# BiocManager::install("ade4") # -> to get packages from bioconductor website
pkgs <- c("phyloseq", "microbiome", "vegan",'egg','adespatial', 'corrplot','RColorBrewer','tidymodels','corrr','GGally','patchwork',"circlize","multcomp",
'tidytext','hrbrthemes','embed', 'pvclust','skimr', 'RColorBrewer', 'grid',"tidyverse") # Load packages
invisible(sapply(pkgs,require, character = TRUE))
select <- dplyr::select # because MASS conflict
packageVersion("tidyverse")
source('~/R/git_hub/MH_2018/scripts/my_functions.R')
source('~/R/git_hub/MH_2018/scripts/theme_publication.R')
theme_set(theme_Publication_3())
# set colors
cols_site_layer <- c("ocean_S" = "blue", "C_S" = "darkgoldenrod3", "C_P" = "darkgreen", "B_S" = "orange2", 'B_P' = 'cyan4', 'A_S' = 'orange4', 'A_P' = 'darkseagreen4', 'GR_S' = 'firebrick3')
cols_layer <- c('S'='chocolate4','P'='darkcyan')
cols_hab_layer <- c('ocean_S' = 'blue', 'MH_S' = 'chocolate', 'MH_P' = 'darkseagreen4' , 'GR_S' = 'firebrick3' )
# check the working directory
current_directory <- getwd()
paste("Working in directory:", current_directory)
```
```{r}
# all the pseq lists
# pseq.list, pseq.clr.list, pseq.filt.list, pseq.rel.filt.list
#load("pseq.lists.RData")
```
# Data preparation
```{r prep data}
# get and prepare the data
# getting the data and filtering
# OTU table
OTUdf <- read.csv("OTU_all_2018.csv", row.names = 1, head = TRUE)
# taxonomy table
TAXdf <- read.csv("taxa_all_2018.csv", row.names = 1, head = TRUE)
# metadata table
meta <- read.csv("meta_2018.csv", row.names = 1)
```
```{r prep data - OTU table}
# check OTU table
str(OTUdf)
names(OTUdf)[1:5]
# let's remove extra text around the table
names(OTUdf) <- str_replace_all(names(OTUdf), "_S*[0-9]+",'')
# names(OTUdf) <- gsub("_S*[0-9]+",'',names(OTUdf))
row.names(OTUdf)[1:5]
row.names(OTUdf) <- row.names(OTUdf) %>% str_replace_all("MH18_", "")
# row.names(OTUdf) <- gsub("MH18_", "", row.names(OTUdf))
OTUdf <- OTUdf %>% dplyr::select(-MH063SR, -MH028SC) # remove replicate samples
```
```{r prep data - tax table}
# check if there is any NA in the taxa table
str(TAXdf)
sum(is.na(TAXdf))
TAXdf %>% map(~sum(is.na(.))) %>% as.data.frame()
apply(TAXdf, 2, function(x) any(is.na(x)))
# remove NAs in Kingdom and Phylum
TAXdf <- TAXdf %>% drop_na(Kingdom, Phylum)
# replace NA by unknown
TAXdf <- TAXdf %>% replace(is.na(.), "unknown")
# remove extra text
TAXdf <- TAXdf %>%
#rownames_to_column("OTU") %>%
mutate(OTU = str_replace(OTU, "MH18_", "")) %>%
# change Family Mitochondria in the bacterial order Rickettsiales to o_Rickettsiales
mutate(Family = case_when(Family == "Mitochondria" ~ "o_Rickettsiales",
TRUE ~ as.character(Family)))
TAXdf %>% filter(Family == "o_Rickettsiales")
# row.names(TAXdf) <- gsub("MH18_", "", row.names(TAXdf))
TAX.table <- TAXdf # generate another table with an OTU column to use later
# new tax table does not have the same OTUs. Subsetting OTU table
OTUdf <- OTUdf %>% rownames_to_column('OTU') %>% filter(OTU %in% TAXdf$OTU)
TAXdf <- TAXdf %>% filter(OTU %in% OTUdf$OTU)
names <- TAXdf$OTU
OTUdf <- OTUdf %>% arrange(match(OTU, names)) %>% column_to_rownames('OTU')
TAXdf <- TAXdf %>% select(-seq) %>% column_to_rownames("OTU")
```
```{r prep data - meta table}
# check and change metadata
str(meta)
meta <- meta %>%
unite("Sample_ID", c('Sample_ID', 'Site','Habitat',"Layer"), remove = FALSE, sep='_') %>%
unite("Site_Layer", c('Site', 'Layer'), remove = FALSE, sep='_') %>%
unite('Habitat_Layer', c('Habitat', 'Layer'), remove = FALSE, sep='_') %>%
rename(Temperature = T)
meta <- meta %>% mutate(Sample_ID = str_replace(Sample_ID, '_GR_GR', "")) %>%
mutate(Sample_ID = str_replace(Sample_ID, '_ocean_ocean',""))
# make categories according minimum distance from the cage
# meta$DistCat <- cut(abs(meta$Min.distance_cage), c(0,1,2,4,6,Inf),c("0-1","1-2","2-4","4-6","6-15"), include.lowest=T)
meta <- meta %>% dplyr::filter(Short_ID !='MH063S-R' & Short_ID != 'MH028S-C')
meta <- meta %>%
mutate(DistCat=cut(Min.distance_cage, breaks=c(0,1,2,4,6,Inf), labels=c("0-1","1-2","2-4","4-6","6-15"))) %>%
mutate_if(is.character, as.factor) # convert all character columns to factor
# skimr::skim(meta)
# make another column uniting Site and ID
# meta <- meta %>% unite("ID", c('Site', 'Short_ID'), remove = FALSE, sep='_')
rownames(meta) <- meta$Sample_ID
names(OTUdf) <- row.names(meta)
meta.NA <- names(meta)[apply(meta,2,function(x) any(is.na(x)))] # vector with columns that contain NAs
# "pH" "Turbidity" "DO_perc" "DO"
# order layers
meta <- meta %>% mutate(Layer = fct_relevel(Layer, c("S", "P")))
meta %>% select(Layer, Short_ID, Habitat, depth, Temperature, Sal, Chlorophyll, Nox:NH4) %>% rownames_to_column("Sample_ID") %>% write_csv(paste0(folder_path, "meta_publication.csv"))
```
```{r prep data - phyloseq objects}
# phyloseq object
pseq.all <- phyloseq(otu_table(as.matrix(as.data.frame(OTUdf)), taxa_are_rows = TRUE),
tax_table(as.matrix(TAXdf)),sample_data(meta))
pseq.all
# Bacteria
pseq.bac <- subset_taxa(pseq.all, (Kingdom %in% c("Bacteria")))
# Archaea
pseq.arc <- subset_taxa(pseq.all, (Kingdom %in% c("Archaea")))
# Eukaryota
pseq.euk <- subset_taxa(pseq.all, (Kingdom %in% c("Eukaryota")))
# make a list of these phyloseq objects
pseq.list <- list(pseq.arc,pseq.bac,pseq.euk)
names(pseq.list) <- c('Archaea', 'Bacteria', 'Eukaryotes')
# pseq_df_bac <- microbiomeutilities::phy_to_ldf(pseq.list{{2}}, transform.counts = NULL) # pseq to long format
```
```{r data prep}
# filtering data and clr-transformation
min.prop=0.001 # minimum proportion in any sample (default)
min.occurrence=0.001 # minimum occurrence across all samples (sparsity filter)
# only surface and without outliers
pseq.filt.list <- list()
pseq.clr.list <- list()
pseq.rel.filt.list <- list()
for (i in seq_along(pseq.list)) {
# filtering and clr-transformation
d.filt0 <- CoDaSeq::codaSeq.filter(otu_table(pseq.list[[i]]), min.prop=0, min.occurrence=0, samples.by.row=FALSE)
d.filt <- CoDaSeq::codaSeq.filter(d.filt0, min.prop=min.prop, max.prop=1, min.occurrence=min.occurrence, samples.by.row=FALSE)
d.zero.hand <- t(zCompositions::cmultRepl(t(d.filt), label =0, method="CZM")) # filtered
d.clr.abund.filt <- CoDaSeq::codaSeq.clr(d.zero.hand, samples.by.row = FALSE) # samples as COLUMN
# creatin a TAX table for the phyloseq object
TAX.filt <- base::subset(TAXdf, row.names(TAXdf) %in% row.names(d.filt))
# create a phyloseq object with clr transformed data
df.clr <- phyloseq(otu_table(as.matrix(d.clr.abund.filt), taxa_are_rows = TRUE),
tax_table(as.matrix(TAX.filt)),sample_data(as.data.frame(sample_data(pseq.list[[i]]))))
# phyloseq with only filtered data
df.filt <- phyloseq(otu_table(as.matrix(as.data.frame(d.filt)), taxa_are_rows = TRUE),
tax_table(as.matrix(TAX.filt)), sample_data(as.data.frame(sample_data(pseq.list[[i]]))))
# transform relative
df.rel <- microbiome::transform(df.filt, 'compositional') # relative abundance
# naming
name <- paste(names(pseq.list[i]))
# put in a list
pseq.clr.list[[name]] <- df.clr
pseq.filt.list[[name]] <- df.filt
pseq.rel.filt.list[[name]] <- df.rel
# clean the env
rm(d.filt0,d.filt, d.zero.hand, d.clr.abund.filt, TAX.filt, df.clr, df.filt, df.rel)
}
# checking
table(tax_table(pseq.list[[2]])[,"Phylum"])
table(tax_table(pseq.rel.filt.list[[2]])[,"Phylum"])
sample_data(pseq.filt.list[[2]]) %>% row.names()
save(pseq.list, pseq.clr.list, pseq.filt.list, pseq.rel.filt.list, file= "pseq.lists.RData")
# load("pseq.lists.RData")
# total zOTUs
pseq.list
pseq.filt.list
# total reads
for (i in 1:3) {
t.reads.all <- tibble(as(sample_data(pseq.list[[i]]), "data.frame"),
TotalReads = sample_sums(pseq.list[[i]])) %>%
summarise(sum(TotalReads))
t.reads.filt <- tibble(as(sample_data(pseq.filt.list[[i]]), "data.frame"),
TotalReads = sample_sums(pseq.filt.list[[i]])) %>%
summarise(sum(TotalReads))
print(paste(round(((t.reads.all - t.reads.filt)/t.reads.all)*100, 2), "%", names(pseq.list[i])))
}
```
## Methods
### Map - Sampling Sites
```{r sampling map}
# make a SpatialPolygons object which defines a bounding box inside which to crop the world map polygons.
# make the bounding box called clipper_MH
clipper_MH <- as(extent(145, 145.7, -42.55, -42.1), "SpatialPolygons") # (min_longitude, max_longitude, min_latitude, max_latitude)
# get the shapefile (downloaded from Tas gov web site)
# read the shapefile with readOGR()
# dsn = file location, layer = name of the file
shpdata_tas <- readOGR(dsn = "tasstatepolygon", layer = "TAS_STATE_POLYGON_shp")
# check if the shapefile has the right Co-ordinate Reference System (CRS)
proj4string(shpdata_tas)
# transform the CRS using spTransform and specify the correct CRS (in this case is EPSG:WGS84 (+proj=longlat +datum=WGS84))
shpdata_tas <- spTransform(shpdata_tas, CRS("+proj=longlat +ellps=GRS80"))
# crop the object to the extent of a bounding box, using intersect():
shpdata_MH_clip <- raster::intersect(shpdata_tas, clipper_MH)
# check if the crop was ok
plot(shpdata_MH_clip)
str(shpdata_MH_clip@data)
# transform spatial object in dataframe
shpdata_MH_clip_f <- fortify(shpdata_MH_clip)
MH_map <- ggplot() +
geom_polygon(data = shpdata_MH_clip_f,
aes(x=long,y=lat,group=group),
fill = 'gray87', color = 'black') +
geom_point(data = meta_surf,
aes(x = long, y = lat),
shape = 21, size = 2, color = 'black') +
xlab("Longitude") +
ylab("Latitude") +
coord_quickmap() + # Prevents stretching when resizing
theme(legend.justification=c(0,0), legend.position=c(0,0)) +
guides(fill = guide_legend(title = 'Locations')) +
labs(title = "All Samples") +
scale_fill_discrete(breaks=order) +
theme_Publication_3()
MH_map <- ggplot() +
geom_polygon(data = shpdata_MH_clip_f,
aes(x=long,y=lat,group=group),
fill = 'gray87', color = 'black') +
geom_point(data = locations_df, aes(x = Long, y = Lat, color = Habitat, fill = Habitat), shape = 21, size = 2, color = 'black') +
egg::theme_article() +
xlab("Longitude") +
ylab("Latitude") +
coord_quickmap() + # Prevents stretching when resizing
theme(legend.justification=c(0,0), legend.position=c(0,0)) +
guides(fill = guide_legend(title = 'Locations')) +
labs(title = "All Samples") +
scale_fill_discrete(breaks=order)
# add a scale bar in the plot
map_MH_scale <- MH_map +
scalebar(data = shpdata_MH_clip_f,
transform = TRUE, dist = 4, dist_unit = "km", model='GRS80',
height = 0.01, location = "bottomright", st.size = 3, anchor = c(x = 145.65, y= -42.52))
# add north arrow
logo <- north2(map_MH_scale, x = 0.67, y = 0.23, scale = 0.1, symbol = 1)
# inset plots using cowplot package
MH_map <- ggdraw(map_MH_scale) + draw_plot(tas_map_annot,.546, .653, .3, .3) +
draw_plot(logo, .625,.165,.1,.1)
```
### Environmental Factors Analysis
To avoid collinearity: Analyze correlation among environmental parameters. If there is, combine values into one parameter for downstream analysis. (r > |0.7|, P ≤ 0.001). Before the PCA, data were log transformed if the distribution was skewed.
PCA: prior to PCA transform the data.
PERMANOVA: to determine the influences of regions and layers (both surface and intermediate waters)
```{r env factors summary tables}
# table env par
df.env <- meta %>% select(Temperature:NH4 ,-pH, -Turbidity, -DO, -DO_perc)
write.csv(df.env, "~R/git_hub/MH_2018/output/data/all/env/df_env.csv")
# Summary
# Environmnental data (surface samples have NAs for pH, Turbidity and DO, DO_perc)
meta_long <- meta %>%
select(Site_Layer,Temperature:NH4 ,-pH, -Turbidity, -DO, -DO_perc) %>%
pivot_longer(names_to="env_par", values_to="value", -Site_Layer)
meta_summary.site <- meta_long %>% group_by(Site_Layer, env_par) %>%
summarise(mean= mean(value), sd= sd(value), max = max(value),min = min(value))
meta_summary.site1 <- meta %>%
select(Site_Layer,Temperature:NH4 ,-pH, -Turbidity, -DO, -DO_perc) %>%
group_by(Site_Layer) %>%
dplyr::summarise(across(where(is.numeric), list(mean= mean, sd = stats::sd, max = base::max, min = base::min)))
# last format on excel
write.csv(meta_summary.site, "~R/git_hub/MH_2018/output/data/all/env/meta_summary_site.csv")
```
```{r env factors stats}
# making summary tables (ANOVA and Tukey)
stats.env <- meta_long %>%
nest(data = c(Site_Layer, value)) %>%
mutate(model = purrr::map(data, ~anova(lm(value ~ Site_Layer, .))),
tidy = purrr::map(model, broom::tidy)) %>%
select(env_par, tidy) %>%
unnest(tidy)
write.csv(stats.env, "~R/git_hub/MH_2018/output/data/all/env/anova_env_summary.csv")
# use autoplot() for Diagnostic plots
# Compute a linear model
m <- lm(Temperature ~ Site_Layer, data = meta)
# Create the plot
autoplot(m, which = 1:6, ncol = 2, label.size = 3, colour = "Site_Layer")
# Tukey Bonferroni adjustments (P-values obtained by number of comparisons)
# #library(multcomp)
stats.env.Tukey.BF <- meta_long %>%
nest(data = c(Site_Layer, value)) %>%
mutate(model = purrr::map(data, ~ multcomp::glht(aov(value ~ Site_Layer, .), linfct = multcomp::mcp(Site_Layer = "Tukey"), test = multcomp::adjusted(type = "bonferroni"))),
tidy = purrr::map(model, broom::tidy)) %>%
dplyr::select(env_par, tidy) %>%
unnest(tidy)
write.csv(stats.env.Tukey.BF, "~R/git_hub/MH_2018/output/data/all/env/anova_env_summary_tukey_BF.csv")
# only sig
env.sig.tukey.BF <- stats.env.Tukey.BF %>% filter(adj.p.value < 0.001) write.csv(env.sig.tukey.BF, "~R/git_hub/MH_2018/output/data/all/env/anova_env_summary_tukey_sig_BF.csv")
```
```{r env factors plots}
# lines plots - both layers
env_line_plot <- meta %>%
select(Site_Layer,Layer,Temperature:NH4 ,-pH, -Turbidity, -DO, -DO_perc) %>%
pivot_longer(names_to="env_par", values_to="value", -c('Site_Layer','Layer')) %>%
group_by(Layer, Site_Layer, env_par) %>%
summarise(mean= mean(value), sd= sd(value), max = max(value),min = min(value)) %>%
ggplot(aes(x= Site_Layer, y = mean, color = Site_Layer, group = interaction(env_par, Layer))) +
geom_errorbar(aes(ymin = mean - sd, ymax = mean +sd), width = .1) +
geom_line(color = 'darkgray', alpha = 0.6) +
geom_point(alpha = 0.6) +
facet_wrap(~env_par, scales = 'free_y') +
theme_bw() +
scale_x_discrete(limits = c('ocean_S','C_S','C_P', 'B_S','B_P','A_S','A_P','GR_S')) +
scale_color_manual(values = cols_site_layer) +
theme_plex()+
theme_Publication_3() +
labs(title="Environmental Parameters", x = "Sites_Layer (Layer: S = surface; P = intermediate layer)") +
theme(legend.position = "none") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
# boxplot
boxplot_env <- meta %>%
select(Layer,Site,Temperature:NH4,-pH, -Turbidity, -DO, -DO_perc) %>%
mutate(Layer = fct_relevel(Layer, c("S", "P"))) %>%
pivot_longer(names_to="env_par", values_to="value", -c('Layer','Site')) %>%
group_by(Layer, Site) %>%
ggplot(aes(x=Site, y=value, color = Layer)) +
geom_boxplot() +
scale_color_manual(values = cols_layer) +
geom_jitter(alpha=0.2, position = position_jitterdodge())+
geom_point(size=1,alpha=0.2)+
facet_wrap(~env_par, scales = "free_y") +
theme(strip.background = element_rect(fill = "white")) +
scale_x_discrete(limits = c("ocean", 'C', 'B', 'A','GR')) +
labs(title = "Environmental Parameters - Surface vs Intermediate Layers", x = "Sites", y = 'Observed Values') +
theme_Publication_3() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
```
```{r env factor correlation}
# check to see if any variables are autocorrelated
meta.cor <- meta %>% select(Temperature:NH4, -pH, -Turbidity, -DO, -DO_perc)
# check the normality
for (i in 1:8){print(shapiro.test(meta.cor[,i]))} # if p-vlaue > 0.05 we can assume the normality.
# using tidymodels
cor.tidy <- meta.cor %>% # Create correlation data frame (cor_df).
correlate(method = 'spearman') %>% # => Spearman for non-normal distributions
rearrange() %>% # rearrange by correlations
shave() # Shave off the upper triangle for a clean result
cor.tidy %>% stretch() %>% filter(r > .85) # filtering (test for autocorrelation between variables)
# cor.tidy %>% fashion() # fashion way
# plot network
meta.cor %>%
correlate(method = 'spearman') %>%
network_plot(min_cor = .2, colors = c('darkorange', 'white', 'darkcyan'))
# using GGally
meta %>%
#drop_na() %>%
select(Habitat, Temperature, Sal, Chlorophyll, Nox:NH4) %>%
# mutate_if(is.numeric, log1p) %>%
mutate(Habitat = recode(Habitat, HG = 'MH')) %>%
ggpairs(mapping = aes(color = Habitat),
upper = list(
continuous = wrap('cor', method = "spearman")
),
lower = list(
continuous = "smooth",
continuous = wrap("points", alpha = 0.3))
)
# plot correlogram
meta.cor %>%
cor(method = "spearman") %>%
corrplot(type = 'upper', order = 'hclust', col = brewer.pal(n=8, name= "RdYlBu"))
```
```{r env factors - PCA}
# prepare the data for PCA
metaPCA <- meta %>%
select(-DO, -DO_perc, -Nox, -NO2) %>% # remove the columns that are autocorrelated (Salinity with Nox and NH4 with NO2)
select_if(~!any(is.na(.))) %>% # select columns without NAs
select(Sample_ID, Site_Layer, Site, Layer, Habitat_Layer, Habitat, Temperature:NH4) # select env factors and Site names for the PCA
# 1 - recipe - no values computed
# Write recipe for PCA
pca_rec <- recipe(~., data = metaPCA) %>% # ~. because it is unsupervised
# Specify character_code as key/id column
update_role(Sample_ID, Site_Layer, Site, Layer, Habitat_Layer, Habitat, new_role = 'id') %>%
# normalize the data (center and scale all predictors (mean to zero and standard deviation of one)
step_normalize(all_predictors()) %>%
# PCA is done here
# can use threshold to specify if we want to capture 90% of variance in the data (threshold = 0.9)
step_pca(all_predictors(), id = "pca") # for the bar plot showing how much variance each component accounts for
# 2 prep the recipe - values computed (prep() implements the recipe)
pca_prep <- prep(pca_rec)
# variance bar plot (variance explained)
total_variance_plot <- pca_prep %>%
tidy(id = 'pca', type = 'variance') %>%
dplyr::filter(terms == 'percent variance') %>%
ggplot(aes(x = component, y = value)) +
geom_col(fill = "lightblue") +
# xlim(c(0,5))+
labs(y = '% of total variance')
# most contributing variables (PCA loadings)
tidied_pca <- tidy(pca_prep, 2) # gives for every PC computed the value for each term(predictor) = Scores
# it can be notice that Nox and NH4 in the first component are closer but in opposite direction which means
# that they tend to not occur in the same sample.
# Opposite direction means that the predictors tend to occur separately and vice versa.
pred_pca_env_factors <- tidied_pca %>%
#filter(component %in% paste0('PC',1:4)) %>% # first three components
group_by(component) %>%
#top_n(8, abs(value)) %>%
ungroup() %>%
mutate(terms = reorder_within(terms, abs(value), component)) %>%
ggplot(aes(abs(value), terms, fill = value > 0 )) +
geom_col() +
tidytext::scale_y_reordered() +
scale_fill_manual(values = c("#b6dfe2", "#0A537D")) +
facet_wrap(~component, scales = "free_y") +
labs(y = NULL, x = "Absolute values of contribution", fill = 'Positive?') +
theme_plex()
# for visualization = juice
pca.plot <- juice(pca_prep) %>% # juice() to return the results of a recipe
mutate(Layer = fct_relevel(Layer, c("S", "P"))) %>%
ggplot(aes(PC1,PC2)) +
geom_point(aes(color = Site_Layer), alpha = 0.7, size =5) +
#geom_text(check_overlap = TRUE, hjust = 'inward', family = 'IBM Plex Sans') +
geom_hline(yintercept=0, linetype="dashed", alpha = 0.3) +
geom_vline(xintercept=0, linetype="dashed", alpha = 0.3) +
# scale_colour_viridis_d(option = "plasma")
scale_color_manual(values = cols_site_layer) +
theme_Publication_3()
# get % variation to add in the legend axis
var_explained <- pca_prep %>%
tidy(id = 'pca', type = 'variance') %>%
dplyr::filter(terms == 'percent variance') %>%
pull(value)
pca.plot$labels$x <- paste0(pca.plot$labels$x," ", round(var_explained[1],2),"%")
pca.plot$labels$y <- paste0(pca.plot$labels$y," ", round(var_explained[2],2),"%")
# PCA loading and Scores
# get pca loadings into a wider format
pca_wider <- tidied_pca %>%
pivot_wider(names_from = component, id_cols = terms)
# define arrow style
arrow_style <- arrow(length = unit(.05, "inches"),
type = "closed")
pca.plot <- pca.plot +
geom_segment(data = pca_wider,
aes(xend = PC1*2, yend = PC2*2),
x = 0, y = 0,
arrow = arrow_style) +
geom_text(data = pca_wider,
aes(x = PC1*2.1, y = PC2*2.1, label = terms),
hjust = 0,
vjust = 1,
size = 5,
color = '#0A537D')
pca.plot
```
```{r env factors - PCA with kmean}
# get PCA socres
scores.pca <- juice(pca_prep) %>% select(-Site,-Site_Layer ,-Layer, -Habitat_Layer, -Habitat) %>% column_to_rownames(var = 'Sample_ID')
# k-means clustering [assume 3 clusters]
km <- kmeans(scores.pca, centers= 4, nstart=5)
# add ellipses to PCA plot
k.env <- factor(km$cluster)
pca.env <- pca.plot +
stat_ellipse(aes(x=PC1,y=PC2,group=k.env),
level=0.95, alpha=0.7, type = "norm", linetype = 2) +
ggtitle("PCA: Environmental variables")
pca.env %>% ggsave(filename = "~R/git_hub/MH_2018/output/plots/figures/pca_env.tiff", compression = "lzw")
```
Comparison of clusters and habitat_layer shows that samples from each layer and habitat are perfectly separated, indicating that they have clear differences regarding physicochemical characteristics.
```{r env factors - Permanova}
# euclidean distance matrix of env fators
env.fact <- metaPCA %>% select(Temperature:NH4) %>% vegdist(method="euclidean")
# Adonis test - partitioning the euclidean distance matrix by Layer and site
perm.df <- vegan::adonis(env.fact ~ Layer*Site, permutations = 9999, pairwise = TRUE, method = 'euclidean', data = metaPCA)
# maka a table to be saved as csv file
adonis.tb <- perm.df$aov.tab %>% as.data.frame() %>% drop_na()
colnames(adonis.tb)[6] <- 'p.value'
row.names(adonis.tb)[1] <-'PERMANOVA'
adonis.tb <- adonis.tb %>% rownames_to_column(var = 'pairs') %>% select(-MeanSqs)
adonis.tb$p.adjusted <- NA
# Pairwise Adonis test - partitioning the euclidean distance matrix by Habitat_Layer
pair.df <- pairwiseAdonis::pairwise.adonis(env.fact, metaPCA$Habitat_Layer, perm = 9999, p.adjust.m = "bonferroni")
# filter and join with permanova output
pair.tb <- pair.df %>% select(-sig) %>% filter(p.adjusted < 0.01)
perm.tb <- rbind(adonis.tb, pair.tb)
# save as csv
write.csv(perm.tb, "~R/git_hub/MH_2018/output/data/all/permanova_env.csv")
```
```{r clean R env}
# keep the environment tidy
rm(list=setdiff(ls(), c('seed',"meta","OTUdf",'pseq.all', 'pseq.list', 'TAXdf', 'TAX.table','cols_site_layer', 'cols_layer',"cols_hab_layer","pca.env", "folder_path")))
source('~/R/phd/2018/script/chapter2/my_functions.R')
source('~/R/phd/2018/script/chapter2/theme_publication.R')
select <- dplyr::select # because MASS conflict
```
### Alpha Diversity
Alpha diversity analyses were performed to explore variation in OTU richness and evenness among habitats and waters masses in the harbor. Prior to analysis samples were rarified to the lowest sequence abundance for any one sample. Chao1 and non-parametric Shannon diversity were calculated.
Linear models and ANOVAs were used to test for variation in alpha diversity among habitats. For Macquarie Harbor samples, linear mixed-effect models and ANOVAs were used to test influences of transects, distance from the cage, and water masses on alpha diversity, controlling for habitats as a random effect.
If significant variation was found within an ANOVA result, post-hoc comparisons were implemented between sample groups using Tukey’s Honest Significant Differences (HSD) tests with Bonferroni adjustments (P-values obtained by number of comparisons) for pairwise comparisons.
```{r alpha div data prep}
# show variable sampling depth for each kingdom
for(i in 1:length(pseq.list)){
print(names(pseq.list[i]))
print(summary(sample_sums(pseq.list[[i]])))
}
# plot species accumulation curves for each sample
# set step at a high number to speed up plotting
for(i in 1:length(pseq.list)){
# set step at a high number to speed up plotting
rarecurve(t(otu_table(pseq.list[[i]])), step=500)
# add lines indicating sampling depth
abline(v=sample_sums(pseq.list[[i]]), lty='dotted', lwd=0.5)
}
# load #library
#library(QsRutils)
# calculate Good's coverage (only uses singletons)
for(i in 1:length(pseq.list)){
print(names(pseq.list[i]))
print(summary(goods(t(otu_table(pseq.list[[i]])))))
}
for (i in 1:length(pseq.list)) {
p <- pseq.list[[i]] %>% estimate_richness(measures = c('Observed', 'Chao1')) %>%
bind_cols(readcounts=sample_sums(pseq.list[[i]])) %>%
plotly::plot_ly(x=~readcounts, y=~Chao1) %>%
plotly::add_markers(text=rownames(.)) %>%
plotly::layout(title = paste(names(pseq.list[i])))
print(p)
rm(p)
}
# according to the results above: for
# archaea = 40000
# bacteria = ok
# euk = 95000
# rarefy the phy;oseq objects and calculate the alpha diversity
arc.rar <- rarefy_even_depth(pseq.list[[1]], 40000, rngseed=101)
bac.rar <- rarefy_even_depth(pseq.list[[2]], rngseed=101)
euk.rar <- rarefy_even_depth(pseq.list[[3]], 95000, rngseed=101)
# make a list of these phyloseq objects
pseq.rar.list <- list(arc.rar,bac.rar,euk.rar)
names(pseq.rar.list) <- c('Archaea', 'Bacteria', 'Eukaryotes')
```
- Notes:
+ Species richness describes the number of different species present in an area (more species = greater richness)
+ Species evenness describes the relative abundance of the different species in an area (similar abundance = more evenness)
+ As species richness and evenness increase, so diversity increases.
+ Observed: count of unique OTUs in each samples
+ Chao1: estimate diversity from abundance data (influenced by rare species)
+ Shannon's (H): influenced by rare species
+ Simpson's Index ($\lambda):less sensitive to the difference in taxa richness than Shannon's index - high values mean high diversity (range: 0-1)
+ inverse Simpson: 1/D - positively correlated with Shannon's
+ Evenness (Pielou's evenness): the lower value of Evenness, the lower the presence of a dominant specie = high diversity
+ [link](http://www.countrysideinfo.co.uk/simpsons.htm)
- Inv Simpson (Simpson's Reciprocal Index):
+ A high index value suggests a stable site with many different niches and low competition (high richness and evenness)
+ A low index value suggests a site with few potential niches where only a few species dominate (low richness and evenness).
+ The index value may change in response to an ecological disturbance (such as human intervention or natural disasters)
```{r plot alpha}
# Plot using microbiomeSeq function
for (i in 1: length(pseq.rar.list)) {
sample_data(pseq.rar.list[[i]])$Site_Layer <- factor(sample_data(pseq.rar.list[[i]])$Site_Layer, levels = c('ocean_S','C_S','C_P','B_S','B_P','A_S','A_P','GR_S'))
# generate a data frame for plot
p <- plot_anova_diversity(pseq.rar.list[[i]], method = c('richness','shannon', 'evenness', "invsimpson"), grouping_column = 'Site_Layer', pValueCutoff = 0.001, print.lines = FALSE)
p <- p + theme_Publication_3() +
theme(legend.position = "none") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
labs(x = "Sites_Layers", y = NULL) +
scale_color_manual(values = cols_site_layer) +
ggtitle(paste("Alpha Diversity:",names(pseq.rar.list[i])))
print(p)
rm(p)
}
# calculate the Stats for alpha diversity and make a table (lm)
boxplot.list <- list()
for (i in seq_along(pseq.rar.list)) {
adiv <- estimate_richness(pseq.rar.list[[i]], measures=c("Chao1", "Shannon", "InvSimpson"))
adiv <- pseq.rar.list[[i]] %>% evenness('Pielou') %>% rename(Pielou = pielou) %>%
bind_cols(adiv) %>% relocate(Pielou, .after = InvSimpson)
adiv$Sample_ID<- rownames(adiv) %>%
as.factor()
# df <- sample_data(pseq.rar.list[[i]]) %>%
# unclass() %>% data.frame() %>%
# left_join(adiv, by = "Sample_ID") %>%
# select(Site_Layer, Chao1 , Shannon , InvSimpson, Pielou) %>%
# gather(key="env_par", value="value", -Site_Layer) %>%
# group_by(Site_Layer, env_par)%>%
# summarise(mean= mean(value), sd= sd(value), max = max(value),min = min(value))
# write.csv(df, file = paste0("~R/git_hub/MH_2018/output/data/all/alpha/alpha_summary.Site_Layer_", names(pseq.rar.list[i]), ".csv"))
#
# df <- sample_data(pseq.rar.list[[i]]) %>%
# unclass() %>% data.frame() %>%
# left_join(adiv, by = "Sample_ID") %>%
# select(Site_Layer, Chao1 , Shannon , InvSimpson, Pielou) %>%
# gather(key="env_par", value="value", -Site_Layer) %>%
# nest(data = c(Site_Layer, value)) %>%
# mutate(model = map(data, ~ anova(lm(value ~ Site_Layer, .))),
# tidy = map(model, broom::tidy)) %>%
# select(env_par, tidy) %>%
# unnest(tidy)
# write.csv(df, file = paste0("~R/git_hub/MH_2018/output/data/all/alpha/alpha_anova_", names(pseq.rar.list[i]), ".csv"))
#
# df <- sample_data(pseq.rar.list[[i]]) %>%
# unclass() %>% data.frame() %>%
# left_join(adiv, by = "Sample_ID") %>%
# select(Site_Layer, Chao1 , Shannon , InvSimpson, Pielou) %>%
# gather(key="env_par", value="value", -Site_Layer) %>%
# nest(data = c(Site_Layer, value)) %>%
# mutate(model = purrr::map(data, ~ multcomp::glht(aov(value ~ Site_Layer, .), linfct = multcomp::mcp(Site_Layer = "Tukey"), test = adjusted(type = "bonferroni"))),
# tidy = purrr::map(model, broom::tidy)) %>%
# select(env_par, tidy) %>%
# unnest(tidy)
# write.csv(df, file = paste0("~R/git_hub/MH_2018/output/data/all/alpha/alpha_Tuk_", names(pseq.rar.list[i]), ".csv"))
boxplot.list[[i]] <- sample_data(pseq.rar.list[[i]]) %>%
unclass() %>% data.frame() %>%
left_join(adiv, by = "Sample_ID") %>%
select(Site, Layer, Chao1 , Shannon , InvSimpson, Pielou) %>%
pivot_longer(cols = Chao1:Pielou,names_to="env_par", values_to="value") %>%
mutate(Layer = factor(Layer, levels = c("S", "P"))) %>%
group_by(Layer, Site) %>%
ggplot(aes(x=Site, y=value, color = Layer)) +
geom_boxplot() +
geom_jitter(alpha=0.2, position = position_jitterdodge())+
geom_point(size=1,alpha=0.2)+theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
facet_wrap(~env_par, scales = "free_y") +ylab("Observed Values")+xlab("Sites") +
theme(strip.background = element_rect(fill = "white")) +
scale_x_discrete(limits = c("ocean", 'C', 'B', 'A','GR')) +
scale_color_manual(values = cols_layer) +
ggtitle(paste0(names(pseq.rar.list[i]))) +
theme_Publication_3() +
theme(axis.text.x = element_text(angle = 45, hjust = 1,size = 10),legend.position = 'bottom')
# print(boxplot_alpha)
#
# rm(adiv, df,boxplot_alpha)
}
plot.box <- wrap_plots(boxplot.list, nrow = 3, guides = "collect") & theme(legend.position = "bottom")
plot.box <- plot.box + plot_annotation(
title = 'Alpha Diveristy - Surface vs Intermediate Layers')
plot.box %>% ggsave(filename = "~R/git_hub/MH_2018/output/plots/figures/box.plt.alpha.div.tiff", compression = "lzw", width = 8.15, height = 12.56)
```
```{r corr: alpha and env}
# Alpha - Environ correlations
for (i in seq_along(pseq.rar.list)) {
# calculate alpha indices
adiv <- estimate_richness(pseq.rar.list[[i]], measures=c("Chao1", "Shannon", "InvSimpson"))
adiv <- pseq.rar.list[[i]] %>% evenness('Pielou') %>% rename(Pielou = pielou) %>%
bind_cols(adiv) %>% relocate(Pielou, .after = InvSimpson)
adiv$Sample_ID <- rownames(adiv) %>% as.factor()
# run the correlation
cor.alpha_env <- meta %>% left_join(adiv) %>% select(Temperature:Pielou, -pH, -Turbidity, -DO, -DO_perc,-se.chao1) %>%
correlate(method = 'spearman') %>% # => Spearman for non-normal distributions
# rearrange() %>% # rearrange by correlations
mutate(across(is.numeric, ~ round(., 3))) %>% # limiting the number of decimals
shave() %>% # Shave off the upper triangle for a clean result
select(term:NH4)
# save
write.csv(cor.alpha_env, file = paste0("~R/git_hub/MH_2018/output/data/all/alpha/corr_alpha_env_", names(pseq.rar.list[i]), ".csv"))
rm(adiv, cor.alpha_env)
}
```
```{r tidying}
# keep the environment tidy
rm(list=setdiff(ls(), c('seed',"meta","OTUdf",'pseq.all', 'pseq.list', 'TAXdf', 'TAX.table','cols_site_layer', 'cols_layer',"cols_hab_layer","pca.env")))
source('~/R/phd/2018/script/chapter2/my_functions.R')
source('~/R/phd/2018/script/chapter2/theme_publication.R')
```
### Community Analysis
```{r number of taxa}
for (i in seq_along(pseq.list)) {
pseq.list[i] %>% names() %>% print()
pseq.list[[i]] %>% tax_table() %>% as.data.frame() %>%
summarise(across(where(is.character), ~n_distinct(.x))) %>% print()
}
```
#### Composition plots
Before the multivariate analysis, let's explore community data
```{r community summary}
# make tables by a tax level by Kingdom - top 5 (not filtered)
summary_rel <- list()
summary_all <- list()
for (i in seq_along(pseq.list)) {
Rank <- rank_names(pseq.list[[i]])
for (j in seq(unique(Rank))) {
tax <- Rank
summary_rel[[j]] <- summarize_taxa(pseq.list[[i]], tax[[j]], arrange = TRUE) # function from
names(summary_rel)[j] <- paste0(names(pseq.list[i]),"_",tax[[j]])
write.csv(summary_rel[[j]], paste0('~R/git_hub/MH_2018/output/data/all/composition/summary_tables/all/rel_summary_all_',tax[[j]],'_',names(pseq.list[i]),".csv"))
name <- paste(names(pseq.list[i]))
summary_all[[name]][[j]] <- summary_rel[[j]]
names(summary_all[[i]])[j] <- paste(tax[j])
}
}
# make tables by tax level by Kingdom by Site Layer
summary_rel_tax <- list()
summary_all_tax <- list()
table <- list()
Group <- 'Habitat_Layer'
group.level <- c('ocean_S','MH_S','MH_P','GR_S')
for (i in seq_along(pseq.list)) {
Rank <- rank_names(pseq.list[[i]])
for (j in seq(unique(Rank))) {
tax <- Rank
summary_rel_tax[[j]] <- summarize_taxa(pseq.list[[i]], tax[[j]], arrange = TRUE, GroupBy = Group)
table[[j]] <- summary_rel_tax[[j]] %>%
select(-Abundance) %>%
pivot_wider(names_from = all_of(tax[[j]]), values_from = freq) %>% # transform in a wide table
arrange(factor(.data[[Group]], levels = group.level))
write.csv(table[[j]], paste0('~R/git_hub/MH_2018/output/data/all/composition/summary_tables/group/rel_summary_',Group,'_',tax[[j]],"_",names(pseq.list[i]),".csv"))
name <- paste(names(pseq.list[i]))
summary_all_tax[[name]][[j]] <- summary_rel_tax[[j]]
names(summary_all_tax[[i]])[j] <- paste(tax[j])
}
}
```
```{r subtax summary, eval=FALSE, include=FALSE}
# make tables by each Family in each phylum by Kingdom by Site layer
summary_rel_subtax <- list()
summary_all_subtax <- list()
for (i in seq_along(pseq.list)) {
Rank <- table(tax_table(pseq.list[[i]])[,"Phylum"]) %>% as.data.frame() %>% pull(Var1)
for (j in seq(unique(Rank))) {
tax <- Rank
summary_rel_subtax[[j]] <- summarize_by_subtaxa(pseq.list[[i]], str_c(tax[[j]]), 'Family','Site_Layer') %>%
arrange(factor(.$Site_Layer, levels = c('ocean_S','C_S','C_P','B_S','B_P','A_S','A_P','GR_S'))) %>%
select(-freq.within.group) %>%
slice_head(n = 5)
write.csv(summary_rel_subtax[[j]], paste0('~R/git_hub/MH_2018/output/data/all/composition/summary_tables/summary_subtax/rel_summary_Site_Layer_',tax[[j]],"_",names(pseq.list[i]),".csv"))
name <- paste(names(pseq.list[i]))
summary_all_subtax[[name]][[j]] <- summary_rel_subtax[[j]]
names(summary_all_subtax[[i]])[j] <- paste(tax[j])
}
}
```
```{r check for specific taxa}
# all community
pseq.filt.list[[2]] %>%
summarize_taxa("Class", GroupBy = "Habitat_Layer") %>% arrange(desc(Abundance))
filter_all(any_vars(. == "ocean_S")) %>%
arrange(desc(Abundance))
# or only within determined group
pseq.filt.list[[3]] %>%
summarize_by_subtaxa("Ochrophyta", 'Family',"Habitat_Layer") %>%
filter(Habitat_Layer == "MH_P")
```
```{r bar plots}
# relative abundance and LCBD (Legendre and De Caceres, 2013)
# Phylum
# PS: need to modify the function to reorder x-axis
p.phylum <- list()
for (i in seq_along(pseq.rel.filt.list)) {
p.phylum[[i]] <-plot_taxa(pseq.rel.filt.list[[i]], taxo = 'Phylum', grouping_column = 'Site_Layer', method = 'hellinger')
p.phylum[[i]] <- p.phylum[[i]] + theme_Publication_3() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
ggtitle(paste(names(pseq.rel.filt.list[i])," - Phylum"))
}
# egg::ggarrange(plots = p.phylum, labels = c("A", "B",'C'))
# Class
p.class <- list()
for (i in seq_along(pseq.rel.filt.list)) {
p.class[[i]] <- plot_taxa(pseq.rel.filt.list[[i]], taxo = 'Class', grouping_column = 'Site_Layer', method = 'hellinger')
p.class[[i]] <- p.class[[i]] + theme_Publication_3() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
ggtitle(paste(names(pseq.rel.filt.list[i])," - Class"))
}
# Family
p.fam <- list()
for (i in seq_along(pseq.rel.filt.list)) {
p.fam[[i]] <- plot_taxa(pseq.rel.filt.list[[i]], taxo = 'Family', grouping_column = 'Site_Layer', method = 'hellinger')
p.fam[[i]] <- p.fam[[i]] + theme_Publication_3() +
theme(axis.text.x = element_text(angle = 45, hjust = 1),
legend.position = "bottom") +
ggtitle(paste(names(pseq.rel.filt.list[i])," - Family"))
}
# to explore the plots
# plotly::ggplotly(p.fam[[3]])
```
```{r box plots}
box_rank <- list()
top_rank <- list()
rank_name <- list()
top_rank_name <- list()
ps_name <- list()
ps <- list()
Rank <- c("Phylum" , "Class" , "Order" , "Family" , "Genus")
# use summary_all_tax from summary
for(i in seq_along(pseq.rel.filt.list)){
for (j in seq(unique(Rank))) {
# get the top class from the summary_rel list
rank_name <- Rank[[j]]
top_rank_name[[j]] <- summary_all_tax[[i]][[rank_name]] %>% ungroup() %>% select(all_of(rank_name)) %>% pull() %>% unique()
name <- paste(names(pseq.rel.filt.list[i]))
top_rank[[name]][[j]] <- top_rank_name[[j]]
names(top_rank[[i]])[j] <- paste(Rank[j])
}
# subset taxa
ps[[name]][[1]] <- pseq.rel.filt.list[[i]] %>% subset_taxa(Phylum %in% top_rank[[i]][['Phylum']])
names(ps[[i]])[1] <- paste('Phylum')
ps[[name]][[2]] <- pseq.rel.filt.list[[i]] %>% subset_taxa(Class %in% top_rank[[i]][['Class']])
names(ps[[i]])[2] <- paste('Class')
ps[[name]][[3]] <- pseq.rel.filt.list[[i]] %>% subset_taxa(Order %in% top_rank[[i]][['Order']])
names(ps[[i]])[3] <- paste('Order')
ps[[name]][[4]] <- pseq.rel.filt.list[[i]] %>% subset_taxa(Family %in% top_rank[[i]][['Family']])
names(ps[[i]])[4] <- paste('Family')
ps[[name]][[5]] <- pseq.rel.filt.list[[i]] %>% subset_taxa(Genus %in% top_rank[[i]][['Genus']])
names(ps[[i]])[5] <- paste('Genus')
}
for (i in seq_along(pseq.rel.filt.list)) {
# to sort the x axis labels
#sample_data(pseq.rel.filt.list[[i]])$Site_layer <- factor(sample_data(pseq.rel.filt.list[[i]])$Site_layer,levels = c('ocean_S','C_S','C_P','B_S','B_P','A_S','A_P','GR_S'))
# run the box plot function
p1 <- boxplot.pseq(ps[[i]][['Phylum']], 'Phylum', 'Site_Layer') +
theme_plex() +
ggtitle(paste("% Abundance",names(pseq.rel.filt.list[i]),' Phylum')) +
theme(legend.position = 'none')
p2 <- boxplot.pseq(ps[[i]][['Class']], 'Class', 'Site_Layer') +
theme_plex() +
ggtitle(paste("% Abundance",names(pseq.rel.filt.list[i]),' Class')) +
theme(legend.position = 'none')
p3 <- boxplot.pseq(ps[[i]][['Order']], 'Order', 'Site_Layer') +
theme_plex() +
ggtitle(paste("% Abundance",names(pseq.rel.filt.list[i]),' Order')) +
theme(legend.position = 'none')
p4 <- boxplot.pseq(ps[[i]][['Family']], 'Family', 'Site_Layer') +
theme_plex() +
ggtitle(paste("% Abundance",names(pseq.rel.filt.list[i]),' Family')) +
theme(legend.position = 'none')
p5 <- boxplot.pseq(ps[[i]][['Genus']], 'Genus', 'Site_Layer') +
theme_plex() +
ggtitle(paste("% Abundance",names(pseq.rel.filt.list[i]),' Genus')) +
theme(legend.position = 'none')
print(p1)
print(p2)
print(p3)
print(p4)
print(p5)
}