← Home

ggmapping

R has several packages for making maps, including the packages maps and ggmap. The latter uses smooth aesthetics of ggplot to make really good-looking maps. In ggmaps, there are several options for map tile types or static map backgrounds, including Google and Stamen maps. Check out this ggmap cheat sheet to get a look at the different mapping backgrounds available. Stamen maps take a little while to download at high resolution, but yield a nice final product. One downside of using ggmap is that global maps aren't available through Google or Stamen maps.

Here, you can download a Stamen map of the native monk parakeet range, then overlay GBIF sightings by country.

library(ggmap)
library(ggplot2)
library(maptools)  #for shapefiles
library(scales)  #for transparency
library(sp)
library(rgdal)
mp <- read.csv(paste("Myiopsitta_monachus_filtered_GBIF_", Sys.Date(), ".csv", sep = ""), header = TRUE)

The GBIF data needs to be filtered for South American sightings

mp1 <- mp[mp$decimalLatitude <= 0 & mp$decimalLongitude <= 0, ]

How many countries are in this filtered dataset?

table(mp1$countryCode)
## 
##   AE   AR   BE   BO   BR   BS   CL   DE   ES   FR   GB   GR   IL   IT   KY 
##    0 2053    0  137  473    0  553    0    0    0    0    0    0    0    0 
##   MX   PR   PT   PY   US   UY   VA 
##    0    0    0  136    0  173    0

The countryCode variable is a factor with empty levels, these empties need to be dropped before mapping

mp1$countryCode <- droplevels(mp1$countryCode)
table(mp1$countryCode)
## 
##   AR   BO   BR   CL   PY   UY 
## 2053  137  473  553  136  173

Download the Stamen map, you need Internet for this. A higher zoom argument makes the download time much longer, but gives you better resolution of waterways and other natural boundaries

bbox <- c(left = -80, bottom = -45, right = -40, top = 2)
smap <- get_stamenmap(bbox = bbox, zoom = 8,
                      maptype = c("toner-background"))

Read in species distribution polygon and initialize data for mapping

mp.distr <- readOGR("./_data", "Myiopsitta_monachus_1608_NS")

dats <- data.frame(lon = mp1$decimalLongitude, lat = mp1$decimalLatitude, 
                   country = mp1$countryCode)

fill.cols <- diverge_hcl(length(table(dats$co)), alpha = 0.6)

cols <- rep("black", length(fill.cols))

Overlay GBIF sightings and the species distribution onto the Stamen map.

tiff("mymon-south-america.tiff", res = 300, units = "cm", width = 20,
     height = 20)

ggmap(smap) + 

  geom_polygon(aes(x = long, y = lat, group = group), color = topo.colors(12)[5], fill = topo.colors(12, alpha = 0.6)[7], size = 1, data = mp.distr) +

  geom_point(aes(x = lon, y = lat, color = country, fill = country, size = country, shape = country), data = dats) + 

  scale_fill_manual(values = fill.cols) +

  scale_colour_manual(values = cols) + 

  scale_shape_manual(values = rep(21, length(fill.cols))) + 

  scale_size_manual(values = rep(4, length(fill.cols))) 

dev.off()