comm = function (size,type, param,pool_size,ifplot) { # This function generates a community of the size 'size' from an abundance distribution # and returns abundances and number of species #ifplot: 1 linear, 2 logarithmic d = distr(type, param,pool_size); dx = cumsum(d); n = rep(0,pool_size) hits = sort(runif(size)) indsp = 1 indind = 1 while (1) { if (indsp > pool_size | indind > size) break if (hits[indind] < dx[indsp]) { n[indsp] = n[indsp] + 1 indind = indind + 1 } else { indsp = indsp+1 } } if (ifplot) { graphics.off() a<-system("wmic desktopmonitor get screenwidth",intern=T) scrw = as.numeric(a[2]) } nospec=sum(n>0); #const=sum((log(n1)/nospec)); #evenness = 1 - (2 / pi) * atan(sum((log(n1)-const)^2) / nospec); n1=n n1[n==0]=NA upperl = max(which(n>0)) if (ifplot) { dev.new(2,xpos=10,ypos=10,width=5,height=5) if (ifplot == 1) matplot(cbind(size*d[1:upperl],n1[1:upperl]),type=c("l","b"),pch=20,lwd=2,lty=1,col=c("blue","red"),xlab="Species",ylab="Abundance") if (ifplot == 2) { matplot(cbind(size*d[1:upperl],n1[1:upperl]),type=c("l","b"),pch=20,log="y",lwd=2,lty=1,col=c("blue","red"),xlab="Species",ylab="Abundance") } legend("topright",lwd=2,lty=1,col=c("blue","red"),legend=c("Theoretical","Sampled community")) dev.new(3,xpos=10+scrw/2,ypos=10,width=5,height=5) if (ifplot == 1) { hist(n1,main = paste("Number of species =",nospec),col="grey80", xlab = "Species abundance class",ylab = "Frequency") } if (ifplot == 2) { hist(log(n1),main = paste("Number of species =",nospec),col="grey80", xlab = "Log species abundance class",ylab = "Frequency") } } n }