########################################################################################################### # graphical illustration of trophic diversity: abundance distribution along classes of trophic levels # # # # inputs : # # - ab: a vector (length S) with the abundances of S species in the community of interest # # - tl: a vector (length S) with the trophic levels of the S species of interest # # -> NA are not allowed in 'tl' and are ignored in 'ab' # # -> names of species in tl and ab must be the same # # and as short as possible (<3 characters), else shortened # # - classes: vector((length N+1) for the limits of the N classes # # by default 5 classes of width 0.5 between 2 and 4.5 # # - dom: criterion of dominance determining species plotted individually (by default: 5%) # # # # output: a plot with distribution of abundances along the N trophic level classes # # abundances and names of dominant species are figured inside classes # # number of species in each class is plotted above the corresponding bar # ########################################################################################################### graphtrophdiv<-function(ab, tl, classes=seq(2,4.5,0.5), dom=5) { # number of species and species names S<-length(ab) if (length(which(names(tl)!=names(ab)))>0) stop("error : species names are different in 'ab' and 'tl'") nmsp<-substr(names(tl),1,3) # check if vector ab and tl are of same length if (length(tl)!=S) stop("error : number of species different in 'ab' and 'tl'") if (length(which(is.na(tl)==T))>0) stop("error : NA are not allowed in 'tl'") # relative abundances relab<-100*ab/sum(ab,na.rm=T) # number of classes N<-length(classes)-1 # names of classes nmclasses<-paste(classes[-(N+1)],classes[-1],sep="-") # relative abundances and number of species in each class relabclasses<-rep(0,N) ; names(relabclasses)<-nmclasses nbspclasses<-rep(0,N) ; names(nbspclasses)<-nmclasses for (i in 1:N) { whichi<-which(tl>=classes[i] & tl=0 & laby<=100)] # plot plot(mean(labx),mean(laby),type="n",bty="n",xaxt="n",yaxt="n",xlab="",ylab="",xlim=limx,ylim=limy ) rect(limx[1],limy[1],limx[2],limy[2]) axis(side=1,labx, tcl=-0.3, pos=0, labels=F) mtext(nmclasses,side=1,las=1,at=labx,line=-0.2,cex=0.9) ; mtext("Trophic level",side=1,line=0.9,cex=1.1,adj=0.5,font=2) axis(side=2, laby, pos=limx[1], tcl=-0.3,labels=F) mtext(laby,side=2,las=1,at=laby,line=-0.3,cex=0.9) ; mtext("% of abundances",side=2,line=1.8,cex=1.1,adj=0.5,font=2) # bar for each class for (i in 1:N) { # bar rect(classes[i],0,classes[i+1],relabclasses[i],col="grey70") # number of species if (nbspclasses[i]>0) text(labx[i],relabclasses[i]+(limy[2]-limy[1])/20,nbspclasses[i],adj=0.5,cex=1.1,font=2) # dominant species: filtering, sorting, plotting domi<-which(tl>=classes[i] & tldom ) ; relabdom<-relab[domi] ; nmspdom<-nmsp[domi] o<-order(relabdom, decreasing=T) ; relabdomo<-relabdom[o] ; nmspdomo<-nmspdom[o] if (length(nmspdomo)>0) { cumrelab<-0 for ( j in 1:length(nmspdomo) ) { rect(classes[i],cumrelab,classes[i+1],cumrelab+relabdomo[j],col="grey99") text(labx[i],cumrelab+0.5*relabdomo[j],nmspdomo[j],adj=0.5,cex=1.1,font=1) cumrelab<-cumrelab+relabdomo[j] }# end of j }# end of if }# end of i } # end of function graphclass ########################################################################################## # example showexample<-F if (showexample==T) { # basic example abex<-sample(c(round(runif(18,0,100)),0,NA)) ; names(abex)<-paste("s",1:20,sep="") tlex<-round(runif(20,2,4.5),1) ; names(tlex)<-paste("s",1:20,sep="") graphtrophdiv(abex,tlex,dom=5) # 4 random communities abex<-matrix(sample(c(round(runif(32,0,100)),rep(0,8))),4,10,dimnames=list(paste("com",1:4,sep=""),paste("sp",1:10,sep=""))) tlex<-round(runif(10,2,4.5),1) ; names(tlex)<-paste("sp",1:10,sep="") layout(matrix(1:4,2,2)) ; layout.show(4) for (k in 1:4) graphtrophdiv(abex[k,],tlex,dom=5) } # end of if showexample