complots <- function(ident, scale, ncols, save=F, subsamp=-1, complete=T) { ### Define colors cols <-(colorRamp(c(rgb(255/255, 249/255,121/255),rgb(106/255,0,0) ) )( (0:100)/100 ) ) cols <- cols/255 colgrad<-NA for (i in 1:101) colgrad[i] <- rgb(cols [i,1] , cols[i,2], cols[i,3]) #define the layout for the plot par(mfrow=c(2,3)) nt <- length(spec) index <- which(substr(colnames(specdat),1,nchar(ident))==ident) dats<- matrix(NA, ncol=nt, nrow=nrow(specdat)) sums<-0 cols <- colorRampPalette(c("yellow", "red"))(ncols+1) for (i in 1:nt) { mm <- max(specdat[,index[i]],na.rm=T) if (scale) dats[,i] <- specdat[,index[i]]/mm else dats[,i] <- specdat[,index[i]] if (subsamp!=-1) { sindex <- dats[,i] < subsamp dats[sindex,i] <- NA } fn <- paste(ident,names(spec)[i],sep="-") ddat <- cbind(specdat[,2], specdat[,3],specdat[,index[i] ]) ddat <- ddat[!is.na(rowSums(ddat)),] r <-rasterFromXYZ(ddat ) if (save) { png(paste(fn,".png",sep="")) plot(r, col=colgrad, main=fn) plot(map, add=T) dev.off() } plot(r, col=colgrad, ext=extent(r), main=fn) plot(map, add=T) } sums <- rowMeans(dats , na.rm=T) if (complete==F) sums <- rowMeans(dats) #use only an entry if all species have data #standardise by the range ranges<- NA for (ii in 1:length(sums)) { if (sum(!is.na(dats[ii,]))) { ranges[ii] <- diff(range(dats[ii,], na.rm=T)) } #else ranges[ii]<-NA } sum_range <- sums/ranges sum_range <- (sum_range - min(sum_range, na.rm=T))/(max(sum_range, na.rm=T)-min(sum_range,na.rm=T)) #plot(specdat[,2], specdat[,3], col = colgrad[sum_range*ncols+1], pch=15, main="av. sum/range") ddat <- cbind(specdat[,2], specdat[,3],sum_range*ncols/100) ddat <- ddat[!is.na(rowSums(ddat)),] r <-rasterFromXYZ(ddat ) if (save) { png(paste(ident[i],"_all.png",sep="")) plot(r, col=colgrad, main=paste(ident," of all species")) plot(map, add=T) dev.off() } plot(r, col=colgrad, main=paste(ident," of all species")) plot(map, add=T) }