Final Project Part 2 PH251D

Tutorial for the package 'ggmap'

Introduction

The term epidemiology often conjures up an image of John Snow and his famous map of cholera cases around the Broad Street Pump. From the early days of epidemiology, disease mapping has played a significant role in the study of the distribution and determinants of disease. With the rapid growth of technology in the 20th century, progams like ArcGIS and QGIS have been developed to work with maps and geographic information. One of the programs that has evolved to work with spatial data visualization/mapping and analysis is R (through the expansion of mapping packages available for download). Although there are a few available R packages that already exist for mapping alone, 'ggmap' offers R users new tools and potential in the analysis and visualization of spatial data.

Demonstration

In this tutorial, I will introduce several functions in 'ggmap' as well as some functions in 'ggplot2' that are of use in spatial data visualization using maps

library(ggplot2)  #loading ggplot2 package
library(ggmap)  #loading ggmap package

geocode function

The geocode function geocodes a location using Google Maps. The default of the output argument is just to provide a data frame with the latitude and longitude, but there is the option of changing the output argument to provide more information (such as address, postal code, country, etc.). Note: The 'geocode' function can be used to obtain a data frame of points to plot on top of the basemap obtained from the 'get_map' function (explained below); however, the 'get_map' function does not need a latitude and longitude as input for location and accepts a character string as a location.

geocode(location = 'xyz',
  output = c("latlon", "latlona", "more", "all"))
geocode("UC Berkeley")  #to get the latitude and longitude of UC Berkeley
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=UC+Berkeley&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
##      lon   lat
## 1 -122.3 37.87
geocode(c("UC Berkeley", "UCLA", "UCSD", "UC Merced", "UC Santa Cruz", "UC Santa Barbara", 
    "UC Irvine", "UC Davis", "UC Riverside", "UCSF"))  #to get the latitude and longitude of all the ten UC campuses, output will be in a data frame
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=UC+Berkeley&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=UCLA&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=UCSD&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=UC+Merced&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=UC+Santa+Cruz&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=UC+Santa+Barbara&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=UC+Irvine&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=UC+Davis&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=UC+Riverside&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=UCSF&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
##       lon   lat
## 1  -122.3 37.87
## 2  -118.4 34.07
## 3  -117.2 32.88
## 4  -120.4 37.37
## 5  -122.1 36.99
## 6  -119.8 34.41
## 7  -117.8 33.64
## 8  -121.5 38.55
## 9  -117.3 33.97
## 10 -122.5 37.76
geocode(c("UC Berkeley", "UCLA", "UCSD", "UC Merced", "UC Santa Cruz", "UC Santa Barbara", 
    "UC Irvine", "UC Davis", "UC Riverside", "UCSF"), output = "latlona")  #specifying the output to latlona will include address of the locations in the data frame
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=UC+Berkeley&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=UCLA&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=UCSD&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=UC+Merced&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=UC+Santa+Cruz&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=UC+Santa+Barbara&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=UC+Irvine&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=UC+Davis&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=UC+Riverside&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=UCSF&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
##       lon   lat
## 1  -122.3 37.87
## 2  -118.4 34.07
## 3  -117.2 32.88
## 4  -120.4 37.37
## 5  -122.1 36.99
## 6  -119.8 34.41
## 7  -117.8 33.64
## 8  -121.5 38.55
## 9  -117.3 33.97
## 10 -122.5 37.76
##                                                                                                                          address
## 1                                                    university of california berkeley, 101 sproul hall, berkeley, ca 94704, usa
## 2                                           university of california los angeles, 405 hilgard avenue, los angeles, ca 90095, usa
## 3                                                  university of california - san diego, 9500 gilman dr, la jolla, ca 92093, usa
## 4                                                            uc merced, university of california - merced, california 95340, usa
## 5                                               university of california santa cruz, 1156 high street, santa cruz, ca 95064, usa
## 6                                                            uc santa barbara, 552 university road, santa barbara, ca 93106, usa
## 7                                                                         university of california irvine, irvine, ca 92697, usa
## 8                                                                   uc davis, 2221 stockton boulevard, sacramento, ca 95817, usa
## 9                                           university of california, riverside, 900 university avenue, riverside, ca 92521, usa
## 10 ucsf, university of california, san francisco, medical center : dentistry, 521 parnassus avenue, san francisco, ca 94117, usa

get_map function

The function 'get_map' queries the static map source of your choice (i.e. Google Maps, OpenStreetMap, Stamen Maps, CloudMade Maps) for the location the user specifies and then downloads a map as an image from the source.

The user can specify:

  1. location - input either address or longitude and latitude
  2. zoom - input either 'auto' or an integer from 3 (continent) - 21 (building), default zoom is 10
  3. map type - options are terrain, satellite, roadmap, hybrid, toner, and watercolor
  4. color - color or black-and-white
  5. static map source - Google Maps, OpenStreetMap, Stamen Maps, or CloudMade maps
  6. return URL only or map and URL - return URL only or not
  7. file name - destination file for download
get_map(location = c('address', c(lon = #, lat = #)), 
  zoom = c("auto", "integer - from 3 (continent) - 21 (building)")
  maptype = c("terrain", "satellite", "roadmap", "hybrid", "toner", "watercolor")
  color = c("color", "bw")
  source = c("google", "osm", "stamen", "cloudmade")
  urlonly = c("TRUE", "FALSE")
  filename = "xyz")

Alternatively, we can specify the static map source with the functions 'get_openstreetmap', 'get_stamenmap', 'get_googlemap', and 'get_cloudmademap'.

ggmap function

The function 'ggmap' plots a ggmap object (which is obtained from the get_map function, i.e. the map image downloaded)

ggmap(ggmap)
SF <- get_map(location = "San Francisco", zoom = 12, maptype = "watercolor", 
    source = "stamen")  #downloads watercolor map of San Francisco from Stamen Maps
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=San+Francisco&zoom=12&size=%20640x640&maptype=terrain&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=San+Francisco&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
ggmap(SF)  #plots map of San Francisco from Stamen Maps

plot of chunk unnamed-chunk-3

Berkeley <- get_map(location = "Berkeley", zoom = 15, maptype = "roadmap")  #downloads map from Google Maps (default)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=Berkeley&zoom=15&size=%20640x640&scale=%202&maptype=roadmap&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Berkeley&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
ggmap(Berkeley)  #plots map of Berkeley from Google Maps

plot of chunk unnamed-chunk-3

UCBerkeley1 <- get_map(location = c(-122.259907, 37.87194), zoom = 15)  #downloads map of UC Berkeley (longitude and latitude coordinates used - in that order) from Google Maps
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=37.87194,-122.259907&zoom=15&size=%20640x640&scale=%202&maptype=terrain&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
ggmap(UCBerkeley1)  #plots map of UC Berkeley

plot of chunk unnamed-chunk-3

UCBerkeley2 <- get_map(location = c(-122.259907, 37.87194), zoom = 16, maptype = "satellite")  #downloads map of UC Berkeley 
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=37.87194,-122.259907&zoom=16&size=%20640x640&scale=%202&maptype=satellite&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
ggmap(UCBerkeley2)  #plots map of UC Berkeley

plot of chunk unnamed-chunk-3

qmap function

The 'qmap' or 'quick map plot' function combines get_map and ggmap into one function. The function arguments combine that of get_map and ggmap with the user being able to specify location, zoom, map type, etc.

qmap(location = c(-122.259907, 37.87194), zoom = 15)  #downloads map of UC Berkeley and plots map of UC Berkeley by using just one function
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=37.87194,-122.259907&zoom=15&size=%20640x640&scale=%202&maptype=terrain&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms

plot of chunk unnamed-chunk-4

geom_point function from ggplot2

The 'geom_point' function from the package 'ggplot2' is used to create scatterplots. As a ggmap is essentially a ggplot object, we can use 'geom_point' as well as other functions such as geom_polygon and stat_density2d (among others) to plot points and shapes in layers (on top of the basemap). For this tutorial, we will focus on using the 'geom_point' function to plot longitudes and latitudes.

The user can use 'geom_point' to specify:

  1. aes - generates aesthetic mappings that describe how variables in the data are mapped
  2. data - data frame used
  3. color - color of plotted points
  4. size - size of the points
  5. shape - shape of the point geom_point(data = xyz, aes(x=longitude in data frame, y = latitude in data frame, colour = variable in data frame you want points color coded by), colour = 'can specify color of points' size = # specifying how big you want the points to be)
UCschools <- geocode(c("UC Berkeley", "UCLA", "UCSD", "UC Merced", "UC Santa Cruz", 
    "UC Santa Barbara", "UC Irvine", "UC Davis", "UC Riverside", "UCSF"))  #get a data frame of longitude and latitude of UC campuses
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=UC+Berkeley&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=UCLA&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=UCSD&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=UC+Merced&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=UC+Santa+Cruz&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=UC+Santa+Barbara&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=UC+Irvine&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=UC+Davis&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=UC+Riverside&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=UCSF&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
California <- get_map(location = "California", zoom = 6, maptype = "roadmap", 
    source = "google")  #download a map of California from Google Maps
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=California&zoom=6&size=%20640x640&scale=%202&maptype=roadmap&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=California&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
UCschoolslocation <- ggmap(California) + geom_point(data = UCschools, aes(x = lon, 
    y = lat), colour = "blue", size = 4)  #creates a map of California as the basemap and plots the longitude and latitude of the UC campuses in a layer on top (data obtained from the geocode function above)
UCschoolslocation  #map of California with UC campuses plotted

plot of chunk unnamed-chunk-5

USA <- get_map(location = "USA", zoom = 4, maptype = "terrain", source = "google", 
    color = "bw")  #download a map of the continental United States from Google in black and white
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=USA&zoom=4&size=%20640x640&scale=%202&maptype=terrain&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=USA&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
library(maps)  #load maps package
data(us.cities)  #load us.cities data from the maps package
continentalUS <- subset(us.cities, country.etc != "AK" & country.etc != "HI")  #removing cities in Alaska and Hawaii from the data frame
quantile(continentalUS$pop)  #looking at the distribution of city population size in the continental US cities data frame
##      0%     25%     50%     75%    100% 
##    8003   50126   66628  103618 8124427
poplabels <- c("small cities", "medium cities", "large cities", "largest cities")  #creating labels to create a population size categorical variable from the continuous variable in the data frame
population.size <- cut(continentalUS$pop, breaks = c(8003, 50126, 66628, 103618, 
    8124427), right = FALSE, labels = poplabels)  #creating a new categorical variable for population size and labelling it
continentalUS$population.size <- population.size  #adding a new variable to the data frame
# creates a map of the continental US with the various US cities in the data
# frame plotted, color coded by population size
US.cities <- ggmap(USA, legend = "topleft") + geom_point(data = continentalUS, 
    aes(x = long, y = lat, colour = population.size, size = population.size), 
    size = 2)
US.cities

plot of chunk unnamed-chunk-5

Discussion

The package 'ggmap' is a relatively newer package that allows for easy spatial visualization using Google Maps [or other static maps such as OpenStreetMap and Stamen Maps or CloudMade Maps (both of which heavily use OpenStreetMap)]. Used in conjunction with the package 'ggplot2', 'ggmap' allows R users (epidemiologists among them) to create sophisticated visualizations of spatial data. With these maps as a basemap, users can then plot spatial data in layers on top using 'ggplot2'. The vast amount of map data available through OpenStreetMap and Google Maps (including different map types such as road map, satellite, terrain, and even watercolor) make 'ggmap' a powerful package to learn for those interested in creating and working with maps and geographic information and the analysis of spatial data. Additionally, the relative ease of learning and using this package further contributes its already high value. However, given the open source nature of R and the constantly growing number of packages available, we can expect the release of R packages in the future that improve or build upon the capabilities in 'ggmap'.

References

Kahle, David, and Hadley Wickham. “ggmap: Spatial Visualization with ggplot2.” The R Journal. Vol. 5/1 (June 2013). 144-161.