Trait_based_Fungi_Ecology

#inspecting dataset
str(traits_data)
## 'data.frame':    31 obs. of  45 variables:
##  $ X            : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ strainID     : chr  "A" "C11" "C13" "C21" ...
##  $ newID        : chr  "RLCS21" "RLCS23" "RLCS06" "RLCS26" ...
##  $ Phylum       : chr  "Ascomycota" "Ascomycota" "Ascomycota" "Ascomycota" ...
##  $ Species      : chr  "Pyrenochaetopsis_leptospora" "Paramyrothecium_sp." "Chaetomium_angustispirale1" "Tetracladium_apiense" ...
##  $ extension    : num  -0.862 -0.811 1.511 -0.934 0.577 ...
##  $ density      : num  1.014 0.372 -0.798 0.989 -0.467 ...
##  $ biomassst    : num  -0.0289 -0.2348 0.5766 -0.4953 0.9904 ...
##  $ biomassopt   : num  0.478 -0.26 1.065 -0.691 1.129 ...
##  $ biomasscomplC: num  0.305 -0.211 1.028 -0.356 0.227 ...
##  $ hyphal_diam  : num  3.96 4.84 4.39 4.3 4.57 ...
##  $ melanin      : num  0.449 0.209 0.173 0.31 0.246 ...
##  $ water_cont   : num  0.904 0.864 0.759 0.823 0.773 ...
##  $ hydrop       : num  0 53.3 39.6 0 51.7 ...
##  $ DNAc         : num  108.9 36.7 189.4 19.5 79.2 ...
##  $ PLFAc        : num  32526 16246 21798 15757 23154 ...
##  $ stoich_C_XPC1: num  -1.23 0.579 1.299 -1.941 2.714 ...
##  $ stoich_N_XPC1: num  0.3188 1.6917 -1.1405 -1.1095 -0.0798 ...
##  $ stoich_N_XPC2: num  -0.534 -0.577 0.104 -0.169 -0.371 ...
##  $ C_cont       : num  46.9 50.9 46.6 44.4 52.7 ...
##  $ enz_la       : num  0.00535 3.91857 0.00944 10.49831 0.01295 ...
##  $ enz_leu      : num  1.4357 0.6278 0.5993 0.5332 0.0865 ...
##  $ enz_cel      : num  2.312 1.04 0.304 1.476 1.078 ...
##  $ enz_pho      : num  1.482 0.418 0.237 0.1 0.455 ...
##  $ complC_use   : num  1.73 1.64 2.52 1.53 1.73 ...
##  $ enz_C_div    : int  5 5 3 6 5 7 5 3 5 2 ...
##  $ spore_abund  : num  -1.639 0.961 0.886 -1.639 -0.854 ...
##  $ spore_shape  : num  3.83 1.7 1.33 1.97 2.77 ...
##  $ spore_size   : num  8.6 16.5 41 41.9 88 ...
##  $ spore_RRx    : num  0 0.335 1.337 0 12.175 ...
##  $ stoich_flex  : num  0.625 0.514 0.082 0.328 0.602 0.645 0.344 0.183 0.433 0.415 ...
##  $ recycling    : num  4.11 5.07 2.39 2.46 3.75 ...
##  $ asegurl      : num  0.0717 0.2313 -0.0939 0.1454 0.1191 ...
##  $ WA_explor    : num  -0.12518 -0.11072 -0.27802 -0.03186 0.00505 ...
##  $ comp_glu     : num  -1.478 -0.377 2.167 -1.76 2.794 ...
##  $ fungic_str   : num  -4.5515 -7.0901 0.3719 -3.6662 0.0854 ...
##  $ cu_str       : num  -0.984 -0.122 -0.548 -0.474 -0.242 ...
##  $ drought_str  : num  -3.704 -4.335 -1.372 -1.814 -0.777 ...
##  $ stress_tolav : num  -0.8939 -0.387 0.4809 0.0508 0.8679 ...
##  $ stress_tolPC1: num  -1.2518 -1.3136 1.0154 0.0135 1.4083 ...
##  $ soil_aggreg  : num  6.07 7.7 5.05 8.87 4.94 ...
##  $ leaf_decomp  : num  33.7 40.5 33.3 37.5 25.9 ...
##  $ wood_decomp  : num  8.54 8.34 60.35 1.4 1.76 ...
##  $ decompPC1    : num  0.767 1.075 1.792 0.172 -0.297 ...
##  $ CUE          : num  0.785 0.526 0.809 0.65 0.731 ...
#removing NA rows
traits_data<-na.omit(traits_data)

#extract trait variables and trait matrix
fungal_traits <- colnames(traits_data)[6:45]
#exclude Species column
trait_matrix <- traits_data[,fungal_traits]

For PCA (and clustering based on Euclidean distance), it’s crucial to scale the traits so that they are comparable. Typically we scale each trait to mean 0 and standard deviation 1 (also known as z-scores).

# Scale the trait variables (exclude Species column)
trait_data_scaled <- scale(trait_matrix) 

# Check that scaling worked:
colMeans(trait_data_scaled,na.rm = TRUE)  # should be ~0 for each trait
##     extension       density     biomassst    biomassopt biomasscomplC 
##  1.982541e-17 -1.586033e-17  6.195441e-18  6.542386e-17 -3.766828e-17 
##   hyphal_diam       melanin    water_cont        hydrop          DNAc 
## -9.278292e-16  7.137148e-17 -4.310044e-15  1.744636e-16  2.299748e-16 
##         PLFAc stoich_C_XPC1 stoich_N_XPC1 stoich_N_XPC2        C_cont 
##  3.965082e-16  3.965082e-18  1.090398e-17  2.379049e-17 -2.652640e-15 
##        enz_la       enz_leu       enz_cel       enz_pho    complC_use 
##  3.271193e-17 -9.317943e-17 -5.551115e-17 -7.930164e-18 -1.744636e-16 
##     enz_C_div   spore_abund   spore_shape    spore_size     spore_RRx 
##  2.379049e-16 -7.137148e-17 -1.546382e-16 -1.903239e-16  1.313433e-17 
##   stoich_flex     recycling       asegurl     WA_explor      comp_glu 
##  3.172066e-17  3.608225e-16  3.568574e-17  9.516197e-17  7.930164e-18 
##    fungic_str        cu_str   drought_str  stress_tolav stress_tolPC1 
## -1.110223e-16  7.930164e-18  9.119689e-17  7.930164e-18  3.766828e-17 
##   soil_aggreg   leaf_decomp   wood_decomp     decompPC1           CUE 
## -2.374093e-16 -8.946217e-17  4.559845e-17  2.354268e-17 -5.253734e-16
apply(trait_data_scaled, 2, sd)    # should be ~1 for each trait
##     extension       density     biomassst    biomassopt biomasscomplC 
##             1             1             1             1             1 
##   hyphal_diam       melanin    water_cont        hydrop          DNAc 
##             1             1             1             1             1 
##         PLFAc stoich_C_XPC1 stoich_N_XPC1 stoich_N_XPC2        C_cont 
##             1             1             1             1             1 
##        enz_la       enz_leu       enz_cel       enz_pho    complC_use 
##             1             1             1             1             1 
##     enz_C_div   spore_abund   spore_shape    spore_size     spore_RRx 
##             1             1             1             1             1 
##   stoich_flex     recycling       asegurl     WA_explor      comp_glu 
##             1             1             1             1             1 
##    fungic_str        cu_str   drought_str  stress_tolav stress_tolPC1 
##             1             1             1             1             1 
##   soil_aggreg   leaf_decomp   wood_decomp     decompPC1           CUE 
##             1             1             1             1             1

We will use the base R function prcomp() for PCA. (There are other functions like princomp() or PCA in packages, but prcomp is straightforward and uses singular value decomposition for numerical stability.) Since we already scaled the data, we can tell prcomp not to scale again. However, for demonstration we could also let prcomp do the scaling by setting scale. = TRUE. Here we’ll use the scaled data we prepared:

library(ggplot2)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(ggfortify)
# Perform PCA on the scaled trait data
pca_result <- prcomp(trait_data_scaled, center = FALSE, scale. = FALSE)
# Print PCA summary
summary(pca_result)
## Importance of components:
##                          PC1    PC2    PC3     PC4     PC5     PC6     PC7
## Standard deviation     2.706 2.2613 2.0964 1.81593 1.63245 1.56868 1.42489
## Proportion of Variance 0.183 0.1278 0.1099 0.08244 0.06662 0.06152 0.05076
## Cumulative Proportion  0.183 0.3109 0.4207 0.50318 0.56980 0.63132 0.68208
##                            PC8     PC9   PC10    PC11    PC12    PC13   PC14
## Standard deviation     1.38862 1.28079 1.1593 1.13254 1.06571 0.99139 0.8989
## Proportion of Variance 0.04821 0.04101 0.0336 0.03207 0.02839 0.02457 0.0202
## Cumulative Proportion  0.73029 0.77130 0.8049 0.83696 0.86536 0.88993 0.9101
##                           PC15    PC16    PC17    PC18    PC19    PC20    PC21
## Standard deviation     0.85213 0.81201 0.69095 0.64784 0.54129 0.51352 0.49076
## Proportion of Variance 0.01815 0.01648 0.01194 0.01049 0.00732 0.00659 0.00602
## Cumulative Proportion  0.92828 0.94477 0.95670 0.96719 0.97452 0.98111 0.98713
##                           PC22    PC23    PC24    PC25    PC26    PC27
## Standard deviation     0.39044 0.35417 0.31475 0.28769 0.18069 0.14954
## Proportion of Variance 0.00381 0.00314 0.00248 0.00207 0.00082 0.00056
## Cumulative Proportion  0.99094 0.99408 0.99656 0.99862 0.99944 1.00000
##                             PC28
## Standard deviation     2.579e-15
## Proportion of Variance 0.000e+00
## Cumulative Proportion  1.000e+00
#The summary(pca_result) will show the proportion of variance explained by each principal component (PC). Because we collected fungal traits based on 28 species (traits data has 28 dimensions), we will get 28 PCs. We expect the first one or two to explain most of the variance. For instance, you might see something like “PC1 – X% Proportion of variance, PC2 – Y% variance,...” etc.
#We can also examine the PCA loadings and scores:
#- Loadings (also called rotation in prcomp output) tell us how each trait contributes to each principal component. These are found in pca_result$rotation.
#- Scores (coordinates of each species on the PCs) are in pca_result$x. Each row corresponds to a species, and columns are PC1, PC2, etc. These tell us where each species lies in the new reduced-dimensional space.
# PCA loadings (how traits map to PCs):
pca_result$rotation
##                         PC1          PC2           PC3           PC4
## extension      0.3093250343 -0.034397086  0.1061346626 -0.0981583479
## density       -0.2501731673  0.010758071 -0.1779388670  0.0918878820
## biomassst      0.1827288791  0.229679051 -0.2477558121 -0.0605262464
## biomassopt     0.2060661430  0.267784721 -0.1829408699 -0.0290037756
## biomasscomplC  0.0617979554  0.205776789  0.2299072648 -0.1179843753
## hyphal_diam    0.0573643384  0.131536497 -0.1480271941 -0.1611048828
## melanin       -0.1911917782 -0.003211710 -0.0432702647  0.0443906915
## water_cont    -0.0287075677 -0.220180907  0.0015857089  0.2505528976
## hydrop         0.0770143637  0.051171761 -0.0358596090 -0.0594312935
## DNAc           0.1789877637 -0.131544870 -0.0295199373  0.1788457248
## PLFAc          0.0659997049 -0.130560085  0.0080413941 -0.1365646247
## stoich_C_XPC1 -0.0002417941  0.080830661 -0.1781676723 -0.3149102542
## stoich_N_XPC1  0.0216885555  0.124674976 -0.2281778068 -0.0136677757
## stoich_N_XPC2  0.1127625269 -0.235119951 -0.0217025816 -0.2382364176
## C_cont         0.0530780480 -0.158435655 -0.2538512042 -0.2033194502
## enz_la        -0.1424251924 -0.017244831  0.1762843542 -0.0284190764
## enz_leu        0.1514284754 -0.152907550  0.1133937609  0.0143674665
## enz_cel       -0.2307336998  0.038537567  0.1230207971  0.2208670106
## enz_pho       -0.0154273213 -0.032221656 -0.0509674220  0.0737595509
## complC_use    -0.0597999274  0.193912092  0.3331865151  0.0510548647
## enz_C_div     -0.1126922293  0.141597049  0.0090438639  0.3498320662
## spore_abund    0.2153895338 -0.018401694  0.0008401137  0.0853396738
## spore_shape    0.0309120058  0.235367986 -0.1734426945  0.0192321409
## spore_size     0.1405162726  0.231326571 -0.0739838075 -0.1799213133
## spore_RRx      0.1610770087 -0.099435772 -0.0145227111 -0.1033100477
## stoich_flex    0.1056109838  0.111452979 -0.2646056887  0.2757799255
## recycling      0.0534603102 -0.051704435 -0.2690437073  0.1171834722
## asegurl       -0.1005255109  0.096426358 -0.3158563892  0.1127178629
## WA_explor     -0.1445954382 -0.056417858 -0.1382235699  0.1289803402
## comp_glu       0.3041589622  0.126915735  0.0687282656 -0.0443680021
## fungic_str     0.2477732959 -0.115322871  0.0096310534  0.1157930055
## cu_str         0.1206897470 -0.100974802  0.0842669493  0.1619486422
## drought_str    0.2046104151  0.197282945  0.0957155963  0.2003954356
## stress_tolav   0.2953120634 -0.009798532  0.0977103064  0.2463901384
## stress_tolPC1  0.3004600998  0.016231116  0.0808413874  0.2255162672
## soil_aggreg   -0.1160290002  0.231417026 -0.0468728741  0.0968162909
## leaf_decomp   -0.0450319303  0.374180075  0.0616913981 -0.0005674858
## wood_decomp   -0.0356340132  0.139035923  0.2967338095 -0.1303120291
## decompPC1     -0.0548251074  0.315600156  0.1748350752 -0.0707384456
## CUE            0.0666714187  0.068757155 -0.0552578540  0.2154954645
##                       PC5          PC6          PC7          PC8          PC9
## extension     -0.07170982 -0.153482370 -0.068103826 -0.014676302  0.113361154
## density       -0.02941407  0.320622092 -0.071829552 -0.014789669  0.077565096
## biomassst      0.02697841  0.075444207  0.036825361 -0.110843448  0.198308144
## biomassopt     0.01074806 -0.072478597  0.028303389 -0.039496929  0.111278387
## biomasscomplC  0.06332883 -0.034224417 -0.064003543 -0.189723128  0.136655882
## hyphal_diam   -0.01707490 -0.270016978 -0.210642583  0.186232579 -0.117908974
## melanin        0.01566304  0.273754000 -0.293449007 -0.095263301  0.047257053
## water_cont    -0.24935188 -0.144492130 -0.092364773 -0.021573452 -0.059717362
## hydrop        -0.15076311 -0.037191875  0.533046900 -0.008316421 -0.072250144
## DNAc          -0.11098168  0.012918415 -0.238440748 -0.002414607 -0.334377983
## PLFAc         -0.36921879  0.103786469 -0.158932148  0.214111837 -0.093253816
## stoich_C_XPC1  0.09840526 -0.233274260 -0.047520046 -0.056251996 -0.006426632
## stoich_N_XPC1 -0.07374229 -0.268933354 -0.167785117  0.121439054 -0.256453201
## stoich_N_XPC2  0.01244551 -0.029272890 -0.021861749  0.056609720  0.139979425
## C_cont         0.02717980 -0.026402156  0.014330746 -0.248984165 -0.180675502
## enz_la         0.34597472 -0.280413727  0.008628059  0.218294111 -0.070749968
## enz_leu       -0.18972254  0.061966753  0.131801669  0.361797478  0.242451350
## enz_cel       -0.02960094 -0.252811831 -0.112172854  0.007272740 -0.082730968
## enz_pho       -0.39129545 -0.317331069 -0.105554139 -0.198356476 -0.013938988
## complC_use     0.16233922 -0.085135476 -0.051812826  0.071144714 -0.152314702
## enz_C_div      0.09724754 -0.056413873  0.127448297  0.208911137  0.059811545
## spore_abund   -0.10426899  0.107725734  0.259367940  0.123276069 -0.093456226
## spore_shape   -0.02953314 -0.084792834 -0.009445653  0.272491374 -0.065100610
## spore_size     0.24348748  0.124794583 -0.159330650  0.080878939 -0.057242272
## spore_RRx      0.26086732  0.202278089  0.138642338 -0.089775717 -0.242978081
## stoich_flex   -0.01558236  0.076797329  0.176440300  0.115066782 -0.004770606
## recycling      0.04910634  0.119850190  0.009736300 -0.028348843 -0.319488368
## asegurl       -0.11352702 -0.036358039  0.068986201 -0.058458060  0.137669349
## WA_explor      0.03363256 -0.210423548  0.084275133 -0.317483990  0.315023188
## comp_glu      -0.13431568 -0.108243167 -0.049264195 -0.053114930 -0.003802497
## fungic_str     0.06198627  0.085073372 -0.254140651 -0.183152397  0.139189526
## cu_str         0.13355391 -0.127490629  0.274993192 -0.188386471 -0.346681434
## drought_str   -0.01153760  0.060726612 -0.124325165  0.164529306  0.142005569
## stress_tolav   0.09481888  0.009435044 -0.053320763 -0.106674668 -0.033745946
## stress_tolPC1  0.06421120  0.057166245 -0.163635489 -0.065811684  0.084696381
## soil_aggreg   -0.02414137  0.228578087 -0.168530232 -0.074856543 -0.223662908
## leaf_decomp   -0.17265170  0.027122186  0.082273484 -0.029762672 -0.043728945
## wood_decomp   -0.23855155  0.102432624  0.047547069 -0.283050736 -0.133631280
## decompPC1     -0.25546209  0.091229449  0.085205048 -0.164131406 -0.095185696
## CUE            0.17963056 -0.198667956  0.005531144 -0.243578886  0.084772700
##                       PC10         PC11          PC12         PC13         PC14
## extension     -0.075489026  0.107773869 -0.0864313205  0.083569536 -0.030210402
## density        0.019179334 -0.109208954  0.0892607461  0.032796547 -0.134786344
## biomassst     -0.010722862  0.140834296  0.1565707115  0.029567700  0.125268861
## biomassopt    -0.132399972  0.108342767  0.0636847870 -0.001310938 -0.055655938
## biomasscomplC -0.066139420 -0.299536064  0.0691939798 -0.248953839 -0.193137395
## hyphal_diam    0.099368331  0.005483981 -0.2027405434  0.101390108  0.386235050
## melanin       -0.058229285  0.141150635  0.0498639320  0.012375028 -0.307891131
## water_cont    -0.127721899 -0.324841412 -0.0785998906 -0.104957919 -0.014407856
## hydrop         0.194673300  0.232772949  0.2207541905  0.054780605 -0.149839083
## DNAc           0.207759987  0.157204509  0.1737546536  0.062742428 -0.041737635
## PLFAc         -0.027905530  0.297000007  0.2067725976 -0.130266764  0.011199815
## stoich_C_XPC1  0.042079306 -0.205802293  0.1806496798  0.196146277 -0.203184479
## stoich_N_XPC1  0.230254800 -0.129588282  0.1371671614  0.036617474 -0.320745135
## stoich_N_XPC2  0.249047881  0.049244910 -0.1985522925  0.015865162 -0.403347012
## C_cont         0.188784388 -0.214109317  0.2318695798 -0.166056786  0.068571664
## enz_la        -0.047222408  0.064407561 -0.0003614703 -0.073438979  0.045174458
## enz_leu       -0.014084078 -0.039337079 -0.1493220327 -0.117157599 -0.204208356
## enz_cel        0.175737727  0.151464611  0.0988999795 -0.102381914 -0.083352575
## enz_pho       -0.105102113  0.127200484  0.0135563123 -0.253854814  0.021275677
## complC_use    -0.065029817 -0.020198357  0.2323718564  0.154684026 -0.165888823
## enz_C_div      0.229661805 -0.037466950  0.2445813824 -0.035031462  0.074153509
## spore_abund    0.050972155 -0.266635215  0.0942046729  0.451494056  0.042728543
## spore_shape   -0.364550459 -0.179596809 -0.0679956684 -0.138146506 -0.295017183
## spore_size     0.126986285  0.187530582 -0.0727858279 -0.154104145  0.112049607
## spore_RRx      0.085153376  0.039061633 -0.0428645619 -0.433894999 -0.161673384
## stoich_flex   -0.050717219 -0.069574859  0.1583587019 -0.330217256  0.032448418
## recycling     -0.398699124  0.035977128 -0.1009743229  0.257312483 -0.108484129
## asegurl        0.135434252  0.154585123 -0.2786986717  0.002104860  0.016621356
## WA_explor      0.231673482 -0.068971569 -0.1154045026  0.113124026 -0.130901028
## comp_glu      -0.033447129 -0.078006143 -0.1507640700  0.024810299 -0.076074349
## fungic_str    -0.032313192 -0.194288862  0.2256210741 -0.006022119  0.165442851
## cu_str         0.006853603  0.024461218 -0.3117756739 -0.077900684 -0.027041864
## drought_str    0.227415611  0.099467666 -0.0622714972  0.059123912 -0.161533768
## stress_tolav   0.104070514 -0.036257394 -0.0764858612 -0.012779185 -0.011920616
## stress_tolPC1  0.111164494 -0.058766464  0.0305634109  0.011253179  0.004728043
## soil_aggreg    0.213107688 -0.041194349 -0.3712764857  0.100629299 -0.074740843
## leaf_decomp    0.045595059 -0.163453262 -0.1101774301 -0.138554406  0.082278658
## wood_decomp   -0.035738646  0.116582617  0.0742844654  0.149711455 -0.069664722
## decompPC1      0.030921504 -0.052986367 -0.0192603309 -0.075245883  0.053097533
## CUE           -0.295717332  0.360278219  0.0690717281  0.077381016 -0.150503348
##                       PC15         PC16        PC17         PC18        PC19
## extension     -0.148394577 -0.016255420  0.12298377 -0.131493890 -0.09672951
## density        0.047567665 -0.135364124  0.03881290  0.194039652  0.16862561
## biomassst      0.053971448  0.018016678  0.10167770 -0.101961018 -0.09280847
## biomassopt     0.189074191 -0.047047187  0.20165881 -0.023907578 -0.16431398
## biomasscomplC -0.188412769  0.187640127  0.23651488  0.266441341  0.16296527
## hyphal_diam   -0.280659542  0.069257084 -0.01480565  0.234604257  0.24404653
## melanin       -0.505747279 -0.132804468  0.02851474 -0.116609876 -0.01367264
## water_cont     0.174893441 -0.155192205  0.33441859 -0.168305871  0.08780658
## hydrop        -0.200796969 -0.130784698  0.14766039  0.176309145  0.01105276
## DNAc           0.238077210  0.199100642  0.18636732  0.076271874  0.17491563
## PLFAc          0.106338662 -0.259747449 -0.14727325 -0.115024790 -0.12630809
## stoich_C_XPC1  0.129686498 -0.439371430 -0.13648984  0.024231776  0.15013110
## stoich_N_XPC1 -0.196841232  0.154447709  0.04887410 -0.186152768  0.06078566
## stoich_N_XPC2  0.212854456  0.192534205  0.24453755  0.025156251 -0.27802713
## C_cont         0.007303824  0.122291121 -0.02011625 -0.254385170  0.05791167
## enz_la         0.125806254 -0.036680730  0.10038004  0.026267480 -0.15234093
## enz_leu       -0.178660960  0.097504828  0.14176920 -0.045700646  0.24269855
## enz_cel        0.049550959 -0.124321840 -0.02601026  0.216557480 -0.07286576
## enz_pho       -0.147605534  0.133596819 -0.13422445  0.326788320 -0.27516795
## complC_use    -0.028765999 -0.066553783  0.08914479  0.006271704  0.03706787
## enz_C_div     -0.161896995  0.065309781  0.14925558 -0.226992189 -0.23004557
## spore_abund    0.013156755  0.183711489 -0.08279533  0.288299912 -0.08930645
## spore_shape    0.203549597 -0.067573082 -0.10625913  0.203345654 -0.15110284
## spore_size    -0.085005307 -0.076743002  0.27335224  0.110126687 -0.13390084
## spore_RRx      0.051167233  0.039021559 -0.23102973  0.129772224  0.03931172
## stoich_flex   -0.059647090  0.051234854 -0.10814798  0.087103663  0.10055097
## recycling     -0.202668721  0.013966933  0.11899508 -0.029656209 -0.25007846
## asegurl        0.105163370 -0.242401290  0.30224007  0.130802848  0.28931681
## WA_explor     -0.058210979  0.005300561 -0.20804748  0.022497605 -0.24189551
## comp_glu      -0.168759084 -0.064334351 -0.25710000 -0.189878515  0.06862852
## fungic_str    -0.009970353 -0.098934845  0.16891141  0.169860716 -0.15118785
## cu_str        -0.126077383 -0.302287369  0.08340300 -0.074176054  0.01539349
## drought_str    0.100976266 -0.059768310 -0.30651153 -0.041353550  0.09960366
## stress_tolav  -0.018072776 -0.237554298 -0.02792847  0.027997515 -0.01864954
## stress_tolPC1  0.021088597 -0.168478043 -0.04927177  0.064713664 -0.03394141
## soil_aggreg    0.114769543  0.190658994 -0.05774585  0.047223827 -0.12775217
## leaf_decomp    0.116751034 -0.001293392  0.03263755 -0.321390247 -0.07460280
## wood_decomp    0.091471248 -0.036276109  0.08035387  0.118988793  0.09314379
## decompPC1      0.045833970  0.033068364  0.07089752 -0.101042019 -0.07132191
## CUE            0.157840055  0.333713787 -0.06049362 -0.144532323  0.34866650
##                        PC20         PC21          PC22         PC23
## extension      0.3129165684  0.061176907 -0.2047325612  0.058030305
## density        0.0108925477  0.190264529  0.1298314123  0.141760791
## biomassst     -0.1057894637 -0.027720663 -0.0602914203 -0.303186982
## biomassopt     0.0464874488  0.136359234  0.0567485426  0.048231732
## biomasscomplC -0.0142114292 -0.307632845 -0.0007120972  0.008247317
## hyphal_diam   -0.3191513917  0.300369302  0.0193472382  0.197084643
## melanin       -0.1996344416  0.087927605 -0.1328298216 -0.083402351
## water_cont    -0.1104035783  0.080421901 -0.0258179588 -0.156498740
## hydrop         0.1073510833  0.031152194  0.0527091274  0.143603637
## DNAc          -0.1697677470  0.078758424 -0.0566959495  0.076706544
## PLFAc         -0.2137272204 -0.104798331  0.0735768746 -0.132304375
## stoich_C_XPC1 -0.0518396846  0.072401297 -0.1677756271 -0.109069958
## stoich_N_XPC1  0.1717249285 -0.191546424  0.3331666132 -0.025524673
## stoich_N_XPC2 -0.2092584952  0.049025939  0.1278778532  0.178651786
## C_cont         0.0453111769  0.011477397 -0.2975312114 -0.100615622
## enz_la        -0.2753848014 -0.293538718 -0.1934971020 -0.200721455
## enz_leu       -0.0785679645  0.221192830 -0.2056343149 -0.283180950
## enz_cel        0.3052861867  0.148095561 -0.2535391337 -0.102106785
## enz_pho       -0.0339194156 -0.089898299 -0.0728873023 -0.006708475
## complC_use    -0.2334810442  0.005894692 -0.0015496817  0.014947645
## enz_C_div     -0.0005735552  0.227890801 -0.2391026672  0.138931110
## spore_abund   -0.1096853214 -0.041242883 -0.1712991444 -0.240633177
## spore_shape    0.0723977613  0.277093290 -0.0226735171  0.049017104
## spore_size     0.0297377994  0.109761275  0.1052382420 -0.302435737
## spore_RRx     -0.1214462911  0.132399126 -0.3109235790  0.180954648
## stoich_flex   -0.1592561564 -0.128453728  0.2146233623 -0.214557525
## recycling     -0.1161854869 -0.231139008 -0.2061001106  0.195231927
## asegurl       -0.0221707101 -0.342971042 -0.3168328643  0.098969717
## WA_explor     -0.3581064949  0.130996489  0.0802979982 -0.116919162
## comp_glu       0.0268443001  0.022911901 -0.1545774511 -0.046839486
## fungic_str     0.0342122101  0.112508536 -0.0093906427  0.086125405
## cu_str        -0.0332907548  0.114722606  0.2643619311 -0.148907217
## drought_str   -0.0536800852 -0.264460382 -0.0629304591  0.126514773
## stress_tolav  -0.0271871947 -0.019184702  0.0989610885  0.032842407
## stress_tolPC1 -0.0172377395 -0.053002143  0.0217210303  0.090202570
## soil_aggreg    0.2131945115  0.037021477 -0.0934938220 -0.330012299
## leaf_decomp   -0.1440321768  0.037187508 -0.0406511369  0.295571488
## wood_decomp   -0.0568273824  0.116245866 -0.0004808500 -0.118024607
## decompPC1     -0.2432919592  0.041600120 -0.0474175123  0.011225302
## CUE           -0.0596522791  0.178063717  0.0068595551 -0.058205603
##                       PC24        PC25         PC26         PC27         PC28
## extension      0.149330999  0.03917975  0.171729400  0.178522458  0.069303020
## density        0.054409366  0.22849834 -0.112795175  0.078346803  0.116525258
## biomassst      0.043895140  0.06208521  0.026801734 -0.083494615 -0.006499856
## biomassopt     0.508680086 -0.12242990  0.167934535  0.050669196  0.039450739
## biomasscomplC -0.124832174 -0.02462702 -0.007996599 -0.093591775 -0.059059937
## hyphal_diam    0.023662237 -0.05768855  0.046264879 -0.023117910  0.195661278
## melanin        0.240951289  0.04277244 -0.214318601  0.007216021  0.071387466
## water_cont     0.059224224 -0.31699710 -0.101841345 -0.045699696  0.393812492
## hydrop        -0.257064219 -0.08310595 -0.184791695  0.199802562  0.285409691
## DNAc          -0.056046011 -0.05660314 -0.015419599  0.070051189 -0.424007670
## PLFAc         -0.144991279  0.16327438  0.102403642  0.110564935  0.106205941
## stoich_C_XPC1 -0.025165458  0.06392533 -0.109297421 -0.196065668 -0.239326960
## stoich_N_XPC1  0.103096515  0.26288643  0.250310106 -0.024964551  0.074521655
## stoich_N_XPC2  0.017786330 -0.05824846 -0.275626790 -0.025359932 -0.032321175
## C_cont        -0.120990682 -0.08312289 -0.027400014 -0.027857503  0.258292681
## enz_la        -0.024270009  0.27799261 -0.057183050  0.130410842  0.142021397
## enz_leu       -0.056260300  0.16012437  0.209209772 -0.088648489 -0.131843477
## enz_cel        0.186615437 -0.15793892 -0.146247855 -0.066720610 -0.136724093
## enz_pho        0.066776772  0.15262880 -0.125495106 -0.117292708  0.045567306
## complC_use     0.077772389 -0.20091453  0.137007205  0.408275239  0.093683786
## enz_C_div     -0.186622856  0.06974803  0.081774069 -0.204402475 -0.087314006
## spore_abund    0.285756875  0.17816462 -0.200705115 -0.043366323  0.105454368
## spore_shape   -0.293830926  0.04814884  0.064234382  0.000330318  0.063571457
## spore_size    -0.185811875 -0.17331386 -0.178839850 -0.147427423  0.032980106
## spore_RRx      0.226964974  0.04157483  0.235705758  0.041705582  0.116576421
## stoich_flex    0.095544406 -0.24844582 -0.032270045  0.170314084 -0.195861035
## recycling     -0.175020395 -0.17241687  0.095379983 -0.177882885 -0.099788647
## asegurl       -0.016733593  0.11999388  0.122864231  0.167661759 -0.051150497
## WA_explor     -0.141765106 -0.13670469  0.320291093  0.143202591 -0.043270232
## comp_glu      -0.140401123 -0.15308327 -0.295392990  0.361066189 -0.224214097
## fungic_str    -0.160455286  0.25451167  0.050093108  0.233879423 -0.021444084
## cu_str         0.084643263  0.20930055 -0.035700771 -0.118956735 -0.126589761
## drought_str   -0.002464051 -0.21376316 -0.016925375 -0.322877257  0.251067774
## stress_tolav  -0.040336660  0.12885347 -0.001305306 -0.107161642  0.092426997
## stress_tolPC1 -0.081315760  0.08733106  0.013066972 -0.066611878  0.114922652
## soil_aggreg   -0.144323267  0.06406402  0.053107727  0.257901752  0.146397693
## leaf_decomp    0.040114061  0.20502932 -0.200034228  0.072255002 -0.043819502
## wood_decomp   -0.120933563 -0.08667035  0.353374338 -0.227489823  0.039505226
## decompPC1      0.066305170  0.11595341 -0.052790251 -0.114885898 -0.086429659
## CUE           -0.139543968  0.21235194 -0.206796512 -0.035241475  0.183459160
# PCA scores (species coordinates on PCs):
pca_scores <- pca_result$x
head(pca_scores, 10)   # print scores for all 28 species (or use head if more cases)
##           PC1        PC2        PC3        PC4         PC5        PC6
## 1  -3.0229119  0.7832240 -1.0704963  1.0993658 -1.83401439  0.1407392
## 2  -1.8898007  0.6973651 -1.3005611 -0.4237364 -1.09980147  0.1864548
## 3   3.0344693  2.1311768  3.2795914 -1.9217677  0.06668774  0.1404638
## 4  -2.8431077  0.5078057  0.4699731  1.2754967  0.93076944  1.7193397
## 5   3.0605572  1.7857013 -1.7076977 -0.4918964  1.88562487 -0.4419365
## 6   0.3050325  2.8881513 -1.9531963  0.4557535  1.15938317  0.7937637
## 7  -2.3207886  1.9586536  0.9701876  1.2760880  0.14293571  1.3602899
## 8  -3.4117893  0.6581091  2.5671509 -0.8165280 -1.21532235  1.2803043
## 9  -0.8825861  0.1522947  0.6992069 -2.8186312 -2.48557639  2.6453037
## 10  1.8649040 -2.3691809 -1.3296108 -0.9072384  0.56449197  0.1462576
##            PC7        PC8        PC9       PC10       PC11       PC12
## 1  -1.14903260  0.2163463  0.8399626 -2.6052885  0.6060996  0.7751050
## 2   2.62983822  0.1691776 -1.6757698  0.9238685 -1.4874226 -0.8160976
## 3   0.25927281 -2.0206993  0.4216301 -0.8701783  1.0139641  1.2025125
## 4  -0.49719226  0.2300597  1.0042976  0.5210144  0.2112051 -2.0045261
## 5   0.75729699 -1.4398451  0.5055648  0.4544844  0.7536120  0.9046143
## 6   2.05251983  0.8451850  1.0817046 -0.8384801 -0.1843635  0.4869635
## 7  -1.08841243 -2.6559832 -0.4378768  0.4983780  0.4981169 -1.0659714
## 8   0.86927708 -0.9917911 -0.4661090  0.8470619  0.3376407 -1.6983069
## 9   0.08490064  3.1018738 -0.1325203  0.8658623 -0.3485545  1.7301808
## 10  0.15097672 -1.0441830 -0.3707654 -2.2061480  1.8101128 -1.0388972
##           PC13       PC14       PC15       PC16       PC17        PC18
## 1  -2.44094755 -1.4316919  0.1630345  0.7718117 -0.2725293 -0.22451611
## 2   0.29793723  0.1278812 -0.9555837 -0.7662876  0.1917387 -0.33783511
## 3   2.03213922 -0.5185504  0.5883320 -0.1167542  0.2060058 -0.09891873
## 4  -0.69977093  0.3322925  0.1438754 -0.4540071 -0.4293299 -0.65472030
## 5  -1.49506595 -0.7764491 -0.5192922 -2.2392163 -1.1530103 -0.24898536
## 6   0.18521256 -0.1236898 -0.2215801  0.9016237  0.6775013  0.15313146
## 7  -0.72027160 -0.8434581 -0.2497464 -0.2352693  1.9432471 -0.13063592
## 8   0.03913913 -0.4559673  1.5024020  0.2955213 -1.4851752  1.35316029
## 9  -1.15679343  1.4115578  0.3670263 -0.6202966  0.4503323  0.54143248
## 10 -0.44804094  1.9519039 -1.0981328  0.8836161  0.1433979  1.29840729
##            PC19         PC20         PC21        PC22        PC23        PC24
## 1  -0.137830447 -0.488116139  0.141225089  0.13542191  0.08250209  0.66004763
## 2   0.007633300 -0.006430238 -1.095910473  0.24020001 -0.01561278  0.73529917
## 3   0.418266016 -0.275923641 -0.030430163 -0.22078052  0.59526437  0.41926011
## 4  -0.375134823  0.326510762 -0.127963835  0.79368652  0.92543556 -0.35473455
## 5  -0.314710102 -0.182630114 -0.006846175 -0.50851309  0.18622501 -0.28386849
## 6  -0.766939085  0.706698409 -0.211119096 -0.10034011 -0.13287804  0.04703948
## 7   1.035041739 -0.415662784 -0.130623999 -0.02274266 -0.39036715 -0.37464226
## 8   0.281199204  0.553637069 -0.001648870 -0.42453674 -0.08923158  0.15624344
## 9   0.318954804 -0.762395106 -0.029347893 -0.20780739  0.18731792 -0.25619231
## 10 -0.008587554  0.098068555 -0.118864190 -0.05890683  0.22619827 -0.16375244
##           PC25        PC26        PC27          PC28
## 1  -0.01293496 -0.10293118 -0.02801499 -2.648323e-15
## 2   0.15252354 -0.13460762  0.22166960 -3.017169e-15
## 3   0.56840747  0.13227009 -0.02287826 -2.936076e-15
## 4   0.39900101  0.09290918 -0.10765631 -3.421403e-15
## 5  -0.26851976 -0.02681633  0.15534723 -2.994198e-15
## 6  -0.28043537  0.60298588  0.01481264 -3.222360e-15
## 7  -0.13327892  0.09010276  0.08663081 -2.746349e-15
## 8  -0.38042453  0.02587866 -0.04373034 -2.503336e-15
## 9   0.04957301  0.11282164 -0.02604501 -2.242911e-15
## 10  0.11249253 -0.12516898  0.24308826 -2.428262e-15
# Next, let’s visualize the PCA results.

# Basic PCA biplot (PC1 vs PC2):
biplot(pca_result, scale = 0)

# Improved biplot with custom labels:
biplot(pca_result, scale = 0, xlabs = traits_data$Phylum)

# Using autoplot to visualise:
p <- autoplot(pca_result, data = traits_data, colour = "Species",
              loadings = TRUE, loadings.colour = 'blue', 
              loadings.label = TRUE, loadings.label.size = 3)
p

# Compute Euclidean distance on scaled data
dist_matrix <- dist(trait_data_scaled, method = "euclidean")

# Perform hierarchical clustering using Ward's method
hc <- hclust(dist_matrix, method = "ward.D2")  # ward.D2 is Ward's in hclust

# Plot the dendrogram
plot(hc, labels = traits_data$strainID, main="Hierarchical Clustering of Fungi (Ward's method)")

# Cut tree into 3 clusters
cluster_membership <- cutree(hc, k = 3)
cluster_membership
##  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 22 23 24 25 26 27 
##  1  1  2  1  2  1  1  1  1  3  3  3  2  1  2  2  2  1  3  1  2  1  1  1  1  2 
## 28 30 
##  1  1
# See which species are in which cluster
data.frame(Species = traits_data$newID, Cluster = cluster_membership)
##    Species Cluster
## 1   RLCS21       1
## 2   RLCS23       1
## 3   RLCS06       2
## 4   RLCS26       1
## 5   RLCS07       2
## 6   RLCS17       1
## 7   RLCS22       1
## 8   RLCS28       1
## 9   RLCS27       1
## 10  RLCS15       3
## 11  RLCS19       3
## 12  RLCS04       3
## 13  RLCS05       2
## 14  RLCS14       1
## 15  RLCS10       2
## 16  RLCS12       2
## 17  RLCS18       2
## 18  RLCS29       1
## 19  RLCS11       3
## 20  RLCS09       1
## 22  RLCS13       2
## 23  RLCS31       1
## 24  RLCS30       1
## 25  RLCS25       1
## 26  RLCS16       1
## 27  RLCS01       2
## 28  RLCS20       1
## 30  RLCS24       1