-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathks_05_timeseries_mca-hcpc.Rmd
1623 lines (1226 loc) · 59.7 KB
/
ks_05_timeseries_mca-hcpc.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: "ks_mca-hcpc_timeseries"
author: "Kate Sheridan"
date: "3/13/2022"
output: html_document
editor_options:
markdown:
wrap: 72
---
```{r setup, include=FALSE}
library(tidyverse)
library(ggplot2)
library(FactoMineR)
library(factoextra)
library(dplyr)
library(here)
# here filepaths
traits <- here('data', 'clean')
plots <- here('output', '2022may', 'mca')
# set seed for whole session:
addTaskCallback(function(...) {set.seed(3824);TRUE})
# to remove
#removeTaskCallback(1)
```
## load in data
```{r}
# time series
algae_traits <- read_csv(here(traits, "20220525_1982-end_algae.csv")) %>%
filter(intertidal_gom == 'yes') %>%
select(!(c(intertidal_gom, group, autotroph,
saccate_external_morphology,
potential_asexual_reproduction,
epilithic_env_position)))
#algae_traits <- algae_traits %>%
# mutate(maximun_longevity = str_replace_all(maximun_longevity, c(" \\(one-ten years\\)"= "")))
animal_traits <- read_csv(here(traits, "20220321_1982-end_animal.csv")) %>%
filter(intertidal == 'yes') %>%
select(!(c(intertidal, trophic_level, pelagic, deposit_feeder, benthic)))
# to load in individual species lists from parts of the time series
#animal_1982_1995_traits <- read_csv(here(mcadata, "20220313_1982-1995_animal.csv")) %>%
# filter(intertidal == 'yes') %>%
# select(!(c(intertidal)))
#animal_2011_end_traits <- read_csv(here(mcadata, "20220313_2011-end_animal.csv")) %>%
# filter(intertidal == 'yes') %>%
# select(!(c(intertidal)))
# all species
#algae_imputed_all <- read_csv(here(traits, "20220323_algae_imputed.csv"),
# col_select = !1) %>%
# filter(intertidal == 'yes') %>%
# select(!(c(intertidal)))
#animal_imputed_all <- read_csv(here(traits, "20220321_animal_imputed.csv"),
# col_select = !1) %>%
# filter(intertidal == 'yes') %>%
# select(!(c(intertidal)))
```
## load in functions
```{r}
# input should be imputed traits, filtered as needed
## note that we're selecting columns automatically here
## starting with column 3 after the species column; change if not true
# note that only output starting with gg are ggplot objects
appledoreMCAHCPC <- function(traits) {
# select columns for MCA
traits <- traits %>%
column_to_rownames('species') %>%
dplyr::select(3:(ncol(traits)-1)) %>%
mutate(across(.fns = ~ as.factor(.)))
# run MCA
mca2 <- MCA(traits, graph = FALSE)
# extract variable categories
mca2_vars <- get_mca_var(mca2)
# prepare data for plotting
cats=apply(traits, 2, function(x) nlevels(as.factor(x)))
mca2_vars_df = data.frame(mca2$var$coord, Variable = rep(names(cats),
cats))
mca2_obs_df = data.frame(mca2$ind$coord)
# plot with lines and vectors
mcaggplot <- ggplot(data = mca2_obs_df, aes(x = Dim.1, y = Dim.2)) +
geom_hline(yintercept = 0, colour = "gray70") +
geom_vline(xintercept = 0, colour = "gray70") +
geom_point(colour = "gray50", alpha = 0.7) +
geom_density2d(colour = "gray80") +
geom_text(data = mca2_vars_df, aes(x = Dim.1, y = Dim.2,
label = rownames(mca2_vars_df),
colour = Variable)) +
ggtitle("MCA plot with FactorMineR") +
scale_colour_discrete(name = "Variable") +
geom_segment(data = mca2_vars_df, aes(x = 0, y = 0, xend = Dim.1, yend = Dim.2),
arrow = arrow(length = unit(0.2, "cm")), colour = "black") +
theme_bw(20)
# ellipses plot
plot_ellipses <- plotellipses(mca2)
# diagnostics
scree2 <- fviz_screeplot(mca2)
contrib2 <- fviz_contrib(mca2, choice = "var", axes = 1:2, top = 15)
cos2plot <- fviz_cos2(mca2, choice = 'var', axes = 1:2, top = 15)
# this plot combines output of contribution and cos2,
## eg: orange+high alpha = high contribution, high quality
cos2contribplot <- fviz_mca_var(mca2, col.var = 'cos2', alpha.var = 'contrib',
gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"),
repel = TRUE, # Avoid text overlapping
ggtheme = theme_minimal())
# run HCPC
hcpc2 <- HCPC(mca2, graph = FALSE)
# hcpc dendrogram
hcpc_dend <- fviz_dend(hcpc2, cex = .6)
# hcpc factor map
hcpc_clust <- fviz_cluster(hcpc2, geom = "point", main = "Factor map")
#output
list(mcares = mca2, mcacos2 = mca2_vars$cos2,
vars_df = mca2_vars_df,
clustmembers = hcpc2$desc.ind$para,
clustervariables = hcpc2$desc.var$category,
hcpc_chi = hcpc2$desc.var$test.chi2,
ggmca = mcaggplot,
ggscreeplot = scree2, ggcontrib = contrib2,
ggcos2 = cos2plot, ggvars = cos2contribplot,
ellipses = plot_ellipses, ggdendrogram = hcpc_dend,
ggclusterplot = hcpc_clust)
}
```
## run function example
explore output; use df\$ to see all saved plots and summary stats Any
output starting with gg is a ggplot object and can be chained with
ggplot functions such as: `algae_all$ggmca + theme_bw()`
```{r function-explore}
algae_all_test <- algae_traits %>%
select(!(c(year, transect, position))) %>%
distinct()
algae_all <- appledoreMCAHCPC(algae_all_test)
# example view a summary
algae_all$mcares$eig
# example ggplot
algae_all$ggscreeplot + theme_bw()
algae_all$ggmca
algae_all$ggdendrogram
algae_all$ggclusterplot
algae_all$clustervariables
algae_all$clustmembers
```
# set up subsets and run
## Algae
```{r algae-all}
algaehcpc <- HCPC(algae_all$mcares, nb.clust = 5)
algaehcpc$desc.var
algaehcpc$desc.ind
fviz_dend(algaehcpc)
```
### Timeseries
```{r algae-timeseries}
# filter
algae_82_95 <- algae_traits %>%
filter(year <= 1994 & year >= 1982) %>%
select(!(c(year, transect, position))) %>%
distinct()
algae_96_06 <- algae_traits %>%
filter(year >= 1996 & year < 2007) %>%
select(!(c(year, transect, position))) %>%
distinct()
algae_10_end <- algae_traits %>%
filter(year <= 2011) %>%
select(!(c(year, transect, position))) %>%
distinct()
# run mca
mca_algae_82_95 <- appledoreMCAHCPC(algae_82_95)
mca_algae_96_06 <- appledoreMCAHCPC(algae_96_06)
mca_algae_10_end <- appledoreMCAHCPC(algae_10_end)
```
```{r algae-timeseries-explore}
mca_algae_82_95$ggmca
mca_algae_96_06$ggmca
mca_algae_10_end$ggmca
```
### exposed/sheltered
for the time series here it will just be beginning and end
```{r algae-exposedsheltered}
# just exposed v sheltered
algae_exposed <- algae_traits %>%
filter(position == 'exposed') %>%
select(!(c(year, transect, position))) %>%
distinct()
algae_sheltered <- algae_traits %>%
filter(position == 'sheltered') %>%
select(!(c(year, transect, position))) %>%
distinct()
mca_algae_exposed <- appledoreMCAHCPC(algae_exposed)
mca_algae_sheltered <- appledoreMCAHCPC(algae_sheltered)
# exposed v sheltered beginning and end
algae_exposed_start <- algae_traits %>%
filter(position == 'exposed' & year <= 1994 & year >= 1982) %>%
select(!(c(year, transect, position))) %>%
distinct()
algae_sheltered_start <- algae_traits %>%
filter(position == 'sheltered' & year <= 1994 & year >= 1982) %>%
select(!(c(year, transect, position))) %>%
distinct()
algae_sheltered_middle <- algae_traits %>%
filter(position == 'sheltered' & year >= 1996 & year < 2007) %>%
select(!(c(year, transect, position))) %>%
distinct()
algae_exposed_middle <- algae_traits %>%
filter(position == 'exposed' & year >= 1996 & year < 2007) %>%
select(!(c(year, transect, position))) %>%
distinct()
algae_exposed_end <- algae_traits %>%
filter(position == 'exposed' & year <= 2010) %>%
select(!(c(year, transect, position))) %>%
distinct()
algae_sheltered_end <- algae_traits %>%
filter(position == 'sheltered' & year <= 2010) %>%
select(!(c(year, transect, position))) %>%
distinct()
mca_algae_exposed_start <- appledoreMCAHCPC(algae_exposed_start)
mca_algae_sheltered_start <- appledoreMCAHCPC(algae_sheltered_start)
mca_algae_exposed_middle <- appledoreMCAHCPC(algae_exposed_middle)
mca_algae_sheltered_middle <- appledoreMCAHCPC(algae_sheltered_middle)
mca_algae_exposed_end <- appledoreMCAHCPC(algae_exposed_end)
mca_algae_sheltered_end <- appledoreMCAHCPC(algae_sheltered_end)
```
```{r algae-position-plots}
mca_algae_exposed_start$ggmca
mca_algae_exposed_end$ggmca
mca_algae_sheltered_start$ggmca
mca_algae_sheltered_end$ggmca
mca_algae_exposed$ggmca
mca_algae_sheltered$ggmca
ggsave(here(plots, 'temporary', '2algaeexposed.png'), mca_algae_exposed$ggmca, width = 10)
ggsave(here(plots, 'temporary', '2algaesheltered.png'), mca_algae_sheltered$ggmca, width = 10)
ggsave(here(plots, 'temporary', '2algaeexposed_start.png'), mca_algae_exposed_start$ggmca, width = 10)
ggsave(here(plots, 'temporary', '2algaeexposed_end.png'), mca_algae_exposed_end$ggmca, width = 10)
ggsave(here(plots, 'temporary', '2algaesheltered_start.png'), mca_algae_sheltered_start$ggmca, width = 10)
ggsave(here(plots, 'temporary', '2algaesheltered_end.png'), mca_algae_sheltered_end$ggmca, width = 10)
```
```{r algae-exposedsheltered-explore}
mca_algae_exposed$ggdendrogram
mca_algae_sheltered$ggdendrogram
mca_algae_exposed_start$ggdendrogram
mca_algae_exposed_end$ggdendrogram
mca_algae_sheltered_start$ggdendrogram
mca_algae_sheltered_end$ggdendrogram
mca_algae_sheltered_end$clustmembers
mca_algae_sheltered_end$clustervariables
mca_algae_sheltered_start$clustmembers
mca_algae_sheltered_start$clustervariables
exposedhcpc2 <- HCPC(res = mca_algae_exposed_end$mcares, nb.clust = 5)
mca_algae_exposed_start$ggdendrogram
fviz_dend(exposedhcpc2)
```
```{r algae-exposedsheltered-summarystats}
mca_algae_exposed_start$mcares$eig
mca_algae_exposed_end$mcares$eig
mca_algae_exposed_middle$mcares$eig
mca_algae_sheltered_start$mcares$eig
mca_algae_exposed_middle$mcares$eig
mca_algae_sheltered_end$mcares$eig
mca_algae_exposed_start$ggcos2
mca_algae_exposed_middle$ggcos2
mca_algae_exposed_end$ggcos2
mca_algae_sheltered_start$ggcos2
mca_algae_sheltered_middle$ggcos2
mca_algae_sheltered_end$ggcos2
mca_algae_exposed_start$mcares$var$contrib
mca_algae_exposed_start$ggcontrib
mca_algae_exposed_middle$ggcontrib
mca_algae_exposed_end$ggcontrib
mca_algae_sheltered_start$ggcontrib
mca_algae_sheltered_middle$ggcontrib
mca_algae_sheltered_end$ggcontrib
mca_algae_sheltered_end$mcares$ind$contrib
mca_algae_sheltered_end$mcares$ind$cos2
mca_algae_sheltered_end$mcares$var$eta2
mca_algae_sheltered_end$mcares$var$contrib
mca_algae_sheltered_start$mcares$ind$contrib
mca_algae_sheltered_start$mcares$ind$cos2
mca_algae_sheltered_start$mcares$var$eta2
mca_algae_sheltered_start$mcares$var$contrib
```
```{r algae-compare}
library(arsenal)
# in output, first dataframe is X, second is Y
exposedvprotect <- comparedf(algae_exposed, algae_sheltered, by = 'species')
#extract differences
diffs(exposedvprotect, what = c('observations'))
protectstvend <- comparedf(algae_sheltered_start, algae_sheltered_end, by = 'species')
diffs(protectstvend, what = c('observations'))
exposedstvend <- comparedf(algae_exposed_start, algae_exposed_end, by = 'species')
diffs(exposedstvend, what = c('observations'))
```
```{r algae-hypervolume}
library(hypervolume)
#testing
#hypervolume1<-hypervolume_box(data.frame(algae_all$mcares$ind$coord))
#plot(hypervolume1)
hyp_algae_exposed_start <-hypervolume_gaussian(data.frame(mca_algae_exposed_start$mcares$ind$coord))
#hyp_algae_exposed_end <-hypervolume_box(data.frame(mca_algae_exposed_end$mcares$ind$coord))
hyp_algae_sheltered_start <-hypervolume_gaussian(data.frame(mca_algae_sheltered_start$mcares$ind$coord))
hyp_algae_sheltered_end <-hypervolume_gaussian(data.frame(mca_algae_sheltered_end$mcares$ind$coord))
# protected v exposed start
hyp_set_algae_sides1 <- hypervolume_set(hyp_algae_exposed_start, hyp_algae_sheltered_start, check.memory=FALSE)
# 1 is first listed, 2 is second listed
hypervolume_overlap_statistics(hyp_set_algae_sides1)
#volumes
get_volume(hyp_set_algae_sides1)
# basic plot
plot.HypervolumeList(hyp_set_algae_sides1, colors = c('#806238', "#94b4d8", '#bcbcd2', 'purple', '#a08b5a', 'blue'))
# protected v exposed current
hyp_set_algae_sides <- hypervolume_set(hyp_algae_exposed_end, hyp_algae_sheltered_end, check.memory=FALSE)
# 1 is first listed, 2 is second listed
hypervolume_overlap_statistics(hyp_set_algae_sides)
#volumes
get_volume(hyp_set_algae_sides)
# basic plot
plot.HypervolumeList(hyp_set_algae_sides, colors = c('#806238', "#94b4d8", '#bcbcd2', 'purple', '#a08b5a', 'blue'))
# exposed beginning v end
hyp_set_algae_exposed <- hypervolume_set(hyp_algae_exposed_start, hyp_algae_exposed_end, check.memory=FALSE)
# 1 is first listed, 2 is second listed
hypervolume_overlap_statistics(hyp_set_algae_exposed)
#volumes
get_volume(hyp_set_algae_exposed)
# basic plot
plot.HypervolumeList(hyp_set_algae_exposed, colors = c('#806238', "#94b4d8", '#bcbcd2', 'purple', '#a08b5a', 'blue'))
# protected beginning v end
hyp_set_algae_sheltered <- hypervolume_set(hyp_algae_sheltered_start, hyp_algae_sheltered_end, check.memory=FALSE)
# 1 is first listed, 2 is second listed
hypervolume_overlap_statistics(hyp_set_algae_sheltered)
#volumes
get_volume(hyp_set_algae_sheltered)
# basic plot
plot.HypervolumeList(hyp_set_algae_sheltered, colors = c('#806238', "#94b4d8", '#bcbcd2', 'purple', '#a08b5a', 'blue'),
show.axes = FALSE, cex.random = .1)
hypervolume_redundancy(hyp_algae_sheltered_start, data.frame(mca_algae_sheltered_end$mcares$ind$coord))
hypervolume_redundancy(hyp_algae_sheltered_start, data.frame(mca_algae_sheltered_start$mcares$ind$coord))
## both tests seem to show change, but not significance.
# to run this test I need to sample from both populations not just one;
# run resample on all sheltered, then overlap test with the path being to overlap test
## this method is less good for small sample sizes
hyp_algae_shelter_all <- hypervolume_gaussian(data.frame(mca_algae_sheltered_end$mcares$ind$coord))
test_algae_start <- hypervolume_resample('test_algae_start', hyp_algae_shelter_all, 'bootstrap', cores = 2)
# this is good for smaller datasets
test_algae_start <- hypervolume_permute('test_algae_permute', hyp_algae_sheltered_start, hyp_algae_sheltered_end, cores = 2)
algae_sheltered_overlap <- hypervolume_overlap_test(hyp_algae_sheltered_start, hyp_algae_sheltered_end, path = test_algae_start)
algae_sheltered_overlap
hypervolume_funnel()
plot(hyp_algae_sheltered_start)
plot(hyp_algae_sheltered_end)
```
## Animals
Note animal_all gives different result than any group of animals. I
really don't understand why, it puts Astarias rubens in totally
different locations.
```{r animal-all}
animal_all_t <- animal_traits %>%
select(!(c(year,transect,position))) %>%
distinct()
mca_animal_all <- appledoreMCAHCPC(animal_all_t)
animal_allhcpc <- HCPC(res = mca_animal_all$mcares, nb.clust = 5)
# plots testing output
fviz_cluster(animal_allhcpc, geom = "point", main = "Factor map")
mca_animal_all$ggclusterplot
fviz_dend(animal_allhcpc)
mca_animal_all$clustmembers
mca_animal_all$clustervariables
animal_allhcpc$desc.var
animal_allhcpc$desc.ind
```
### Timeseries
```{r animal-timeseries}
# filter
animal_82_95 <- animal_traits %>%
filter(year <= 1994 & year >= 1982) %>%
select(!(c(year, transect, position))) %>%
distinct()
animal_96_06 <- animal_traits %>%
filter(year >= 1996 & year < 2007) %>%
select(!(c(year, transect, position))) %>%
distinct()
animal_10_end <- animal_traits %>%
filter(year <= 2011) %>%
select(!(c(year, transect, position))) %>%
distinct()
# run mca
mca_animal_82_95 <- appledoreMCAHCPC(animal_82_95)
mca_animal_96_06_mca <- appledoreMCAHCPC(animal_96_06)
mca_animal_10_end <- appledoreMCAHCPC(animal_10_end)
```
### exposed/sheltered
for the time series here it will just be beginning and end
```{r animal-exposedsheltered}
# just exposed v sheltered
animal_exposed <- animal_traits %>%
filter(position == 'exposed') %>%
select(!(c(year, transect, position))) %>%
distinct()
animal_sheltered <- animal_traits %>%
filter(position == 'sheltered') %>%
select(!(c(year, transect, position))) %>%
distinct()
mca_animal_exposed <- appledoreMCAHCPC(animal_exposed)
mca_animal_sheltered <- appledoreMCAHCPC(animal_sheltered)
# exposed v sheltered beginning and end
animal_exposed_start <- animal_traits %>%
filter(position == 'exposed' & year <= 1994 & year >= 1982) %>%
select(!(c(year, transect, position))) %>%
distinct()
animal_sheltered_start <- animal_traits %>%
filter(position == 'sheltered' & year <= 1994 & year >= 1982) %>%
select(!(c(year, transect, position))) %>%
distinct()
animal_exposed_middle <- animal_traits %>%
filter(position == 'exposed' & year >= 1996 & year < 2007) %>%
select(!(c(year, transect, position))) %>%
distinct()
animal_sheltered_middle <- animal_traits %>%
filter(position == 'sheltered' & year >= 1996 & year < 2007) %>%
select(!(c(year, transect, position))) %>%
distinct()
animal_exposed_end <- animal_traits %>%
filter(position == 'exposed' & year <= 2010) %>%
select(!(c(year, transect, position))) %>%
distinct()
animal_sheltered_end <- animal_traits %>%
filter(position == 'sheltered' & year <= 2010) %>%
select(!(c(year, transect, position))) %>%
distinct()
mca_animal_exposed_start <- appledoreMCAHCPC(animal_exposed_start)
mca_animal_sheltered_start <- appledoreMCAHCPC(animal_sheltered_start)
mca_animal_exposed_middle <- appledoreMCAHCPC(animal_exposed_middle)
mca_animal_sheltered_middle <- appledoreMCAHCPC(animal_sheltered_middle)
mca_animal_exposed_end <- appledoreMCAHCPC(animal_exposed_end)
mca_animal_sheltered_end <- appledoreMCAHCPC(animal_sheltered_end)
```
```{r animal-position-plots}
# overall
mca_animal_exposed$ggmca
mca_animal_sheltered$ggmca
mca_animal_exposed_start$ggvars
mca_animal_exposed_end$ggvars
# exposed
mca_animal_exposed_start$ggmca
mca_animal_exposed_end$ggmca
# sheltered
mca_animal_sheltered_start$ggmca
mca_animal_sheltered_end$ggmca
mca_animal_sheltered_start$ggvars
mca_animal_sheltered_end$ggvars
ggsave(here(plots, 'temporary', 'animalexposed_start.png'), mca_animal_exposed_start$ggmca, width = 10)
ggsave(here(plots, 'temporary', 'animalexposed_end.png'), mca_animal_exposed_end$ggmca, width = 10)
ggsave(here(plots, 'temporary', 'animalsheltered_start.png'), mca_animal_sheltered_start$ggmca, width = 10)
ggsave(here(plots, 'temporary', 'animalsheltered_end.png'), mca_animal_sheltered_end$ggmca, width = 10)
```
```{r animal-hcpc-exploration}
shelteredhcpc2 <- HCPC(res = mca_animal_sheltered$mcares, nb.clust = 5)
exposedhcpc2 <- HCPC(res = mca_animal_exposed$mcares, nb.clust = 4)
fviz_cluster(exposedhcpc2, geom = "point", main = "Factor map")
fviz_cluster(shelteredhcpc2, geom = "point", main = "Factor map")
fviz_dend(exposedhcpc2)
fviz_dend(shelteredhcpc2)
mca_animal_exposed$ggclusterplot
mca_animal_sheltered$ggclusterplot
mca_animal_exposed$ggdendrogram
mca_animal_sheltered$ggdendrogram
mca_animal_exposed$clustervariables
mca_animal_sheltered$clustervariables
#mca_animal_exposed$clustmembers
mca_animal_sheltered$clustmembers
mca_animal_sheltered_start$ggclusterplot
mca_animal_sheltered_end$ggclusterplot
mca_animal_exposed_start$ggclusterplot
mca_animal_exposed_end$ggclusterplot
mca_animal_sheltered$ggdendrogram
mca_animal_sheltered_start$ggdendrogram
mca_animal_sheltered_end$ggdendrogram
mca_animal_exposed$ggdendrogram
mca_animal_exposed_start$ggdendrogram
mca_animal_exposed_end$ggdendrogram
mca_animal_sheltered_end$mcares$ind$cos2
mca_animal_sheltered_end$mcares$ind$contrib
mca_animal_sheltered_end$ggvars
fviz_mca_ind(mca_animal_sheltered_end$mcares, col.var = 'contrib', #alpha.var = 'contrib',
gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"),
select.var = list(cos2 = .4),
repel = TRUE, # Avoid text overlapping
ggtheme = theme_minimal())
mca_animal_exposed_end$mcares$var$eta2
mca_animal_exposed_end$mcares$var$contrib
```
# arsenal to extract differences quickly
```{r animal-compare}
library(arsenal)
# in output, first dataframe is X, second is Y
exposedvprotect <- comparedf(animal_exposed, animal_sheltered, by = 'species')
#extract differences
diffs(exposedvprotect, what = c('observations'))
protectstvend <- comparedf(animal_sheltered_start, animal_sheltered_end, by = 'species')
diffs(protectstvend, what = c('observations'))
exposedstvend <- comparedf(animal_exposed_start, animal_exposed_end, by = 'species')
diffs(exposedstvend, what = c('observations'))
animal_exp_notmiddle <- full_join(animal_exposed_start, animal_exposed_end)
exposedstvend <- comparedf(animal_exposed_middle, animal_exp_notmiddle, by = 'species')
diffs(exposedstvend, what = c('observations'))
#nothing unique to the middle
```
```{r animal-hypervolume}
library(hypervolume)
hyp_animal_sheltered_start <-hypervolume_gaussian(data.frame(mca_animal_sheltered_start$mcares$ind$coord))
hyp_animal_sheltered_end <-hypervolume_gaussian(data.frame(mca_animal_sheltered_end$mcares$ind$coord))
# protected beginning v end
hyp_set_animal_sheltered <- hypervolume_set(hyp_animal_sheltered_start, hyp_animal_sheltered_end, check.memory=FALSE)
# 1 is first listed, 2 is second listed
hypervolume_overlap_statistics(hyp_set_animal_sheltered)
#volumes
get_volume(hyp_set_animal_sheltered)
# basic plot
plot.HypervolumeList(hyp_set_animal_sheltered, colors = c('#806238', "#94b4d8", '#bcbcd2', 'purple', '#a08b5a', 'blue'),
show.axes = FALSE, cex.random = .1)
```
# presentation plots
palette is:
`#bcbcd2, #94b4d8, #5484c4, #374658, #a08b5a, #806238, #543414, #3a230e`
lavender grey, pale cerulean, glaucous, charcoal, metallic sunburst,
coyote brown, dark brown, bistre
sets of 2 should be primarily drawn from the middle two of each part of
the palette:
two blues
#94b4d8, #5484c4
two browns
#806238, #543414
blue brown light
#94b4d8,#806238
blue brown dark
#5484c4, #543414
```{r nice-fonts}
library(showtext)
showtext_auto()
# to see list of fonts
font_files()
# add desired fonts with ('name to call it by in code', 'name in filepath, or complete filepath')
font_add('quicksand', 'Quicksand-Medium.ttf')
```
## clusters
```{r testing}
#tree
library(dendextend)
# extract dendrogram and save as ggdend object
test <- as.ggdend(mca_animal_exposed$ggdendrogram$plot_env$dend)
ggplot(test, horiz = TRUE) +
labs(y = NULL, title = 'Algae Clusters') +
ylim(.25,-.12) +
# manually set colors
scale_color_manual(values = c('red','blue','purple','black','green','orange','grey')) +
theme(axis.ticks = element_blank(),
axis.text.y = element_blank()
)
ggsave(filename = here(plots, 'algaetest.png'), device = 'png',
height = 5, width = 7, units = 'in')
# clusters
mca_algae_exposed_start$ggclusterplot +
#manually set colors
scale_fill_manual(values = c('red','blue','purple','black','green','orange')) +
scale_color_manual(values = c('red','blue','purple','black','green','orange')) +
labs(title = 'Algae clusters') +
theme_classic() +
theme(axis.text = element_blank())
ggsave(filename = here(plots, 'algaetest2.png'), device = 'png',
height = 5, width = 7, units = 'in')
#mcas
mca_animal_exposed_start$ggmca +
labs(title = 'Algae mca') +
scale_color_manual(values = c('red','blue','purple', 'black',
'green','orange', 'grey', 'yellow', 'brown',
'grey17', 'pink', 'grey60', 'salmon',
'violet', 'cornsilk2')) +
theme_classic() +
theme(legend.position = 'none')
ggsave(filename = here(plots, 'algaetest3.png'), device = 'png',
height = 5, width = 7, units = 'in')
```
```{r algae}
library(dendextend)
algaehcpc_dend <- fviz_dend(algaehcpc, cex = .6)
algaehcpc_dend <- as.ggdend(algaehcpc_dend$plot_env$dend)
algaehcpc_cluster <- fviz_cluster(algaehcpc, geom = "point", main = "Factor map")
ggplot(algaehcpc_dend, horiz = TRUE) +
labs(y = NULL, title = 'Algae Functional Groups: All') +
ylim(.2,-.12) +
# manually set colors
scale_color_manual(values = c('#bcbcd2','#5484c4','#543414','#374658', '#a08b5a', 'black')) +
theme(axis.ticks = element_blank(),
axis.text.y = element_blank()
)
ggsave(filename = here(plots, 'hcpc', '20220525_algae_dend.png'), device = 'png',
height = 5, width = 7, units = 'in')
algaehcpc_cluster$data
algaehcpc_cluster +
#manually set colors
scale_fill_manual(values = c('#543414', '#a08b5a','#5484c4', '#bcbcd2','#374658')) +
scale_color_manual(values = c('#543414','#a08b5a','#5484c4', '#bcbcd2','#374658')) +
labs(title = 'Algae functional groups: All') +
theme_classic() +
theme(axis.text = element_blank(),
legend.position = 'none')
ggsave(filename = here(plots, 'hcpc', '20220323_algae_all_cluster.png'), device = 'png',
height = 5, width = 7, units = 'in')
```
```{r animal-all}
animal_allhcpc <- HCPC(res = mca_animal_all$mcares, nb.clust = 4)
animal_hcpc_dend <- fviz_dend(animal_allhcpc, cex = .6)
animal_hcpc_dend <- as.ggdend(animal_hcpc_dend$plot_env$dend)
animalhcpc_cluster <- fviz_cluster(animal_allhcpc, geom = "point", main = "Factor map")
ggplot(animal_hcpc_dend, horiz = TRUE) +
labs(y = NULL, title = 'Animal Functional groups: All') +
ylim(.34,-.12) +
# manually set colors
scale_color_manual(values = c('#5484c4','#543414','#374658', '#a08b5a', 'black')) +
theme(axis.ticks = element_blank(),
axis.text.y = element_blank()
)
ggsave(filename = here(plots, 'hcpc', '20220323_animal_dend_all.png'), device = 'png',
height = 5, width = 7, units = 'in')
animalhcpc_cluster$data
animalhcpc_cluster +
#manually set colors
scale_fill_manual(values = c('#374658', '#5484c4', '#a08b5a', '#543414')) +
scale_color_manual(values = c('#374658','#5484c4', '#a08b5a', '#543414')) +
labs(title = 'Animal functional groups: All') +
theme_classic() +
theme(axis.text = element_blank(),
legend.position = 'none')
ggsave(filename = here(plots, 'hcpc', '20220323_animal_all_cluster.png'), device = 'png',
height = 5, width = 7, units = 'in')
```
```{r animals}
#manually split the 4 clusters for sheltered_start
## probably better plotting can be done at this point but
# i'm just going to make it into the same format as the others
shelteredhcpc2 <- HCPC(res = mca_animal_sheltered_start$mcares, nb.clust = 5)
start_test <- fviz_dend(shelteredhcpc2, cex = .7)
start_test2 <- as.ggdend(start_test$plot_env$dend)
start_cluster2 <- fviz_cluster(shelteredhcpc2, geom = "point", main = "Factor map")
shelteredhcpc3 <- HCPC(res = mca_animal_sheltered_end$mcares, nb.clust = 5)
end_test <- fviz_dend(shelteredhcpc3, cex = .7)
end_test2 <- as.ggdend(end_test$plot_env$dend)
#dend_animal_sheltered_end <- as.ggdend(mca_animal_sheltered_end$ggdendrogram$plot_env$dend)
ggplot(start_test2, horiz = TRUE) +
labs(y = NULL, title = 'Animal Functional groups: Protected 1982-1995') +
ylim(.25,-.12) +
# manually set colors
scale_color_manual(values = c('#5484c4','#bcbcd2','#543414','#374658', '#a08b5a', 'black')) +
theme(axis.ticks = element_blank(),
axis.text.y = element_blank()
)
#ggsave(filename = here(plots, 'hcpc', '20220321_animal_dend_protected-start.png'), device = 'png',
# height = 5, width = 7, units = 'in')
ggplot(end_test2, horiz = TRUE) +
labs(y = NULL, title = 'Animal functional groups: protected 2010-present') +
ylim(.3,-.12) +
# manually set colors
scale_color_manual(values = c('#a08b5a','#543414','#bcbcd2','#5484c4', '#374658', 'black')) +
theme(axis.ticks = element_blank(),
axis.text.y = element_blank()
)
ggsave(filename = here(plots, 'hcpc', '20220321_animal_protected-end_dend.png'), device = 'png',
height = 5, width = 7, units = 'in')
start_cluster2 +
#manually set colors
scale_fill_manual(values = c('#543414', '#a08b5a', '#5484c4', '#bcbcd2')) +
scale_color_manual(values = c('#543414', '#a08b5a', '#5484c4', '#bcbcd2')) +
labs(title = 'Animal functional groups: protected 1982-1995') +
theme_classic() +
theme(axis.text = element_blank(),
legend.position = 'none')
ggsave(filename = here(plots, 'hcpc', '20220321_animal_protected-start_cluster.png'), device = 'png',
height = 5, width = 7, units = 'in')
mca_animal_sheltered_end$ggclusterplot +
#manually set colors
scale_fill_manual(values = c('#a08b5a','#543414','#bcbcd2')) +
scale_color_manual(values = c('#a08b5a','#543414','#bcbcd2')) +
labs(title = 'Animal functional groups: protected 2010-present') +
theme_classic() +
theme(axis.text = element_blank(),
legend.position = 'none')
ggsave(filename = here(plots, 'hcpc', '20220321_animal_protected-end_cluster.png'), device = 'png',
height = 5, width = 7, units = 'in')
dend_animal_exposed <- as.ggdend(mca_animal_exposed_end$ggdendrogram$plot_env$dend)
ggplot(dend_animal_exposed, horiz = TRUE) +
labs(y = NULL, title = 'Animal functional groups: exposed 2010-present') +
ylim(.3,-.12) +
# manually set colors
scale_color_manual(values = c('#a08b5a','#543414','#bcbcd2','black')) +
theme(axis.ticks = element_blank(),
axis.text.y = element_blank()
)
ggsave(filename = here(plots, 'hcpc', '20220321_animal_exposed-end_dend.png'), device = 'png',
height = 5, width = 7, units = 'in')
```
## MCA plots
```{r algae-scratch}
fviz_mca_var(mca_animal_sheltered_end$mcares, col.var = 'cos2', alpha.var = 'contrib',
gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"),
select.var = list(cos2 = .4),
repel = TRUE, # Avoid text overlapping
ggtheme = theme_minimal())
mca_algae_sheltered_middle$vars_df
#'space_use_sub_canopy', 'crustose_morphology_growth_form', 'leathery_external_morphology', 'erect_morphology_growth_form'
#('branched_external_morphology', 'leathery_external_morphology', 'erect_morphology_growth_form')
```
```{r algae-all-mca}
library(ggrepel)
#added scavenger and filter/suspension to >.6 cos2
# make subset of vars to plot arrows for
arrows1 <- algae_all$vars_df %>%
filter(Variable %in% c('branched_external_morphology', 'leathery_external_morphology')) %>%
`row.names<-`(c('Not Leathery', 'Leathery', 'Not Branched', 'Branched'))
# exposed
ggplot(data.frame(algae_all$mcares$ind$coord), aes(x = Dim.1, y = Dim.2)) +
geom_hline(yintercept = 0, colour = "gray90") +
geom_vline(xintercept = 0, colour = "gray90") +
geom_point(colour = "gray50", alpha = 0.7) +
geom_density2d(colour = "gray87") +
geom_text_repel(data = arrows1, aes(x = Dim.1, y = Dim.2,
label = rownames(arrows1),
colour = Variable), seed = 3824) +
labs(title = "Algae: all", x = NULL, y = NULL) +
scale_colour_manual(values = c('#806238','#5484c4','purple')) +
geom_segment(data = arrows1, aes(x = 0, y = 0, xend = Dim.1, yend = Dim.2),
arrow = arrow(length = unit(0.2, "cm")), colour = "black") +
xlim(-1.25,1.8) +
ylim(-1.15,1.7) +
theme_classic() +
theme(axis.text = element_blank(),
legend.position = 'none')
ggsave(filename = here(plots, '20220525_algae_all.png'), device = 'png',