set.seed(1)
# generate random community ecology data
# using a Poisson distribution
data<-matrix(rpois(5*7,1),5,7)
# get relative abundance, distance matrices
propAbundMat<-t(apply(data,1,function(x) x/sum(x)))
rownames(propAbundMat)<-paste0("site ", 1:nrow(propAbundMat))
colnames(propAbundMat)<-paste0("taxon ", 1:ncol(propAbundMat))
# for simplicity, let's calculate
# the pairwise square chord distance
# between sites and taxa
squareChordDist<-function(mat){
res<-apply(mat,1,function(x)
apply(mat,1,function(y)
sum((sqrt(x)-sqrt(y))^2)
)
)
#
res<-as.dist(res)
return(res)
}
# its not a very popular distance metric
# but it will do
# quite popular in palynology
siteDist<-squareChordDist(propAbundMat)
taxaDist<-squareChordDist(t(propAbundMat))
dev.new(width=10)
twoWayEcologyCluster(
xDist = siteDist,
yDist = taxaDist,
propAbund = propAbundMat
)
if (FALSE) {
# now let's try an example with the example kanto dataset
# and use bray-curtis distance from vegan
library(vegan)
data(kanto)
# get distance matrices for sites and taxa
# based on bray-curtis dist
# standardized to total abundance
# standardize site matrix to relative abundance
siteStandKanto <- decostand(kanto, method = "total")
# calculate site distance matrix (Bray-Curtis)
siteDistKanto <- vegdist(siteStandKanto, "bray")
# calculate taxa distance matrix (Bray-Curtis)
# from transposed standardized site matrix
taxaDistKanto <- vegdist(t(siteStandKanto), "bray")
dev.new(width=10)
twoWayEcologyCluster(
xDist = siteDistKanto,
yDist = taxaDistKanto,
propAbund = siteStandKanto,
cex.axisLabels = 0.8
)
}
Run the code above in your browser using DataLab