Load the required packages:

library(rgeos)
library(rgdal)
library(sp)
library(raster)
library(maptools)

For this exercise we’ll use data on Malagasy mammals (range maps), which are available in the IUCN online database (http://www.iucnredlist.org/technical-documents/spatial-data) You could use another taxon or range maps (in this case, you need to work/have with the “shapefiles”: .shp files and associated ones)

mdg.mammals <- readShapePoly("Madagascar_shp/malagasy_mammals_IUCN.shp")

plot(mdg.mammals,col=rainbow(46, alpha=0.5))

“Project” in geographic coordinates – define the projection of the data/shapefiles

proj4string(mdg.mammals) <- CRS("+proj=longlat +datum=WGS84")

Create a raster for Madagascar

mdg.ras <- raster()
# Set the raster "extent" 
extent(mdg.ras) <- c(43, 51, -26, -12)

If you check the created object, you should see this:

>mdg.ras
class       : RasterLayer 
dimensions  : 180, 360, 64800  (nrow, ncol, ncell)
resolution  : 0.02222222, 0.07777778  (x, y)
extent      : 43, 51, -26, -12  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 

Change the raster resolution to 1º long-lat and add some values.

res(mdg.ras) <- 0.5
values(mdg.ras) <- 0

Now, it has to look like this:

> mdg.ras
class       : RasterLayer 
dimensions  : 28, 16, 448  (nrow, ncol, ncell)
resolution  : 0.5, 0.5  (x, y)
extent      : 43, 51, -26, -12  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
data source : in memory
names       : layer 
values      : 0, 0  (min, max)

Species richness raster

Create a raster for the Malagasy mammals based on the raster of Madagascar that we already have. This new raster will be the species richness map of mammals from Madagascar!

NOTE: There are other ways to create the richness raster and associated data (e.g. presence-absence matrix of species by cells) for macroecological and biogeographical analyses (see, for example, http://onlinelibrary.wiley.com/doi/10.1111/2041-210X.12401/abstract)

mdg.mamms.raster <- rasterize(mdg.mammals, mdg.ras, field="binomial",fun=function(x,...){length(unique(na.omit(x)))})

Plot the mammal raster.

plot(mdg.mamms.raster)
plot(wrld_simpl,add=T)

#change the color scale
brks <- seq(0, 60, by=5)
nb <- length(brks)-1 
cols <- rev(terrain.colors(nb))
plot(mdg.mamms.raster, breaks=brks, col=cols)
plot(wrld_simpl,add=T)

So, is it good? Is there a gradient? Towards where? What are the richest regions for mammals in Madagascar?

Scale dependency

Create another raster for Madagascar with a different resolution, 2 degrees long-lat

mdg.ras2 <- mdg.ras
# Define the resolution to 2 degrees
res(mdg.ras2) <- 2

Create the raster for the mammals at the new resolution (using the new raster of 2º)

mdg.mamms.raster2 <- rasterize(mdg.mammals, mdg.ras2, field="binomial",fun=function(x,...){length(unique(na.omit(x)))})

Plot the new raster

plot(mdg.mamms.raster2)
plot(wrld_simpl,add=T)

And now, is it good? similar to the previous one?

Another resolution, courser? Create another raster for Madagascar with a different resolution, 4 degrees long-lat

mdg.ras4 <- mdg.ras2

res(mdg.ras4) <- 4

Create the raster for the mammals at the new resolution (using the new raster of 4º)

mdg.mamms.raster4 <- rasterize(mdg.mammals, mdg.ras4, field="binomial",fun=function(x,...){length(unique(na.omit(x)))})

Plot the raster

plot(mdg.mamms.raster4)
plot(wrld_simpl,add=T)

Did the pattern change?

Where are the largest differences?

par(mfrow=c(1,3))

plot(mdg.mamms.raster, main="Mammal richness 0.5º")
plot(wrld_simpl,add=T)

plot(mdg.mamms.raster2, main="Mammal richness 2º")
plot(wrld_simpl,add=T)

plot(mdg.mamms.raster4, main="Mammals richness 4º")
plot(wrld_simpl,add=T)

So, is there a scale effect?