Maps of Primates on the IUCN Red List

A friend needed some points plotted quickly on a map, so I started to mess around with some tools for plotting simple maps in R. I needed something to play with, so I found the data from the IUCN Red List 2008 on Threatened Primates by Habitat Country. (You can download my data file from here.) Anyway, below are the maps. Click the images for a larger, better PDF. Technical notes with R code follow.

Total critically endangered (CR), endangered (EN), and vulnerable (VU) primate species by country:

Total critically endangered (CR) primate species by country:

Total endangered (EN) primate species by country:

Total vulnerable (VU) primate species by country:

Total primate species by country:

Technical Notes

The maps and maptools are really quite simple to use. Here I can display only Pennsylvania from the built-in "states" map, and put a dot and label on my home town:

# Load necessary libaries
library(maptools);
library(maps);

home.lat = 40.214082;
home.lon = -75.26347;

# Plot map of PA
map("state", region="pennsylvania", fill=TRUE, 
	col="darkgreen", bg="lightblue");
# Add point
points(home.lon, home.lat, pch=13, cex=8, lwd=5);
# Add label
text(home.long, home.lat, pos=2, offset=2, labels="Home", cex=5);

The world map included in the package is out of date, but a newer one is available called "wrld_simpl." Here's how to use it in a similar way. This time a dot is placed on Mongolia:

# Load necessary libaries
library(maptools);
library(maps);

# Load simple world map.
data(wrld_simpl)

# Draw world map.
plot(wrld_simpl)

# Random lat/long for Mongolia that I pulled off the web.
mongolia.lat = 47.916667
mongolia.lon = 106.883333

# Add point
points(mongolia.lon, mongolia.lat, pch=16, col='red', cex=2)
text(mongolia.lon, mongolia.lat, pos=3, offset=1, 
	labels="Mongolia", cex=2, col='red')

The primate plotting R script in its entirety can be downloaded from here. It requires the aforementioned libaries, plus RColorBrewer for the color gradients and plotrix for the legend. The data file is available here.

# Load necessary libraries
library(maptools)
library(maps)
library(plotrix)	# Only necessary for the legend
library(RColorBrewer)
data(wrld_simpl)

# Load data from text file.
prim = read.csv(file="primate_sp_by_country.txt", header=TRUE)


# Function to plot data column on map

plot.primates = function(col.name, 
                         main.title="", 
                         palette="Blues", 
                         leg.breaks=10){

  # Draw world map. 
  par(mar=c(0, 2, 2, 8) + 0.1)
  plot(wrld_simpl)

  # Add title to plot
  mtext(main.title, cex=1.5)

  # Define palette
  # Expand the 9 colors in the RColorBrewer
  #  to the number of colors we have
  col.pal=colorRampPalette(brewer.pal(9,palette))(max(prim[col.name]));
	
  # Add a color for zero
  col.pal = c("#FFFFFF", col.pal)
	
  # Add country colors to data.frame
  prim2 = prim
  prim2$country.colors = col.pal[1 + unlist(prim[col.name])]
	
  # Function to color in a country
  color.country = function(x) {
	
    if (sum(wrld_simpl$NAME == x[1], na.rm=1) == 0) {
      stop("ERROR: Country not found in shape file: ", x[1])
    } else {
      plot(wrld_simpl[wrld_simpl$NAME == x[1],],
      col=x[10],axes=TRUE,add=TRUE)
    }
				
  }

  # Apply color.country function to each row
  apply(prim2, 1, color.country)
	
  # Pull out some indices to label legend
  leg.colors.indices = floor(seq(0,
                                 max(prim2[col.name]),
                                 by=leg.breaks))
	
  # Add legend with plotix
  color.legend(200,-50,215,50,
    leg.colors.indices,
    col.pal,
    cex=1,
    align="lt",
    gradient="y")
}


plot.primates("Species", 
              main.title="Total Primate Species", 
              palette="Blues")

plot.primates("CriticallyEndangered", 
              main.title="Total Critically Endangered", 
              palette="YlOrRd", 
              leg.breaks=1)

plot.primates("Endangered", 
              main.title="Total Endangered", 
              palette="YlOrRd", 
              leg.breaks=1)

plot.primates("Vulnerable", 
              main.title="Total Vulnerable", 
              palette="YlOrRd", 
              leg.breaks=1)