#' Calculate range sizes of species and generates a histogram.
#'
#' @param data Data.frame (colnames:x,y). A dataframe of species occurrences (species,x,y).
#' @param metric Character. Which metric to use to calculate range: "latitude", "longitude" or "GCD" (Great Circle distance; WGS84 ellipsoid).
#' @param breaks Integer. Number of breaks desired in plot. Value should relate to unit of measurement (e.g. in degree for latitude/longitude, km for GCD)
#' @return A dataframe of the latitudinal (degrees), longitudinal (degrees), or Great Circle distance (kilometres) range of each species.
#' @importFrom dplyr bind_rows
#' @importFrom sp spDists
#' @export
RangeFinder <- function(data, metric = "latitude", breaks, write = FALSE, name = "simulation"){
if(metric == "latitude"){
sp <- unique(data$species)
dat <- lapply(sp, function(i){
tmp <- subset(data, species == i)
mx <- max(tmp$y, na.rm = TRUE)
mn <- min(tmp$y, na.rm = TRUE)
species <- i
range <- abs((mn)-(mx))
output <- cbind.data.frame(species, range)
output
})
names(dat) <- unique(data$species)
output <- suppressWarnings(dplyr::bind_rows(dat))
max <- max(output$range, na.rm = TRUE)
brk <- seq(from = 0, to = (max+breaks), by = breaks)
if(write == TRUE){
suppressWarnings(dir.create("./Figures/"))
png(paste("./Figures/", name, "_latitudinal_range_plot.png", sep = ""), width = 2400, height = 2000, res = 300)
hist(output$range, main = "Latitudinal range", breaks = brk, col = "grey80", freq = TRUE, ylab = expression(bold(Frequency)), xlab = expression(bold(Latitude~(degree))))
dev.off()}
hist(output$range, main = "Latitudinal range", breaks = brk, col = "grey80", freq = TRUE, ylab = expression(bold(Frequency)), xlab = expression(bold(Latitude~(degree))))
}
else if(metric == "longitude"){
sp <- unique(data$species)
dat <- lapply(sp, function(i){
tmp <- subset(data, species == i)
mx <- max(tmp$x, na.rm = TRUE)
mn <- min(tmp$x, na.rm = TRUE)
species <- i
range <- (mn-mx) %% 360
if(range >= 180){range <- abs(range -360)}
else{range <- abs(range)}
output <- cbind.data.frame(species, range)
output
})
names(dat) <- unique(data$species)
output <- suppressWarnings(dplyr::bind_rows(dat))
max <- max(output$range, na.rm = TRUE)
brk <- seq(from = 0, to = (max+breaks), by = breaks)
if(write == TRUE){
suppressWarnings(dir.create("./Figures/"))
png(paste("./Figures/", name, "_longitudinal_range_plot.png", sep = ""), width = 2400, height = 2000, res = 300)
hist(output$range, main = "Longitudinal range", breaks = brk, col = "grey80", freq = TRUE, ylab = expression(bold(Frequency)), xlab = expression(bold(Longitude~(degree))))
dev.off()}
hist(output$range, main = "Longitudinal range", breaks = brk, col = "grey80", freq = TRUE, ylab = expression(bold(Frequency)), xlab = expression(bold(Longitude~(degree))))
}
else if(metric == "GCD"){
sp <- unique(data$species)
dat <- lapply(sp, function(i){
tmp <- subset(data, species == i)
tmp <- cbind.data.frame(tmp$x, tmp$y)
colnames(tmp) <- c("x", "y")
species <- i
tmp <- data.matrix(tmp)
range <- max(sp::spDists(tmp, tmp, longlat = TRUE), na.rm = TRUE)
output <- cbind.data.frame(species, range)
output
})
names(dat) <- unique(data$species)
output <- suppressWarnings(dplyr::bind_rows(dat))
max <- max(output$range, na.rm = TRUE)
brk <- seq(from = 0, to = (max+breaks), by = breaks)
if(write == TRUE){
suppressWarnings(dir.create("./Figures/"))
png(paste("./Figures/", name, "_GCD_range_plot.png", sep = ""), width = 2400, height = 2000, res = 300)
hist(output$range, main = "Great Circle Distance range", breaks = brk, col = "grey80", freq = TRUE, ylab = expression(bold(Frequency)), xlab = expression(bold(Range~(km))))
dev.off()}
hist(output$range, main = "Great Circle Distance range", breaks = brk, col = "grey80", freq = TRUE, ylab = expression(bold(Frequency)), xlab = expression(bold(Range~(km))))
}
return(output)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.