Setup

Etherpad for today: https://etherpad.wikimedia.org/p/nIvb84mY8r

Today we will be doing some work with spatial data. If the internet cooperates, we’ll start with some basics making and plotting points on a quick and dirty map using ggplot.

When you get to class however, install the following packages

install.packages('ggmap')
install.packages('rworldmap')
install.packages("raster")
install.packages("rgdal")
install.packages("sp")

# There is a slight chance that you will need to install the 'ncdf4' package. 
# But because it's slow, I'm going to leave it commented out in the case that we 
# don't need it.
#install.packages('ncdf4')

After you’ve gotten that going, download these data files:

Super useful cheatsheet for spatial stuff in R!


Worldmap warm-up

We’ll start with a simple world map. Just because these can be super useful and is a good ‘pick-me-up’ when you are feeling demoralised by R.

library(ggplot2)
library(rworldmap)
## Loading required package: sp
## ### Welcome to rworldmap ###
## For a short introduction type :   vignette('rworldmap')
# We have to tell R to load the world map data
# There are multiple worldmaps out there, with a high resolution version in the
# package mapdata if you're interested

worldmap <- map_data(map = "world")

# For interest you can take a look at the data for the worldmap
# Since it is going to play with ggplot, you'll notice that it has to be a 
# data.frame, which is different than the majority of spatial objects you will
# come across.
str(worldmap)
## 'data.frame':    25553 obs. of  6 variables:
##  $ long     : num  -133 -132 -132 -132 -130 ...
##  $ lat      : num  58.4 57.2 57 56.7 56.1 ...
##  $ group    : num  1 1 1 1 1 1 1 1 1 1 ...
##  $ order    : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ region   : chr  "Canada" "Canada" "Canada" "Canada" ...
##  $ subregion: chr  NA NA NA NA ...
# Plot entire world map with sites
# Note - we have to group, otherwise the map goes crazy and connects ALL the dots
# and takes forever
ggplot(data = worldmap, aes(x = long, y = lat, group = group)) +
  geom_polygon() + 
  theme_bw() +
  geom_point(aes(x = -71.0359052, y = 42.312449), colour = 'red')

#48.664799, -123.467094

Another quick and dirty way to make a plot is to use the ggmap extension of ggplot.

library(ggmap)

# Notice that the longitude is actually x! 
map <- get_map(location = c(lon = -122.4263441, lat = 37.8161694), zoom = 8, maptype = 'satellite')
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=37.816169,-122.426344&zoom=8&size=%20640x640&scale=%202&maptype=satellite&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
ggmap(map)

map <- get_map(location = c(lon = -122.4263441, lat = 37.8161694), zoom = 13, maptype = 'satellite')
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=37.816169,-122.426344&zoom=13&size=%20640x640&scale=%202&maptype=satellite&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
ggmap(map)

# option for saving any ggplot
#ggsave(filename = 'your_path_here/filename.png')

Often cleaner graphics are produced when you use simple shapefiles, but I often have a hard time tracking them down, and I’m no expert in them at all so unfortunately we won’t spend time on using them. Instead we’re going to look at extracting and manipulating geospatial data that we may want to plot, but also that we may want to extract as covariates.


Rasters (Adapted from: a NEON tutorial http://neondataskills.org/R/Raster-Data-In-R/)

Preparing a cropped version of the cumulative human impact data