Biodiversity informatics workshop - Namibia University of Science and Technology JRS Biodiversity Foundation
dismo
package (Hijmans & Elith 2011) and from our friend Vijay Barve (https://vijaybarve.wordpress.com)So, we’ve already seen where do the primary biodiversity data comes from (e.g. point occurrences from inventories, collections, museums, etc.). Some of this data is already available in the internet!!
Install and load the following packages
library(sp)
library(raster)
library(rgdal)
library(maptools)
library(maps)
library(dismo)
Get some occurrence data for a particular species from GBIF, directly within R. This may take some time, given the number of occurrences for the selected species. Here, we’ll work with the Aardvark NOTA: we need to have an internet connection
aardvark <- gbif("Orycteropus","afer")
Take a look at the object dimensions (i.e. how many lines and columns)
dim(aardvark)
Check the column names, so we can look for the coordinates later
names(aardvark)
Create another object keeping only the coordinates (longitude/latitude)
aardvark_points <- aardvark[,c("lon","lat")]
NOTE: the variable names’ may be different (.e.g “LATITUDE”, “Latidude”, “lat”, etc.). Hence, it is always good to check the column names (as above)
Plot the points (x,y)
plot(aardvark_points[,1],aardvark_points[,2],col="blue",pch=19)
Add a world map, for reference. We need to load a dataobject from the maptools
package
data(wrld_simpl)
plot(wrld_simpl,add=T)
Are there any “weird” points?
Do the opposite, first plot the worldmap and then the points
plot(wrld_simpl,col="light yellow")
Add the points
points(aardvark_points[,1],aardvark_points[,2],col="blue",cex=0.2)
NOTE: the arguments pch
above and cex
here are used simply to define the size and tipe of the points. You can play with this!
Depending on the species, the map/points can be too large or be outside our region of interest. We can focus on a particular region/country. Let’s do this for Namibia and Angola
#Firts, get the "position" of Angola and Namibia in the "wrld_simpl" object
which(wrld_simpl$NAME=="Namibia")
which(wrld_simpl$NAME=="Angola")
#Then, use these indices (numbers) to subset the original object
angl.nmb_map <- wrld_simpl[c(6,218),]
Now you can repeat the steps above (plotting) for the selected region (Angola and Namibia) only.
Check if there are any duplicated points
aardvark_dups <- duplicated(aardvark_points)
### NOTE: the function "duplicated" returns the results of a logical test (e.g. TRUE or FALSE)
# How many are duplicates?
length(which(aardvark_dups==TRUE))
# How many are NOT duplicates?
length(which(aardvark_dups==FALSE))
# Keep only those lines that are not duplicates
aardvark_dups_row <- which(aardvark_dups==TRUE)
# What's the size? That is, how many points are duplicates
length(aardvark_dups_row)
# Create another object withoyt the duplicate records
aardvark_nodups <- aardvark_points[-aardvark_dups_row,]
# What are the dimensions of the new object?
dim(aardvark_nodups)
# Take a look at the first rows of data
head(aardvark_nodups)
Check if there are data with longitude/latitude of zero
aardvark_lonzero = subset(aardvark_nodups, lon==0)
aardvark_latzero = subset(aardvark_nodups, lat==0)
NOTE: Remember that variable names can be different (Latitude, latitude, lat, etc…)
Check if there are data with NAs
aardvark_nas <- which(is.na(aardvark_nodups[,1])==TRUE)
aardvark_points_nonas <- aardvark_nodups[-aardvark_nas,]
#how many NAs?
dim(aardvark_nodups)
dim(aardvark_points_nonas)
If we have time, we can try and repeat the steps above for a different species. Choose your favorite!!!
This part can be used to create “simple” range maps based on geometry (e.g. minimum convex polygons, etc.), without considering environmental variables (no ENMs or SDMs).
This model draws a convex hull around all ’presence’ points.
aardvark_hull <- convHull(aardvark_points_nonas)
aardvark_hullmodel <- predict(seamask, aardvark_hull, mask=TRUE)
plot(aardvark_hullmodel, main='Convex Hull')
plot(wrld_simpl, add=TRUE, border='dark grey')
points(aardvark_points_nonas, pch='+')
This model draws circles around all ’presence’ points.
aardvark_circ <- circles(aardvark_points_nonas, lonlat=TRUE)
aardvark_circles <- predict(seamask, aardvark_circ, mask=TRUE)
plot(aardvark_circles, main='Circles')
points(aardvark_points_nonas, pch=19, cex=0.5, col="red")
plot(wrld_simpl, add=TRUE, border='dark grey')