if (FALSE) {
data(crotalus)
# standardize species names
crotalus$genSp <- synonymMatch(crotalus$genSp, db='squam')
# get 10 species occurrence sets
uniqueSp <- unique(crotalus$genSp)[1:10]
uniqueSp <- uniqueSp[complete.cases(uniqueSp)]
# create range polygons
ranges <- vector('list', length = length(uniqueSp))
for (i in 1:length(uniqueSp)) {
x <- crotalus[which(crotalus$genSp == uniqueSp[i]),]
ranges[[i]] <- getDynamicAlphaHull(x, coordHeaders = c('decimallongitude',
'decimallatitude'), clipToCoast = 'terrestrial')
}
# name the polygons
names(ranges) <- uniqueSp
# keep only the polygons
ranges <- lapply(ranges, function(x) x[[1]])
# Create a rasterStack with the extent inferred from the polygons, and a cell
# resolution of 0.2 degrees.
# cells with the presence of a species get a value of 1, NA if absent.
rangeStack <- rasterStackFromPolyList(ranges, resolution = 0.2)
# calculate species richness per cell, where cell values are counts of species
richnessRaster <- calc(rangeStack, fun=sum, na.rm = TRUE)
# set values of 0 to NA
richnessRaster[richnessRaster == 0] <- NA
#plot
ramp <- colorRampPalette(c('blue','yellow','red'))
plot(richnessRaster, col=ramp(100))
plot(gshhs, add = TRUE, lwd=0.5)
}
Run the code above in your browser using DataLab