#' Generates latitudinal centroid for each Hemisphere.
#'
#' @param f Dataframe (colnames:x, y, p). A dataframe containing latitudinal bin midpoint (x), species richness (y), and centroid weights (p).
#' @return Occurrence frequency distribution of species (dataframe and plot). If reps is FALSE, occurrences counts for each species given in each bin returned. If reps is TRUE, the same as FALSE but mean counts, standard deviation, n, and 95 percent confidence intervals.
#' @importFrom sp SpatialPointsDataFrame
#' @importFrom spatialEco wt.centroid
#' @export
LatCentroid <- function(f){
tmp <- subset(f, x > 0)
tmp <- subset(tmp, !is.na(y))
if(sum(tmp$y, na.rm = TRUE) == 0){
cent1 <- data.frame(matrix(NA, nrow = 1, ncol = 3))
colnames(cent1) <- c("latitude", "richness", "hemisphere")
}
else{p <- sp::SpatialPointsDataFrame(coords = data.frame(tmp[,1:2]), data = data.frame(tmp[,3]))
cent1 <- data.frame(spatialEco::wt.centroid(x = p, p = "tmp...3.", sp = TRUE))
cent1 <- cbind.data.frame(cent1, c("N"))
colnames(cent1) <- c("latitude", "richness", "hemisphere")}
tmp <- subset(f, x < 0)
tmp <- subset(tmp, !is.na(y))
if(sum(tmp$y, na.rm = TRUE) == 0){
cent2 <- data.frame(matrix(NA, nrow = 1, ncol = 3))
colnames(cent2) <- c("latitude", "richness", "hemisphere")
}
else{p <- sp::SpatialPointsDataFrame(coords = data.frame(tmp[,1:2]), data = data.frame(tmp[,3]))
cent2 <- data.frame(spatialEco::wt.centroid(x = p, p = "tmp...3.", sp = TRUE))
cent2 <- cbind.data.frame(cent2, "S")
colnames(cent2) <- c("latitude", "richness", "hemisphere")}
return(rbind.data.frame(cent1, cent2))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.