Pacotes necessários

Precisaremos dos seguintes pacotes antes das análises. Se não os tiver instalado, use o seguinte código antes. Note que é necessário que a internet esteja ligada.

install.packages("tidyverse")
install.packages("vegan")
install.packages("dendextend")
install.packages("ggrepel")
install.packages("factoextra")
install.packages("ggdendro")

Depois de instalado, carregue os pacotes no seu computador.

library(tidyverse)
library(vegan)
library(dendextend)
library(ggrepel)
library(factoextra)
library(ggdendro)

Carregar os dados

Carregue os seus dados de comunidades (locais nas linhas e espécies nas colunas) usando o comando read.csv (se estiver em usando windows provavelmente precisará trocar o read.csv por read.csv2). Aqui vamos usar os dados de exemplo do pacote vegan, para que todos possam acompanhar.

# Carregar os dados do pacote.
data(varespec)

Veja como se a tabela está correta.

varespec
Callvulg Empenigr Rhodtome Vaccmyrt Vaccviti Pinusylv Descflex Betupube Vacculig Diphcomp Dicrsp Dicrfusc Dicrpoly Hylosple Pleuschr Polypili Polyjuni Polycomm Pohlnuta Ptilcili Barbhatc Cladarbu Cladrang Cladstel Cladunci Cladcocc Cladcorn Cladgrac Cladfimb Cladcris Cladchlo Cladbotr Cladamau Cladsp Cetreric Cetrisla Flavniva Nepharct Stersp Peltapht Icmaeric Cladcerv Claddefo Cladphyl
18 0.55 11.13 0.00 0.00 17.80 0.07 0.00 0.00 1.60 2.07 0.00 1.62 0.00 0.00 4.67 0.02 0.13 0.00 0.13 0.12 0.00 21.73 21.47 3.50 0.30 0.18 0.23 0.25 0.25 0.23 0.00 0.00 0.08 0.02 0.02 0.00 0.12 0.02 0.62 0.02 0.00 0.00 0.25 0.00
15 0.67 0.17 0.00 0.35 12.13 0.12 0.00 0.00 0.00 0.00 0.33 10.92 0.02 0.00 37.75 0.02 0.23 0.00 0.03 0.02 0.00 12.05 8.13 0.18 2.65 0.13 0.18 0.23 0.25 1.23 0.00 0.00 0.00 0.00 0.15 0.03 0.00 0.00 0.85 0.00 0.00 0.00 1.00 0.00
24 0.10 1.55 0.00 0.00 13.47 0.25 0.00 0.00 0.00 0.00 23.43 0.00 1.68 0.00 32.92 0.00 0.23 0.00 0.32 0.03 0.00 3.58 5.52 0.07 8.93 0.00 0.20 0.48 0.00 0.07 0.10 0.02 0.00 0.00 0.78 0.12 0.00 0.00 0.03 0.00 0.00 0.00 0.33 0.00
27 0.00 15.13 2.42 5.92 15.97 0.00 3.70 0.00 1.12 0.00 0.00 3.63 0.00 6.70 58.07 0.00 0.00 0.13 0.02 0.08 0.08 1.42 7.63 2.55 0.15 0.00 0.38 0.12 0.10 0.03 0.00 0.02 0.00 0.02 0.00 0.00 0.00 0.00 0.00 0.07 0.00 0.00 0.15 0.00
23 0.00 12.68 0.00 0.00 23.73 0.03 0.00 0.00 0.00 0.00 0.00 3.42 0.02 0.00 19.42 0.02 2.12 0.00 0.17 1.80 0.02 9.08 9.22 0.05 0.73 0.08 1.42 0.50 0.17 1.78 0.05 0.05 0.00 0.00 0.00 0.00 0.02 0.00 1.58 0.33 0.00 0.00 1.97 0.00
19 0.00 8.92 0.00 2.42 10.28 0.12 0.02 0.00 0.00 0.00 0.00 0.32 0.02 0.00 21.03 0.02 1.58 0.18 0.07 0.27 0.02 7.23 4.95 22.08 0.25 0.10 0.25 0.18 0.10 0.12 0.05 0.02 0.00 0.00 0.00 0.00 0.02 0.00 0.28 0.00 0.00 0.00 0.37 0.00
22 4.73 5.12 1.55 6.05 12.40 0.10 0.78 0.02 2.00 0.00 0.03 37.07 0.00 0.00 26.38 0.00 0.00 0.00 0.10 0.03 0.00 6.10 3.60 0.23 2.38 0.17 0.13 0.18 0.20 0.20 0.02 0.00 0.00 0.02 0.02 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.15 0.00
16 4.47 7.33 0.00 2.15 4.33 0.10 0.00 0.00 0.00 0.00 1.02 25.80 0.23 0.00 18.98 0.00 0.02 0.00 0.13 0.10 0.00 7.13 14.03 0.02 0.82 0.15 0.05 0.22 0.22 0.17 0.00 0.00 0.00 0.02 0.18 0.08 0.00 0.00 0.03 0.00 0.07 0.00 0.67 0.00
28 0.00 1.63 0.35 18.27 7.13 0.05 0.40 0.00 0.20 0.00 0.30 0.52 0.20 9.97 70.03 0.00 0.08 0.00 0.07 0.03 0.00 0.17 0.87 0.00 0.05 0.02 0.03 0.07 0.10 0.02 0.00 0.02 0.00 0.00 0.00 0.02 0.00 0.00 0.02 0.00 0.00 0.00 0.08 0.00
13 24.13 1.90 0.07 0.22 5.30 0.12 0.00 0.00 0.00 0.07 0.02 2.50 0.00 0.00 5.52 0.00 0.02 0.00 0.03 0.25 0.07 23.07 23.67 11.90 0.95 0.17 0.05 0.23 0.18 0.57 0.02 0.07 0.00 0.07 0.18 0.02 0.00 0.00 0.03 0.02 0.00 0.00 0.47 0.00
14 3.75 5.65 0.00 0.08 5.30 0.10 0.00 0.00 0.00 0.00 0.00 11.32 0.00 0.00 7.75 0.00 0.30 0.02 0.07 0.00 0.00 17.45 1.32 0.12 23.68 0.22 0.50 0.15 0.23 0.97 0.00 0.00 0.00 0.00 0.68 0.02 0.00 0.00 0.33 0.00 0.02 0.00 1.57 0.05
20 0.02 6.45 0.00 0.00 14.13 0.07 0.00 0.00 0.47 0.00 0.85 1.87 0.08 1.35 13.73 0.07 0.05 0.00 0.12 0.00 0.00 6.80 11.22 0.05 4.75 0.03 0.12 0.22 0.18 0.07 0.00 0.02 0.00 0.02 0.15 0.00 0.00 0.02 0.00 0.00 0.00 0.00 1.20 0.00
25 0.00 6.93 0.00 0.00 10.60 0.02 0.10 0.02 0.05 0.07 14.02 10.82 0.00 0.02 28.77 0.00 6.98 0.13 0.00 0.22 0.00 6.00 2.25 0.00 0.80 0.12 0.57 0.17 0.15 0.07 0.00 0.00 0.00 0.02 0.03 0.02 0.00 4.87 0.10 0.07 0.00 0.02 0.05 0.00
7 0.00 5.30 0.00 0.00 8.20 0.00 0.05 0.00 8.10 0.28 0.00 0.45 0.03 0.00 0.10 0.00 0.25 0.00 0.03 0.00 0.00 35.00 42.50 0.28 0.35 0.08 0.20 0.25 0.18 0.13 0.08 0.00 0.00 0.00 0.05 0.00 0.23 0.20 0.93 0.00 0.03 0.00 0.10 0.00
5 0.00 0.13 0.00 0.00 2.75 0.03 0.00 0.00 0.00 0.00 0.00 0.25 0.03 0.00 0.03 0.18 0.65 0.00 0.00 0.00 0.00 18.50 59.00 0.98 0.28 0.23 0.23 0.23 0.10 0.05 0.00 0.00 0.03 0.00 0.18 0.00 0.28 0.00 10.28 0.00 0.10 0.00 0.25 0.00
6 0.30 5.75 0.00 0.00 10.50 0.10 0.00 0.00 0.00 0.00 0.00 0.85 0.00 0.00 0.05 0.03 0.08 0.00 0.00 0.08 0.00 39.00 37.50 11.30 3.45 0.18 0.20 0.25 0.25 0.23 0.03 0.00 0.00 0.03 0.35 0.00 0.08 0.00 0.03 0.00 0.00 0.00 0.28 0.00
3 0.03 3.65 0.00 0.00 4.43 0.00 0.00 0.00 1.65 0.50 0.00 0.55 0.00 0.00 0.05 0.00 0.00 0.00 0.03 0.03 0.00 8.80 29.50 55.60 0.25 0.08 0.25 0.25 0.15 0.10 0.03 0.00 0.03 0.00 0.05 0.00 0.15 0.15 0.28 0.00 0.00 0.00 0.08 0.00
4 3.40 0.63 0.00 0.00 1.98 0.05 0.05 0.00 0.03 0.00 0.00 0.20 0.00 0.00 1.53 0.00 0.10 0.00 0.05 0.00 0.00 15.73 20.03 28.20 0.73 0.10 0.15 0.13 0.10 0.15 0.00 0.00 0.00 0.05 0.28 0.05 10.03 0.00 0.95 0.00 0.00 0.05 0.08 0.00
2 0.05 9.30 0.00 0.00 8.50 0.03 0.00 0.00 0.00 0.00 0.00 0.03 0.00 0.00 0.75 0.00 0.03 0.00 0.00 0.03 0.00 0.48 24.50 75.00 0.20 0.00 0.03 0.03 0.05 0.03 0.03 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.03 0.03 0.00
9 0.00 3.47 0.00 0.25 20.50 0.25 0.00 0.00 0.00 0.25 0.00 0.38 0.25 0.00 4.07 0.00 0.25 0.00 0.25 0.25 0.00 0.46 4.00 84.30 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.00 0.00 0.25 0.25 0.25 0.67 0.00 0.00 0.00 0.00 0.00 0.25 0.25
12 0.25 11.50 0.00 0.00 15.80 1.20 0.00 0.00 0.00 0.00 0.25 0.25 0.00 0.00 6.00 0.00 0.00 0.00 0.25 0.00 0.00 3.60 14.60 63.30 1.30 0.00 0.25 0.25 0.25 0.25 0.00 0.00 0.00 0.00 0.00 0.25 0.00 0.00 0.00 0.00 0.00 0.00 0.25 0.00
10 0.25 11.00 0.00 0.00 11.90 0.25 0.00 0.00 0.00 0.00 0.00 0.25 0.25 0.00 0.67 0.00 0.25 0.00 0.25 0.00 0.00 1.30 8.70 84.30 0.25 0.25 0.25 0.00 0.25 0.25 0.25 0.00 0.00 0.00 0.25 0.25 0.25 0.00 0.25 0.00 0.00 0.00 0.25 0.25
11 2.37 0.67 0.00 0.00 12.90 0.80 0.00 0.00 0.00 0.00 0.00 0.25 0.25 0.00 17.70 0.25 0.25 0.00 0.25 0.67 0.00 9.67 29.80 31.80 2.53 0.25 0.25 0.25 0.00 0.25 0.00 0.00 0.00 0.00 0.00 0.25 0.00 0.00 0.93 0.25 0.00 0.00 0.00 0.25
21 0.00 16.00 4.00 15.00 25.00 0.25 0.50 0.25 0.00 0.00 0.25 0.25 3.00 0.00 2.00 0.00 0.25 0.25 0.25 10.00 3.00 0.70 4.70 10.90 0.25 0.00 0.05 0.25 0.25 0.25 0.25 0.25 0.00 0.00 0.00 0.67 0.00 0.00 0.00 0.00 0.00 0.00 0.40 0.00

Dissimilaridade

Para calcular as métricas de dissimilaridade entre as localidades, podemos usar a função vegdist do pacote vegan.

vare.dist.jac <- vegdist(varespec, 
                     method = "jaccard")

O resultado é uma matriz de distância triangular. podemos ver como uma matriz da seguinte maneira.

as.matrix(vare.dist.jac)
18 15 24 27 23 19 22 16 28 13 14 20 25 7 5 6 3 4 2 9 12 10 11 21
18 0.0000000 0.6936661 0.8010067 0.7196925 0.5451454 0.6750350 0.7680495 0.6960158 0.9141181 0.5133810 0.7054857 0.5589811 0.7744265 0.5298114 0.6627172 0.5054896 0.6908836 0.6389019 0.7280664 0.7939870 0.6383478 0.7691547 0.6167328 0.7147243
15 0.6936661 0.0000000 0.5291720 0.5770806 0.5350236 0.6264451 0.5271936 0.5690444 0.6864068 0.7549682 0.6489915 0.5490621 0.5047975 0.8036411 0.8357616 0.7771415 0.8622467 0.8126317 0.9014773 0.8739398 0.8091126 0.8665064 0.6409603 0.8641063
24 0.8010067 0.5291720 0.0000000 0.6608590 0.6684692 0.6748225 0.6675594 0.7427412 0.7290827 0.8621330 0.7903426 0.6059699 0.5040127 0.8846176 0.9225155 0.8533013 0.9119861 0.9076932 0.9114195 0.8630238 0.8161854 0.8791015 0.7242760 0.8421053
27 0.7196925 0.5770806 0.6608590 0.0000000 0.6000389 0.6557505 0.6352989 0.7248094 0.4648217 0.8599920 0.8550436 0.6629079 0.6668083 0.8759572 0.9323192 0.8569426 0.8944312 0.9138581 0.8624680 0.8516239 0.7695046 0.8301727 0.7747289 0.7060043
23 0.5451454 0.5350236 0.6684692 0.6000389 0.0000000 0.5300865 0.6498078 0.5809879 0.8221103 0.7670662 0.7217023 0.4468449 0.5973394 0.7798061 0.8436134 0.7694188 0.8323758 0.8316337 0.8405709 0.8019568 0.7000124 0.7970269 0.6404066 0.6623166
19 0.6750350 0.6264451 0.6748225 0.6557505 0.5300865 0.0000000 0.6419025 0.6203786 0.7828430 0.7292910 0.7754059 0.5666962 0.6025028 0.8206553 0.8825695 0.7248175 0.6930459 0.6822689 0.7003891 0.7007202 0.6002785 0.6719708 0.4955046 0.6941670
22 0.7680495 0.5271936 0.6675594 0.6352989 0.6498078 0.6419025 0.0000000 0.4224680 0.7488792 0.8199710 0.6977140 0.6326675 0.5531341 0.8545171 0.9253927 0.8405234 0.8905008 0.8898334 0.9083842 0.8716554 0.8270999 0.8811394 0.7351554 0.8074772
16 0.6960158 0.5690444 0.7427412 0.7248094 0.5809879 0.6203786 0.4224680 0.0000000 0.8245914 0.7109200 0.6510503 0.5441625 0.6019908 0.7949008 0.8361801 0.7886041 0.8122071 0.7851286 0.8475413 0.9002448 0.7761720 0.8672790 0.6818539 0.8554790
28 0.9141181 0.6864068 0.7290827 0.4648217 0.8221103 0.7828430 0.7488792 0.8245914 0.0000000 0.9247383 0.9034888 0.8210022 0.7566944 0.9451589 0.9764372 0.9481678 0.9602009 0.9680705 0.9503083 0.9297183 0.9214356 0.9482875 0.8600133 0.8404972
13 0.5133810 0.7549682 0.8621330 0.8599920 0.7670662 0.7292910 0.8199710 0.7109200 0.9247383 0.0000000 0.7136249 0.7330179 0.8514068 0.6238268 0.6797798 0.5202364 0.6635909 0.5592593 0.7475492 0.8434312 0.7423271 0.8345286 0.5987340 0.8380721
14 0.7054857 0.6489915 0.7903426 0.8550436 0.7217023 0.7754059 0.6977140 0.6510503 0.9034888 0.7136249 0.0000000 0.6768337 0.7131244 0.7916014 0.8410957 0.7675222 0.8787139 0.8048816 0.9242968 0.9060575 0.8576046 0.9073479 0.8054907 0.8948108
20 0.5589811 0.5490621 0.6059699 0.6629079 0.4468449 0.5666962 0.6326675 0.5441625 0.8210022 0.7330179 0.6768337 0.0000000 0.6224525 0.7468013 0.8340794 0.7045892 0.8017120 0.8075260 0.8201661 0.8223161 0.6826875 0.8028504 0.6170379 0.7745422
25 0.7744265 0.5047975 0.5040127 0.6668083 0.5973394 0.6025028 0.5531341 0.6019908 0.7566944 0.8514068 0.7131244 0.6224525 0.0000000 0.8608354 0.9247808 0.8468550 0.8992048 0.9130514 0.8998349 0.8816971 0.8278432 0.8792978 0.7635951 0.8549333
7 0.5298114 0.8036411 0.8846176 0.8759572 0.7798061 0.8206553 0.8545171 0.7949008 0.9451589 0.6238268 0.7916014 0.7468013 0.8608354 0.0000000 0.4891346 0.2985549 0.6802589 0.7180902 0.7853595 0.9082037 0.8229524 0.8698939 0.6895758 0.8847594
5 0.6627172 0.8357616 0.9225155 0.9323192 0.8436134 0.8825695 0.9253927 0.8361801 0.9764372 0.6797798 0.8410957 0.8340794 0.9247808 0.4891346 0.0000000 0.5698491 0.7207722 0.6993989 0.8410901 0.9481757 0.8769496 0.9191638 0.7149666 0.9411444
6 0.5054896 0.7771415 0.8533013 0.8569426 0.7694188 0.7248175 0.8405234 0.7886041 0.9481678 0.5202364 0.7675222 0.7045892 0.8468550 0.2985549 0.5698491 0.0000000 0.6223643 0.6362179 0.7140541 0.8387706 0.7311627 0.7928483 0.5793384 0.8038378
3 0.6908836 0.8622467 0.9119861 0.8944312 0.8323758 0.6930459 0.8905008 0.8122071 0.9602009 0.6635909 0.8787139 0.8017120 0.8992048 0.6802589 0.7207722 0.6223643 0.0000000 0.5286208 0.3469985 0.5596809 0.4179532 0.5089512 0.4618457 0.8576503
4 0.6389019 0.8126317 0.9076932 0.9138581 0.8316337 0.6822689 0.8898334 0.7851286 0.9680705 0.5592593 0.8048816 0.8075260 0.9130514 0.7180902 0.6993989 0.6362179 0.5286208 0.0000000 0.6523951 0.7671322 0.6550772 0.7322565 0.4866789 0.8662970
2 0.7280664 0.9014773 0.9114195 0.8624680 0.8405709 0.7003891 0.9083842 0.8475413 0.9503083 0.7475492 0.9242968 0.8201661 0.8998349 0.7853595 0.8410901 0.7140541 0.3469985 0.6523951 0.0000000 0.3779776 0.3116874 0.2543010 0.5925005 0.8080808
9 0.7939870 0.8739398 0.8630238 0.8516239 0.8019568 0.7007202 0.8716554 0.9002448 0.9297183 0.8434312 0.9060575 0.8223161 0.8816971 0.9082037 0.9481757 0.8387706 0.5596809 0.7671322 0.3779776 0.0000000 0.3709677 0.2009988 0.6794548 0.7462830
12 0.6383478 0.8091126 0.8161854 0.7695046 0.7000124 0.6002785 0.8270999 0.7761720 0.9214356 0.7423271 0.8576046 0.6826875 0.8278432 0.8229524 0.8769496 0.7311627 0.4179532 0.6550772 0.3116874 0.3709677 0.0000000 0.3041317 0.5388770 0.7181243
10 0.7691547 0.8665064 0.8791015 0.8301727 0.7970269 0.6719708 0.8811394 0.8672790 0.9482875 0.8345286 0.9073479 0.8028504 0.8792978 0.8698939 0.9191638 0.7928483 0.5089512 0.7322565 0.2543010 0.2009988 0.3041317 0.0000000 0.6705291 0.7614469
11 0.6167328 0.6409603 0.7242760 0.7747289 0.6404066 0.4955046 0.7351554 0.6818539 0.8600133 0.5987340 0.8054907 0.6170379 0.7635951 0.6895758 0.7149666 0.5793384 0.4618457 0.4866789 0.5925005 0.6794548 0.5388770 0.6705291 0.0000000 0.8033527
21 0.7147243 0.8641063 0.8421053 0.7060043 0.6623166 0.6941670 0.8074772 0.8554790 0.8404972 0.8380721 0.8948108 0.7745422 0.8549333 0.8847594 0.9411444 0.8038378 0.8576503 0.8662970 0.8080808 0.7462830 0.7181243 0.7614469 0.8033527 0.0000000

Podemos calular a distância de bray-curtis mudando o argumento method para “bray”.

vare.dist.bray <- vegdist(varespec, 
                     method = "bray")

Agrupamento

Para fazer um upgma, podemos aplicar a função hclust do pacote stats (que já vem no R) à matriz de distância escolhida.

# Jaccard
h_jac <- hclust(vare.dist.jac, method = "average")
# Bray-Curtis
h_bray <- hclust(vare.dist.bray, method = "average")

Podemos avaliar o nosso dendograma usando uma medida de correlação cofenética.

cor(cophenetic(h_jac), vare.dist.jac)
## [1] 0.8132574
cor(cophenetic(h_bray), vare.dist.bray)
## [1] 0.760622

Podemos plotar isso usando a função plot.

par(mfrow = c(1, 2))
plot(h_jac, ann = F)
mtext("Jaccard", cex = 2)
plot(h_bray, ann = F)
mtext("Bray-Curtis", cex = 2)

Para comparar as duas classificações podemos usar o seguinte código.

# Dendrograms
dend1 <- as.dendrogram(h_jac)
dend2 <- as.dendrogram(h_bray)
# Align and plot two dendrograms side by side
dendlist(dend1, dend2) %>%
  untangle(method = "step1side") %>%
  tanglegram(
    main_left = "Jaccard",
  main_right = "Bray-Curtis",
  lwd = 2,
  highlight_branches_lwd = F,
  )

Para um plot mais organizado de cada um individualmente use o seguitne código.

ddata <- dendro_data(h_jac, type = "rectangle")

p <- ggplot(segment(ddata)) + 
  geom_segment(aes(x = x, y = y, 
                   xend = xend,
                   yend = yend)) + 
  geom_text(data = ddata$labels, 
            aes(x, y, label = label,
            hjust = -0.2, vjust = 0.5), size = 6) +
  scale_color_brewer(palette = "Dark2") +
  coord_flip() + 
  scale_y_reverse(expand = c(0.2, 0)) +
  theme_bw() +
  ylab("Distância de Jaccard") +
  xlab("") +
  ggtitle("Dendograma das áreas",
          subtitle = "Distância de Jaccard") +
  theme(axis.line = element_line(colour = "black"),
        axis.text.y = element_blank(),
        axis.text.x = element_text(size = 12),
        axis.ticks.y = element_blank(),
        panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
        panel.border = element_blank())
p

PCoA

Para fazer a PCoA vamos usar a função cmdscale

pcoa_bray <- cmdscale(vare.dist.bray, k = 2, eig = TRUE)

Para calcular a explicação de cada eixo podemos usar o seguinte código.

porcs <- round(100 * (pcoa_bray$eig / sum(pcoa_bray$eig)),
               2)

porcs
##  [1] 38.62 24.94  9.75  8.14  5.40  4.31  3.85  2.83  2.14  1.67  1.40  1.28
## [13]  0.87  0.38  0.11  0.00 -0.01 -0.14 -0.29 -0.56 -0.83 -1.06 -1.18 -1.63

O plot pode ser feito usando o código abaixo.

mydata <- data.frame(pcoa_bray$points[, 1:2])
g <- ggplot(mydata, aes(x = X1, 
                   y = X2)) +
  geom_point(size = 1) +
  geom_label_repel(aes(label = rownames(mydata))) +
  theme_bw() +
  theme(text = element_text(size = 17)) +
  xlab(paste0("Eixo 1 (", porcs[1], "%)")) +
  ylab(paste0("Eixo 2 (", porcs[2], "%)"))

g