-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathpractice.Rmd
More file actions
1578 lines (1138 loc) · 56.7 KB
/
practice.Rmd
File metadata and controls
1578 lines (1138 loc) · 56.7 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: "Practice"
output:
distill::distill_article:
toc: true
toc_depth: 3
toc_float: true
---
# About
<br />
This tutorial describes the basic workflow showing how to compute step by step
functional diversity (FD) indices in a multidimensional space. It is divided in
three parts:
1. Computing trait-based distances and the multidimensional functional space
2. Using the [`mFD`](https://github.com/CmlMagneville/mFD) package to compute
FD alpha and beta indices and plot them
([Magneville _et al._ 2021](https://doi.org/10.1111/ecog.05904))
3. Using the [`funrar`](https://github.com/rekyt/funrar) package to compute
functional rarity indices
([Violle _et al._ 2017](https://doi.org/10.1016/j.tree.2017.02.002),
[Grenié _et al._ 2017](https://doi.org/10.1111%2Fddi.12629))
<br />
**N.B.** You can chose to do Part 2 and Part 3 in the order that you want but
Part 1 has to be realized first.
<br />
# Prerequisites
Be sure you have followed the [**instructions**](https://frbcesab.github.io/workshop-free/instructions.html)
to set up your system (e.g. `R version >= 3.5`).
If not already done, please install the following R packages:
```{r echo=TRUE, eval=FALSE}
## CRAN packages ----
pkgs <- c("funrar", "mFD")
install.packages(pkgs)
```
<br />
# Data description
The dataset used as study case all along this workshop is the **Fruits dataset**
based on 25 types of fruits (i.e. species) distributed in 10 fruits baskets
(i.e. assemblages). Each fruit is characterized by five traits values
summarized in the following table:
<br />
| Trait name | Trait measurement | Trait type | Number of classes | Classes code | Unit |
|:------------:|:------------------:|:-------------:|:-------------------:|:----------------------------------:|:------:|
| Size | Maximal diameter | Ordinal | 5 | 0-1 ; 1-3 ; 3-5 ; 5-10 ; 10-20 | cm |
| Plant | Growth form | Categorical | 4 | tree ; shrub ; vine ; forb | NA |
| Climate | Climatic niche | Ordinal | 3 | temperate ; subtropical ; tropical | NA |
| Seed | Seed type | Ordinal | 3 | none ; pip ; pit | NA |
| Sugar | Sugar | Continuous | NA | NA | g/kg |
<br />
The use of the `mFD` and `funrar` packages is based on two datasets:
* a **`data.frame`** summarizing traits values for each species called
`fruits_traits` in this tutorial
*Code*
```{r}
## Loading data ----
data("fruits_traits", package = "mFD")
## Removing fuzzy traits in this tutorial ----
fruits_traits <- fruits_traits[ , -c(6:8)]
## Display the table ----
knitr::kable(head(fruits_traits),
caption = "Species x traits data.frame")
```
<br />
* a **`matrix`** summarizing species assemblages called `baskets_fruits_weights`
in this tutorial. Weights in this matrix can be occurrence data, abundance,
biomass, coverage, etc. The studied example works with biomass (*i.e.* grams of
a fruit in a basket) and this matrix looks as follows:
*Code*
```{r}
## Loading data ----
data("baskets_fruits_weights", package = "mFD")
## Display the table ----
knitr::kable(as.data.frame(baskets_fruits_weights[1:6, 1:6]),
centering = TRUE,
caption = "Species x assemblages matrix based on the **fruits** dataset")
```
# Questions
Using this Practice, we ask the following questions:
* How different are the fruits baskets based on their functional traits?
* __*{{ Question functional rarity }}*__
* __*{{ Question funbiogeo }}*__
To answer these three questions, the first step is to **build a functional space based on species traits**
on which functional diversity and functional rarity indices will be then computed.
<br/>
# Part 1. Build a functional space using the `mFD` package
<br/>
## 1.0. Compute summaries about your data
<br/>
This part is not developed in this Practice (not enough time to see
everything ;) ), but it could be useful to know that the `mFD` package can
compute summaries about your traits or assemblage data. For instance, you can
compute a matrix of species occurrence in each assemblage (needed in 2.2).
```{r}
## Summary of the assemblages * species data.frame ----
asb_sp_fruits_summ <- mFD::asb.sp.summary(asb_sp_w = baskets_fruits_weights)
asb_sp_fruits_occ <- asb_sp_fruits_summ$"asb_sp_occ"
head(asb_sp_fruits_occ)
```
<br/>
## 1.1. What about the traits?
<br/>
The first thing to do before starting analyses is to know your data. To do so,
you must be able to characterize the traits you are using (*i.e.* tell the
package what type of traits you are using). That is why `mFD` package needs a
`data.frame` summarizing the type of each trait (*i.e.* each column of the
`fruits_traits` `data.frame`).
*Code*
```{r}
## Loading data ----
data("fruits_traits_cat", package = "mFD")
## Removing fuzzy traits in this tutorial ----
fruits_traits_cat <- fruits_traits_cat[-c(6:8), ]
## Thus remove the "fuzzy_name" column ----
fruits_traits_cat <- fruits_traits_cat[ , -3]
## Displaying the table ----
knitr::kable(head(fruits_traits_cat),
caption = "Traits types based on **fruits & baskets** dataset")
```
The **first column** contains **traits name**. The **second column** contains
**traits type** following this code:
* **N**: nominal trait (factor variable)
* **O**: ordinal traits (ordered variable)
* **C**: circular traits (integer values) (**N.B.** circular traits can not be used in
`mFD` function used to compute functional distance but ok for summary function
and function to group species into Functional Entities)
* **Q**: quantitative traits (numeric values)
* **F**: fuzzy traits (described with several values defined in several columns
in the `fruits_traits` `data.frame`)
<br/>
## 1.2. Computing distances between species based on functional traits
<br/>
The next step toward the computation of functional diversity indices is to
estimate functional traits-based distances between species in order to build the
functional space in which indices will be computed.
To compute trait-based distances, we will use the `mFD::funct.dist()` function
which includes the following arguments:
*Code*
```{r, results = "hide"}
sp_dist_fruits <- mFD::funct.dist(
sp_tr = fruits_traits,
tr_cat = fruits_traits_cat,
metric = "gower",
scale_euclid = "scale_center",
ordinal_var = "classic",
weight_type = "equal",
stop_if_NA = TRUE)
```
* `sp_tr` is the species x trait `data.frame`
* `tr_cat` is the `data.frame` summarizing trait type for each trait
* `metric` is a character string referring to the metric used to compute
distances. Two metrics are available and **the choice depends on your traits
data**:
* if **all traits are continuous** use the **Euclidean distance** (`metric =
"euclidean"`) and check the [Compute Functional Diversity Indices based on
Only Continuous
Traits](https://cmlmagneville.github.io/mFD/articles/Continuous_traits_framework.html)
tutorial which explains how to build a multidimensional space from traits
through PCA analysis or considering directly each trait as a dimension.
* if you have **non-continuous traits** use the **Gower distance** (`metric =
"gower"`) as this method allows traits weighting. This method can also deal
with fuzzy traits.
* `scale_euclid` is a character string referring to the way the user wants to
scale **euclidean** traits. You can either chose to scale by range (`range`), use
the center transformation (`center`), use the scale transformation (`scale`), use
the scale-center transformation (`scale_center`) or you can chose not to scale
(`noscale`).
* `ordinal_var` is a character string specifying the method to be used for
ordinal variables (*i.e.* ordered). You can either chose to treat ordinal
variables as continuous variables (with `"classic"` option) or to treat ordinal
variables as ranks (with `metric` or `podani` options, see `mFD::funct.dist()`
help file for detail).
* `weight_type` is a character string referring to the type of method to weight
traits. You can either chose to define weights using the `tr_cat` `data.frame` (cf.
**step 1.1**) (`user` option) or you can chose to give the same weight to all
traits (`equal` option). (**N.B.** Using `mFD`, you can not define weights for fuzzy
traits, use
[`gawdis`](https://cran.r-project.org/package=gawdis) package
instead)
* `stop_if_NA` is a logical value to stop or not the process if the `sp_tr`
`data.frame` contains `NA`. If the `sp_tr` `data.frame` contains `NA` you can either
chose to compute anyway functional distances (but keep in mind that **Functional
measures are sensitive to missing traits!**) or you can delete species with
missing or extrapolate missing traits (see [Johnson _et al._
(2020)](https://onlinelibrary.wiley.com/doi/full/10.1111/geb.13185)).
This function returns a `dist` object with traits-based distances between all
pairs of species:
*Code*
```{r}
## Output of the function mFD::funct.dist() ----
round(sp_dist_fruits, 3)
```
<br/>
## 1.3. Building functional spaces and chosing the best one
<br/>
### 1.3.1. Computing several multimensional functional spaces and assessing their quality
<br/>
In order to generate a multidimensional space in which functional diversity
indices
are computed ([Mouillot _et al._ 2013](https://www.sciencedirect.com/science/article/pii/S0169534712002650),
we will perform a PCoA using the trait-based distances (and if
required a functional dendrogram).
`mFD` evaluates the quality of PCoA-based multidimensional spaces according
to the deviation between trait-based distances and distances in the functional
space (extension of [Maire _et al._ (2015)](https://onlinelibrary.wiley.com/doi/full/10.1111/geb.12299) framework).
For that, we will use the `mFD::quality.fspaces()` function:
<br />
*Code*
```{r, results = "hide"}
fspaces_quality_fruits <- mFD::quality.fspaces(
sp_dist = sp_dist_fruits,
maxdim_pcoa = 10,
deviation_weighting = "absolute",
fdist_scaling = FALSE,
fdendro = "average")
```
* `sp_dist` is the `dist` object with pairwise trait-based distance between
species as computed in **step 1.2**
* `maxdim_pcoa` is the maximum number of PCoA axes to consider to build
multidimensional spaces. Actually, the maximum number of dimensions considered
depends on the number of PCoA axes with positive eigenvalues.
* `deviation_weighting` refers to the method(s) used to weight the difference
between species pairwise distances in the functional space and trait-based
distances.
**You can chose between**:
* `absolute`: absolute differences are used to compute the **mean absolute
deviation (mad)** . It reflects the actual magnitude of errors that will
affect FD metrics.
* `squared`: squared differences are used to compute the **root of mean square
deviation (rmsd)**. This weighting puts more weight to the large deviations
between trait-based distances and distances in the functional space.
* Both quality metrics can be used with: `deviation_weighting = c("absolute",
"squared")`.
* `fdist_scaling` specifies whether distances in the functional space should be
scaled before computing differences with trait-based distances. Scaling ensures
that trait-based distances and distances in the functional space have the same
maximum. Scaling distances implies that the quality of the functional space
accounts for congruence in distances rather than their equality.
**N.B.** The combination of `deviation_weighting` and `fdist_scaling` arguments leads
to **four possible quality metrics**: `mad`, `rmsd`, `mad_scaled` and
`rmsd_scaled`
* `fdendro` specifies the clustering algorithm to compute a functional
dendrogram. `NULL` means no dendrogram computed. The chosen algorithm must be one
of the method recognized by the `stats::hclust()` function from the
[`stats`](https://www.rdocumentation.org/packages/stats) package.
This function returns a list various objects:
<br />
* a `data.frame` gathering for each space (in rows), values of quality metric(s)
(in columns)
*Code*
```{r}
## Quality metrics of functional spaces ----
round(fspaces_quality_fruits$"quality_fspaces", 3)
```
<br />
* `list` with details required for other tasks in **step 1.4** to plot functional
space quality and in **step 1.5** to plot functional space.
<br />
**N.B.** The space with the best quality has the lowest quality metric. Here, thanks
to mad values, we can see that the 4D space is the best one. That is why the
following of this Practice will use this multidimensional space.
<br/>
### 1.3.2. Illustrating the quality of the functional spaces
<br/>
With the `mFD` package, it is possible to illustrate the quality of PCoA-based
multidimensional spaces according to deviation between trait-based distances and
distances in the functional space. For that, we use the
`mFD::quality.fspace.plot()` function with the following arguments:
<br />
*Code*
```{r, fig.show = 'hide', results = "hide"}
mFD::quality.fspaces.plot(
fspaces_quality = fspaces_quality_fruits,
quality_metric = "mad",
fspaces_plot = c("tree_average", "pcoa_2d", "pcoa_3d",
"pcoa_4d", "pcoa_5d", "pcoa_6d"),
name_file = NULL,
range_dist = NULL,
range_dev = NULL,
range_qdev = NULL,
gradient_deviation = c(neg = "darkblue", nul = "grey80", pos = "darkred"),
gradient_deviation_quality = c(low = "yellow", high = "red"),
x_lab = "Trait-based distance")
```
* `fspaces_quality` is the output of the `mFD::quality.fspaces()` function
(**step 1.3.1**).
* `quality_metric` refers to the quality metric used. It should be one of the
column name(s) of the table gathering quality metric values (output of
`mFD::quality.fspaces()` called `quality_fspaces`) (here:
`fspaces_quality_fruits$quality_fspaces`) Thus it can be: `mad`, `rmsd`,
`mad_scaled` or `rmsd_scaled` (see **step 1.3.1**)
* `fspaces_plot` refers to the names of spaces for which quality has to be
illustrated (up to 10). Names are those used in the output of
`mFD::quality.fspaces()` function showing the values of the quality metric.
* `name_file` refers to the name of file to save (without extension) if the user
wants to save the figure. If the user only wants the plot to be displayed,
then `name_file = NULL`.
* `range_dist`, `range_dev`, `range_qdev` are arguments to set ranges of panel
axes (check function help for further information).
* `gradient_deviation` and `gradient_deviation_quality` are arguments to set
points colors (check function help for further information).
* `xlab` is a parameter to set x-axis label.
<br />
This function generates a figure with three panels (in rows) for each selected
functional space (in columns). Each column represents a functional space, the
value of the quality metric is written on the top of each column. The x-axis of
all panels represents trait-based distances. The y-axis is different for each
row:
* on the first (top) row, the y-axis represents species functional distances in
the multidimensional space. Thus, the closer species are to the 1:1 line,
the better distances in the functional space fit trait-based ones.
* on the second row, the y-axis shows the raw deviation of species distances in
the functional space compared to trait-based distances. Thus, the raw deviation
reflects the distance to the horizontal line.
* on the third row (bottom), the y-axis shows the absolute or squared deviation of the
("scaled") distance in the functional space. It is the deviation that is taken
into account for computing the quality metric.
*Code*
```{r, fig.height = 7, fig.width = 12, fig.align = "center"}
mFD::quality.fspaces.plot(
fspaces_quality = fspaces_quality_fruits,
quality_metric = "mad",
fspaces_plot = c("tree_average", "pcoa_2d", "pcoa_3d",
"pcoa_4d", "pcoa_5d", "pcoa_6d"),
name_file = NULL,
range_dist = NULL,
range_dev = NULL,
range_qdev = NULL,
gradient_deviation = c(neg = "darkblue", nul = "grey80", pos = "darkred"),
gradient_deviation_quality = c(low = "yellow", high = "red"),
x_lab = "Trait-based distance")
```
<br />
For the 2D space, on the top row there are a lot of points below the 1:1 lines,
meaning that distances are overestimated in this multidimensional space. Looking
at panels, we can see that the 4D space is the one in which points are the
closest to the 1:1 line on the top row,and the closest to the x-axis for the two
bottom rows, which reflects a better quality compared to other functional spaces
/ dendrogram. For the dendrogram, we can see on the top row that species pairs
arrange in horizontal lines, meaning that different trait-based distances have
then the same cophenetic distance on the dendrogram.
<br/>
### 1.3.3. Testing the correlation between functional axes and traits
<br />
`mFD` allows to test for correlations between traits and functional axes and
then illustrate possible correlations (continuous traits = linear model is
computed and r^2^ and associated p-value are returned; non-continuous traits =
Kruskal-Wallis test is computed and eta2 statistic is returned). The function
`mFD::traits.faxes.cor()` allows to test and plot correlation and needs the
following arguments:
* `sp_tr` is the species x traits `data.frame`
* `sp_faxes_coord` is a `matrix` of species coordinates taken from the outputs of
the `mFD::quality.fspaces()` function **with columns representing axes on which
functional space must be computed**. For instance, in this tutorial, we will
plot the functional space for 4 and 10 dimensions (*cf.* the two examples
below). The whole `sp_faxes_coord` can be retrieved through the output of the
`mFD::quality.fspaces()` function:
*Code*
```{r, results = "hide"}
sp_faxes_coord_fruits <- fspaces_quality_fruits$"details_fspaces"$"sp_pc_coord"
```
* `plot` is a logical value indicating whether correlations should be
illustrated or not. If this option is set to `TRUE`, traits-axis relationships
are plotted through scatterplot for continuous traits and boxplot for
non-continuous traits.
The function `mFD::traits.faxes.cor()` works as follows:
<br />
*Code*
```{r, results = "hide"}
fruits_tr_faxes <- mFD::traits.faxes.cor(
sp_tr = fruits_traits,
sp_faxes_coord = sp_faxes_coord_fruits[ , c("PC1", "PC2", "PC3", "PC4")],
plot = TRUE)
```
We can print only traits with significant effect on position along one of the
axis and look at the plots:
*Code*
```{r, fig.height = 7, fig.width = 12, fig.align = "center"}
## Print traits with significant effect ----
fruits_tr_faxes$"tr_faxes_stat"[which(fruits_tr_faxes$"tr_faxes_stat"$"p.value" < 0.05), ]
## Plot ----
fruits_tr_faxes$"tr_faxes_plot"
```
<br />
We can thus see that **PC1** is mostly driven by *Climate* (temperate on the
left and tropical on the right) and *Plant Type* (forb & shrub on the left vs
tree & vine on the right) and *Size* (large fruits on the right) with weaker
influence of *Seed* (eta2 < 0.25).
Then, **PC2** is mostly driven by *Seed* (no seed on the left and pit seed on
the right) with weaker influence of *Plant Type*. **PC3** is driven by only one
trait, *Size*. And finally **PC4** is mostly driven by *Sugar* (high sugar
content on the right and low sugar content on the left) with a weaker influence
of *Plant Type*.
<br />
## 1.4. Plotting the selected functional space and position of species
<br/>
Once the user has selected the dimensionality of the functional space, `mFD`
allows you to plot the given multidimensional functional space and the position
of species in all 2-dimensions spaces made by pairs of axes.
<br />
The `mFD::funct.space.plot()` function allows to illustrate the position of all
species along pairs of space axes.
<br />
This function allows to plot with many possibilities to change colors/shapes of
each plotted element. Here are listed the main arguments:
* `sp_faxes_coord` is a `matrix` of species coordinates taken from the outputs of
the `mFD::quality.fspaces()` function **with columns representing axes on which
functional space must be computed**. For instance, in this tutorial, we will
plot the functional space for 4 and 10 dimensions (*cf.* the two examples
below). The whole `sp_faxes_coord` can be retrieved through the output of the
`mFD::quality.fspaces()` function:
<br />
*Code*
```{r, results = "hide"}
sp_faxes_coord_fruits <- fspaces_quality_fruits$"details_fspaces"$"sp_pc_coord"
```
<br />
* `faxes` is a `vector` containing names of axes to plot. If set to `NULL`, the
first four functional axes will be plotted.
* `faxes_nm` is a `vector` containing labels of `faxes` (following faxes vector
rank). If `NULL`, labels follow `faxes` vector names.
* `range_faxes` is a `vector` to complete if the user wants to set specific
limits for functional axes. If `range_faxes = c(NA, NA)`, the range is
computed according to the range of values among all axes.
* `plot_ch` is a `logical` value used to draw or not the 2D convex-hull filled
by the global pool of species. Color, fill and opacity of the convex hull
can be chosen through other inputs, please refer to the function's help.
* `plot_sp_nm` is a `vector` containing species names to plot. If `NULL`, no
species names plotted. Name size, color and font can be chosen through other
inputs, please refer to the function's help.
* `plot_vertices` is a `logical` value used to plot or not vertices with a
different shape than other species. **Be careful** these representations are
2D representations, thus vertices of the convex-hull in the n-multidimensional
space can be close to the center of the hull projected in 2D. Color, fill,
shape and size of vertices can be chosen through other inputs, please refer to
the function's help.
* `check_input` is a recurrent argument in the `mFD` package. It defines
whether inputs should be checked before computation or not. Possible error
messages will thus be more understandable for the user than R error messages
(**Recommendation:** set it as `TRUE`).
* other inputs are used to chose color, fill, size, and shape of species from
the global pool, please refer to the function's help.
<br />
Here are the plots for the *fruits & baskets* dataset for the **first four PCoA
axis**:
<br />
*Code*
```{r, fig.height = 7, fig.width = 12, fig.align = "center"}
big_plot <- mFD::funct.space.plot(
sp_faxes_coord = sp_faxes_coord_fruits[ , c("PC1", "PC2", "PC3", "PC4")],
faxes = c("PC1", "PC2", "PC3", "PC4"),
name_file = NULL,
faxes_nm = NULL,
range_faxes = c(NA, NA),
plot_ch = TRUE,
plot_vertices = TRUE,
plot_sp_nm = NULL,
check_input = TRUE)
big_plot$"patchwork"
```
<br />
Here, the convex-hull of the species pool is plotted in white and axis have the
same range to get rid of bias based on different axis scales. Species being
vertices of the 4D convex hull are in purple.
<br/>
# Part 2. Computing and plotting FD indices using the `mFD` package
<br />
The `mFD::alpha.fd.multidim()` function allows computing alpha and beta FD indices.
<br />
## 2.1. Computing and plotting alpha FD indices
<br />
Using the `alpha.fd.multidim()` function, you can compute up to nine alpha FD indices:
* `FDis` **Functional Dispersion**: the biomass weighted deviation of species
traits values from the center of the functional space filled by the assemblage
*i.e.* the biomass-weighted mean distance to the biomass-weighted mean trait
values of the assemblage.
* `FRic` **Functional Richness**: the proportion of functional space filled by
species of the studied assemblage, *i.e.* the volume inside the convex-hull
shaping species. To compute `FRic` the number of species must be at least
higher than the number of functional axis + 1.
* `FDiv` **Functional Divergence**: the proportion of the biomass supported by
the species with the most extreme functional traits *i.e.* the ones located
close to the edge of the convex-hull filled by the assemblage.
* `FEve` **Functional Evenness**: the regularity of biomass distribution in
the functional space using the Minimum Spanning Tree linking all species
present in the assemblage.
* `FSpe` **Functional Specialization**: the biomass weighted mean distance to
the mean position of species from the global pool (present in all
assemblages).
* `FMPD` **Functional Mean Pairwise Distance**: the mean weighted distance
between all species pairs.
* `FNND` **Functional Mean Nearest Neighbour Distance**: the weighted distance
to the nearest neighbor within the assemblage.
* `FIde` **Functional Identity**: the mean traits values for the assemblage.
`FIde` is always computed when `FDis` is computed.
* `FOri` **Functional Originality**: the weighted mean distance to the nearest
species from the global species pool.
<br />
**A cheat sheet on alpha FD indices is available [here](biblio/FD_alpha_full.pdf)**
<br />
In this Practice we will only compute five of them.
The function is used as follow:
*Code*
```{r, results = "hide"}
alpha_fd_indices_fruits <- mFD::alpha.fd.multidim(
sp_faxes_coord = sp_faxes_coord_fruits[ , c("PC1", "PC2", "PC3", "PC4")],
asb_sp_w = baskets_fruits_weights,
ind_vect = c("fdis", "fric", "fdiv",
"fspe", "fide"),
scaling = TRUE,
check_input = TRUE,
details_returned = TRUE)
```
<br />
The arguments and their use are listed below:
* `sp_faxes_coord` is the species coordinates matrix. This dataframe gathers
only axis of the functional space you have chosen based on **step 4**.
* `asb_sp_w` is the matrix linking species and assemblages they belong to
(summarized in **step 1**).
* `ind_vect` is a vector with names of diversity functional indices to compute.
* `scaling` is a logical value indicating whether indices should be scaled
between 0 and 1. If scaling is to be done, this argument must be set to `TRUE`.
* `check_input` is a recurrent argument in the `mFD` package. It defines whether
inputs should be checked before computation or not. Possible error messages will
thus be more understandable for the user than R error messages
(**Recommendation:** set it as `TRUE`).
* `details_returned` is used if the user wants to store information that are
used in graphical functions. If the user wants to plot FD indices, then
`details_returned` must be set to `TRUE`.
<br />
**N.B.** **Use lowercase letters to enter FD indices names**
<br />
The function has two main outputs:
* a **`data.frame` gathering indices values in each assemblage** (for `FIde`
values, there are as many columns as there are axes to the studied functional
space).
*Code*
```{r}
fd_ind_values_fruits <- alpha_fd_indices_fruits$"functional_diversity_indices"
fd_ind_values_fruits
```
<br />
* a **details list** of `data.frames` and `lists` gathering information such as
coordinates of centroids, distances and identity of the nearest neighbour,
distances to the centroid, etc. The user does not have to directly use it but it
will be useful if FD indices are then plotted. It can be retrieved through:
*Code*
```{r, results = "hide"}
details_list_fruits <- alpha_fd_indices_fruits$"details"
```
<br />
Then, you can plot functional indices using the `mFD::alpha.multidim.plot()` for
up to two assemblages:
<br />
*Code*
```{r, results = "hide", fig.show = 'hide', message = FALSE, fig.height = 7, fig.width = 12, fig.align = "center"}
plots_alpha <- mFD::alpha.multidim.plot(
output_alpha_fd_multidim = alpha_fd_indices_fruits,
plot_asb_nm = c("basket_1", "basket_5"),
ind_nm = c("fdis", "fric", "fdiv",
"fspe", "fide"),
faxes = NULL,
faxes_nm = NULL,
range_faxes = c(NA, NA),
plot_sp_nm = NULL,
save_file = FALSE,
check_input = TRUE)
```
<br />
This function has a lot of arguments: most of them are graphical
arguments allowing the user to chose colors, shapes, sizes, scales, etc. This
tutorial only presents main arguments. To learn about the use of graphical
arguments, check the function help file. The main arguments of this function are
listed below:
* `output_alpha_fd_multidim` is the output of the ``mFD::alpha.fd.multidim()`
function.
* `plot_asb_nm` is a vector gathering name(s) of assemblage(s) to plot.
* `ind_vect` is a vector gathering FD indices to plot. Plots are available for
`FDis`, `FIde`, `FEve`, `FRic`, `FDiv`, `FOri`, `FSpe`, and `FNND.`
* `faxes` is a vector containing names of axes to plot. You can only plot from
two to four axes labels for graphical reasons.
* `faxes_nm` is a vector with axes labels if the user ants different axes labels
than `faxes` ones.
* `range_faxes` is a vector with minimum and maximum values for axes.
If `range_faxes = c(NA, NA)`, the range is computed according to the range
of values among all axes, all axes having thus the same range. To have a fair
representation of species positions in all plots, all axes must have the same
range.
* `plot_sp_nm` is a vector containing species names to plot. If `NULL`, then no
name is plotted.
* `check_input` is a recurrent argument in `mFD`. It defines whether inputs
should be checked before computation or not. Possible error messages will thus
be more understandable for the user than R error messages (**Recommendation:**
set it as `TRUE`.
* size, color, fill, and shape arguments for each component of the graphs *i.e.*
species of the global pool, species of the studied assemblage(s), vertices,
centroids and segments. If you have to plot two assemblages, then inputs
should be formatted as follow: `c(pool = ..., asb1 = ..., asb2 = ...)` for
inputs used for global pool and studied assemblages and
`c(asb1 = ..., asb2 = ...)` for inputs used for studied assemblages only.
<br />
Then, using these arguments, here are the output plots for the **fruits &
baskets** dataset:
<br />
* `FRic` representation: the colored shapes reflect the convex-hull of the
studied assemblages and the white shape reflects the convex-hull of the global
pool of species:
*Code*
```{r, fig.height = 15, fig.width = 20, fig.align = "center", warning = FALSE}
plots_alpha$"fric"$"patchwork"
```
* `FDiv` representation: the gravity centers of **vertices** (i.e. species with the
most extreme functional traits) of each assemblages are plotted as a square and
a triangle. The two colored circles represent the mean distance of species to
the gravity center for each assemblage. Species of each assemblage have
different size given their relative weight into the assemblage.
*Code*
```{r, fig.height = 15, fig.width = 20, fig.align = "center", warning = FALSE}
plots_alpha$"fdiv"$"patchwork"
```
* `FSpe` representation: colored traits represent distances of each species from
a given assemblage to the center of gravity of the global pool (i.e center of
the functional space). the center of gravity is plotted with a purple diamond.
Species of each assemblage have different size given their relative weight into
the assemblage.
*Code*
```{r, fig.height = 15, fig.width = 20, fig.align = "center", warning = FALSE}
plots_alpha$"fspe"$"patchwork"
```
* `FDis` representation: colored traits represent distances of each species from
a given assemblage to the center of gravity of species of the assemblage
(defined by FIde values). The center of gravity of each assemblage is plotted
using a square and a triangle. Species of each assemblage havedifferent size
given their relative weight into the assemblage.
*Code*
```{r, fig.height = 15, fig.width = 20, fig.align = "center", warning = FALSE}
plots_alpha$"fdis"$"patchwork"
```
* `FIde` representation:colored lines refer to the weighted average position of
species of each assemblage along each axis. Species of each assemblage have
different size given their relative weight into the assemblage.
*Code*
```{r, fig.height = 15, fig.width = 20, fig.align = "center", warning = FALSE}
plots_alpha$"fide"$"patchwork"
```
**N.B.** Using the `mFD` package, you can plot more than two assemblages but not
with the `alpha.multidim.plot()` function. There are several specific functions
for each step of the plot: build the background of the plot (`background.plot()`),
plot the pool of species you are working on (`pool.plot()`),
plot species from the studied assemblages (`species.plot()`) function and lastly
plot the wanted metric using related function (`fric.plot()`, `fdiv.plot()`,
`fide.plot()`,`fdis.plot()`, `feve.plot()`, `fnnd.plot()`, `fori.plot()`,
`fspe.plot()`). Plots for different axes combination can be gathered into a
single plot using the `panels.to.patchwork()` function.
<br/>
## 2.2. Computing and plotting beta FD indices
<br/>
**N.B.** Some Mac OS X 10.15 may encounter some issues with the beta_*()
functions.
`mFD` package allows you to compute beta diversity indices for each assemblage
pairs following [Villeger _et al._
2013](https://onlinelibrary.wiley.com/doi/full/10.1111/geb.12021). For that we
will use the `mFD::beta.fd.multidim()` function. This function can compute two
families of functional beta diversity indices, either **Jaccard** or **Sorensen**.
In this example, we will use Jaccard index. For each assemblages pair, the
dissimilarity index is decomposed into two additive components: **turnover** and
**nestedness-resultant**.
**NB** The **turnover** component is the highest if there
is no shared traits combination between the two assemblages.
The **nestedness** component is the highest if one assemblage hosts a small
subset of the functional strategies present in the other.
The `mFD::beta.fd.multidim()` function has the main following arguments:
*Code*
```{r, results = "hide", message=FALSE}
beta_fd_indices_fruits <- mFD::beta.fd.multidim(
sp_faxes_coord = sp_faxes_coord_fruits[ , c("PC1", "PC2", "PC3", "PC4")],
asb_sp_occ = asb_sp_fruits_occ,
check_input = TRUE,
beta_family = c("Jaccard"),
details_returned = TRUE)
```
* `sp_faxes_coord` is the species coordinates matrix. This dataframe gathers
**only** axis of the functional space you have chosen based on **step 4**.
* `asb_sp_occ` is the matrix of occurrence (coded as 0/1) of species assemblages
(summarized in **step 1**).
* `check_input` is a recurrent argument in the `mFD` package. It defines whether
inputs should be checked before computation or not. Possible error messages will
thus be more understandable for the user than R error messages (**Recommendation:**
set it as `TRUE`.
* `beta_family` a character string for the type of beta-diversity index to
compute, it can either be `Jaccard` or `Sorensen`.
* `details_returned` is a logical value indicating whether details of outputs
must be stored. It should be stored if you plan to use the graphical function to
illustrate beta diversity indices thereafter.
* There are also other arguments for parallelisation options. Check the function
help file for more explanation.
<br />
The function returns a list containing:
* a dist object with beta indices values for each pair of assemblages:
*Code*
```{r}
head(beta_fd_indices_fruits$"pairasb_fbd_indices", 10)
```
* a list containing details such as inputs, vertices of the global pool and of
each assemblage and FRic values for each assemblage
*Code*
```{r}
beta_fd_indices_fruits$"details"
```
* a vector containing the `FRic` value for each assemblage retrieved through
the `details_beta` list:
*Code*
```{r}
beta_fd_indices_fruits$"details"$"asb_FRic"
```