R/NicheViews.R

Defines functions NicheViews

Documented in NicheViews

#' NicheViews - 
#' 
#' Function to generate bivariate graphs of all combinations of PCA components, 
#' overlaid with occurrence data. Graphs are stored in jpg files. This 
#' function also generates bar plots of each component, occurrence of that components.
#' 
#' Nininteractive version. 
#' 
#' Time required to run the function depends upon the size of component files. 
#' 
#' @import raster
#' @importFrom grDevices dev.copy dev.off hsv jpeg dev.new
#' @param OutputSufix - Suffix for the output graph files. (file names will be 
#' "sample12.jpg" where sample is suffix and component 1 and 2 are plotted in this 
#' plot)
#' @param RasterFileList - Component files in .asc format (Use the components 
#' generated by PCA function, you can use any .asc files if you need)
#' @param DataFileName - Species occurrences file - .csv file with "Species, 
#' Longitude, Latitude" fields with headers. 
#' @examples \dontrun{
#' NicheViews()
#' }
#' @export
NicheViews <- function(OutputSufix=NA,RasterFileList=NA,DataFileName=NA)
{
  if(is.na(OutputSufix)){
    stop("Please specify OutputSufix (Suffix for output graph files)")
  }
  if(is.na(RasterFileList)){
    stop("Please specify RasterFileList (Select ASCII files to crop)")
  }
  st1 = MakeStack(RasterFileList)
  cat("Stacks created\n")
  if(is.na(DataFileName)){ 
    stop("Please specify DataFileName (occurrence points)")
  }
  bpt1 = rasterToPoints(st1)
  cat("Points generated\n")
  bpt1 = as.data.frame(bpt1)
  compname = names(bpt1)
  oMat = read.table(DataFileName, header=T, sep = ",")
  xypt = oMat[,2:3]
  cat("Now extracting components values for occurrences.....")
  tm = extract(st1,xypt)
  cat("Done.\n")
  TotBins = as.numeric(readline("No. of bins (Depends upon the no of pixels in .asc files) : "))
  ## Extract the component names from stack 
  comps = dim(st1)[3]
  for (i in 1:comps){
    t2=st1[[i]]@file@name
    compname[i]<-strsplit(strsplit(t2,"\\\\")[[1]][length(strsplit(t2,"\\\\")[[1]])],"\\.")[[1]][1]
  }
  
  ## Generate bivariate plots and barplots. 
  for (i in 1:comps)
  {
    for (j in 1:i)
    {
      if (i !=j)
      {
        cat(paste("Generating plot for ", compname[j], " and ", compname[i], "\n", sep = ""))
        #print(paste(i,"_",j))
        jpeg(filename=paste(OutputSufix,"_",compname[j],"_",compname[i],".jpg", sep = ""),width=1200,height=800) 
        plot(bpt1[,j+2],bpt1[,i+2], pch= 15, col = "red",xlab=compname[j],ylab=compname[i])
        points(tm[,j],tm[,i], pch= 19, col = "blue")
        dev.off()
      }
    }
    
    ## Generate barplots.
    ## Generate a sequence from min and max of the component to make bins of 100. Currently bins are fixed to 100.
    cat(paste("Generating barplot for ", compname[i], "\n", sep = ""))
    ##S1 = seq(range(bpt1[,i+2])[1],range(bpt1[,i+2])[2], (range(bpt1[,i+2])[2]-range(bpt1[,i+2])[1])/100)
    S1 = seq(range(bpt1[,i+2])[1],range(bpt1[,i+2])[2], (range(bpt1[,i+2])[2]-range(bpt1[,i+2])[1])/TotBins)
    tb1 = table(cut(bpt1[,i+2],S1))
    tb2=table(cut(tm[,i],S1))
    ## Converting the data into log. Because occurrences are few and other data is huge. 
    ntb1=cbind(log(as.data.frame(tb1)[,2]),log(as.data.frame(tb2)[,2]))
    ntb1[which(ntb1[,2]==-Inf),2]<- 0
    ntb1[which(ntb1[,1]==-Inf),1]<- 0
    jpeg(filename=paste(OutputSufix,"_",compname[i],".jpg", sep = ""),width=1200,height=800) 
    barplot(t(ntb1),col=c("red","blue"), beside=TRUE,xlab=compname[i])
    dev.off()
  } 
  cat("Done processing :)\n")
}
narayanibarve/ENMGadgets documentation built on May 25, 2019, 10:34 p.m.