I'm trying to calculate and visualize Bray-Curtis dissimilarity across a set of sites and between life stages. So essentially, I'm trying to create a matrix that can visualize three things:
I can run the calculation across all of the communities and just hand-pick the comparisons that I actually need (for instance comparison of Adults at site A and Larvae at Site C, doesn't mean much to me, but Adults at site A compared to Larvae at site A is informative). This is effectively what I'm doing through Excel at the moment with Adult comparisons below the diagonal, Larval comparisons above diagonal, and then Adult x Larvae from the same site along the diagonal.
For an example dataset:
library(vegan)
Site = c("A", "B", "C", "D", "E", "A", "B", "D", "E")
LifeStage = c(Ad, Ad, Ad, Ad, Ad, L, L, L, L)
Sp1 = c(56, 42, 67, 23, 44, 21, 15, 20, 12)
Sp2 = c(15, 10, 17, 1, 5, 2, 3, 1,6)
Sp3 = c(10, 6, 7, 10, 5, 4, 0, 1, 0)
Sp4 = c(9, 6, 4, 8, 13, 5, 2, 1, 0)
df = data.frame(Site, LifeStage, Sp1, Sp2, Sp3, Sp4)
mat <- df[,3:6]
dist.mat <- vegdist (mat, dist = "bray") #All comparisons, but many unnecessary
As a note: I am aware that not each site has both adult and larvae data
The best solution I've found was from this thread. I could see this option working, just sounds like it still wouldn't help me skip the need for the manual step mentioned above.
Thanks for the help!
This lets you compute just the values you want, but it does not assemble them into a matrix because it is not clear how you want to represent the missing larval stage:
(Adult <- vegdist(df[which(df$LifeStage=="Ad"), 3:6], dist="bray"))
# A_Ad B_Ad C_Ad D_Ad
# B_Ad 0.1688312
# C_Ad 0.1135135 0.2201258
# D_Ad 0.3636364 0.3207547 0.4890511
# E_Ad 0.1974522 0.1145038 0.2839506 0.3211009
(Larval <- vegdist(df[which(df$LifeStage=="L"), 3:6], dist="bray"))
# A_L B_L D_L
# B_L 0.2692308
# D_L 0.1636364 0.2093023
# E_L 0.4400000 0.2105263 0.3658537
(unlist(sapply(unique(df$Site), function(x) vegdist(df[which(df$Site==x), 3:6], dist="bray"))))
# A B D E
# 0.4754098 0.5238095 0.2923077 0.6000000