#' Generates a global distribution plot of occurrences.
#'
#' @param data Data.frame (colnames:species, x, y). A dataframe containing species ID and occurrences (x, y).
#' @param res Numeric. Size of cells for distribution plot. Occurrences are counted within each cell.
#' @return A global plot of occurrences. Longitudinal and latitudinal margins indicate mean number of occurrences within bin == 'res'.
#' @importFrom sp SpatialPointsDataFrame
#' @importFrom raster rasterize raster
#' @importFrom viridisLite viridis
#' @importFrom rasterVis levelplot
#' @export
OccPlot <- function(data, res, write, name = "simulation"){
r <- raster::raster(res = res)
pts <- sp::SpatialPointsDataFrame(cbind.data.frame(data$x, data$y), data = data)
occ_plot <- raster::rasterize(pts, r, 'species', function(x, ...) length(x))
coul <- viridisLite::viridis(100)
if(write == TRUE){
suppressWarnings(dir.create("./Figures/"))
png(paste("./Figures/", name, "_occurrence_plot.png", sep = ""), width = 2400, height = 2000, res = 300)
suppressWarnings(print(rasterVis::levelplot(occ_plot, margin = list(axis = TRUE, FUN = 'mean'), main = "Distribution of occurrences", par.settings=list(panel.background=list(col="lightgrey")), col.regions = coul)))
dev.off()
}
suppressWarnings(rasterVis::levelplot(occ_plot, margin = list(axis = TRUE, FUN = 'mean'), main = "Distribution of occurrences", par.settings=list(panel.background=list(col="lightgrey")), col.regions = coul))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.