Creating range maps with R

Using the range data available from Lifemapper has been an adventure. Mapping the data onto the world map is relatively simple and straightforward:

library(maps)
library(ggplot2)

# read range data
range.data <- read.csv(file=range.file,header=T) # create world range map world.map <- map_data("world") ggplot(world.map, aes(long, lat)) +   geom_polygon(aes(group = group), fill = "white", color = "gray40", size = .2) +   geom_jitter(data = range.data, aes(lon, lat), alpha=0.6, size = 4, color = "red") +   annotate("text",x=175,y=-87,label=map.make,size=3) +   opts(title = map.title)

I wanted to map the same data onto a map of Africa. Unfortunately, I could not find a shapefile for Africa so I had to create my own. That exercise will be documented separately in another post. Once I had the shapefile created plotting the dta was only a little harder than before:

library(maps)
library(ggplot2)
library(rgeos)
library(maptools)
library(gpclib)

# read range data
range.data <- read.csv(file=range.file,header=T) #create Africa range map africa <- readShapeSpatial("GIS DataBase/Africa_adm0.shp") africa.map <- fortify(africa) ggplot(africa.map, aes(long, lat)) +   geom_polygon(aes(group = group), fill = "white", color = "gray40", size = .2) +   geom_jitter(data = range.data, aes(lon, lat), alpha=0.6, size = 4, color = "red") +   annotate("text",x=53,y=-48,label=map.make,size=3) +   opts(title = map.title)

Playing around with the different ggplot parameters was an interesting exercise in getting the map to look the way I thought it should. The final code to produce range maps for Malachite Kingfisher:

library(maps)
library(ggplot2)
library(rgeos)
library(maptools)
library(gpclib)

genus <- "Corythornis" species <- "cristatus" common <- "Malachite Kingfisher" # set up common variables map.title <- paste(genus," ",species," (",common,")",sep="") range.file = paste("birds/",genus,"_",species,".csv",sep="") filename0 <- paste("maps/map0_",genus,"_",species,".png",sep="") filename1 <- paste("maps/map1_",genus,"_",species,".png",sep="") map.make <- paste("created",format(Sys.Date(),format="%d %b %Y")) # read range data range.data <- read.csv(file=range.file,header=T) # create world range map world.map <- map_data("world") pWorld <- ggplot(world.map, aes(long, lat)) +   geom_polygon(aes(group = group), fill = "white", color = "gray40", size = .2) +   geom_jitter(data = range.data, aes(lon, lat), alpha=0.6, size = 4, color = "red") +   annotate("text",x=175,y=-87,label=map.make,size=3) +   opts(title = map.title) ggsave(filename=filename0,plot=pWorld,width=16,height=9) #create Africa range map africa <- readShapeSpatial("GIS DataBase/Africa_adm0.shp") africa.map <- fortify(africa) pAfrica <- ggplot(africa.map, aes(long, lat)) +   geom_polygon(aes(group = group), fill = "white", color = "gray40", size = .2) +   geom_jitter(data = range.data, aes(lon, lat), alpha=0.6, size = 4, color = "red") +   annotate("text",x=53,y=-48,label=map.make,size=3) +   opts(title = map.title) ggsave(filename=filename1,plot=pAfrica,width=5,height=5)

World map:

Africa map:

The next improvement I will be making is reading the data directly from the web site, but that is an exercise for another day.


August 22, 2012 at 8:21 pm | No comments


Comments are closed.