#' @title Displays a graph with the correlations between the variables
#' @name cor.data
#'
#' @description A function to display a graph with the correlations between the reported environmental variables.
#'
#' @param abio the rasters. Object of type RasterStack, generated by the function \code{\link[raster]{stack}}
#' @param plot logic. Plots one of the cut rasters.
#' @param method a character string indicating which correlation coefficient (or covariance) is to be computed: "pearson" (default), "kendall", or "spearman".
#' @param rep numeric. Number of points generated to extract rater values and use in correlation. The default is 1000.
#'
#' @details Graph indicating the correlations
#'
#' @return Returns a table with the correlation values between the variables.
#'
#' @author Diogo S. B. Rocha
#'
#' @seealso \code{\link[graphics]{pairs}}
#'
#' @examples
#' fnames <- list.files(path=paste(system.file(package="dismo"), '/ex', sep=''), pattern='grd', full.names=TRUE )
#' predictors <- raster::stack(fnames)
#' cor.data(abio = predictors)
#'
#' @import raster
#' @import dismo
#'
#' @export
cor.data = function(abio, plot = TRUE, method = "pearson", rep = 1000) {
if(class(abio)=="RasterStack"|class(abio)=="RasterLayer"){
backg <- randomPoints(abio, n = rep)
colnames(backg) = c("long", "lat")
backvalues = extract(abio, backg)
}else(backvalues=abio)
if (plot == T) {
panel.cor <- function(x, y, digits = 2, prefix = "", ...) {
usr <- par("usr")
on.exit(par(usr))
par(usr = c(0, 1, 0, 1))
r <- cor(x, y, method = method)
txt <- format(c(r, 0.123456789), digits = digits)[1]
txt <- paste0(prefix, txt)
text(0.5, 0.5, txt, cex = 1.2)
}
panel.hist <- function(x, ...){
usr <- par("usr"); on.exit(par(usr))
par(usr = c(usr[1:2], 0, 1.5) )
h <- hist(x, plot = FALSE)
breaks <- h$breaks; nB <- length(breaks)
y <- h$counts; y <- y/max(y)
rect(breaks[-nB], 0, breaks[-1], y, col = "gray", ...)
}
pairs(backvalues, lower.panel = panel.smooth, diag.panel= panel.hist, upper.panel = panel.cor)
}
return(round(cor(backvalues, method = method), 2))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.