packages

library(ggplot2)
library(tidyverse)
## -- Attaching packages ----------------------------------------------------------------------------------------------------- tidyverse 1.2.1 --
## v tibble  1.4.2     v purrr   0.2.4
## v tidyr   0.8.0     v dplyr   0.7.4
## v readr   1.1.1     v stringr 1.3.0
## v tibble  1.4.2     v forcats 0.3.0
## -- Conflicts -------------------------------------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(forcats)
library(labeling)
library(ggmap)

ggmap

Getting Maps

Before we do anything else, we need our map layer upon which everything else will be plotted.

ggmap has a variety of functions (which have varying parameters) which can pull up maps from popular sources: Google Maps, Stamen Maps, OpenStreetMap

Retrieving StamenMap

Suppose you want a plain map of North America:

mapzone = c(left = -135, bottom = 25.75, right = -62, top = 75) #you specify the box
map = get_stamenmap(mapzone, zoom = 5, maptype = "toner-lite") 
## 63 tiles needed, this may take a while (try a smaller zoom).
## Map from URL : http://tile.stamen.com/toner-lite/5/4/5.png
## Map from URL : http://tile.stamen.com/toner-lite/5/5/5.png
## Map from URL : http://tile.stamen.com/toner-lite/5/6/5.png
## Map from URL : http://tile.stamen.com/toner-lite/5/7/5.png
## Map from URL : http://tile.stamen.com/toner-lite/5/8/5.png
## Map from URL : http://tile.stamen.com/toner-lite/5/9/5.png
## Map from URL : http://tile.stamen.com/toner-lite/5/10/5.png
## Map from URL : http://tile.stamen.com/toner-lite/5/4/6.png
## Map from URL : http://tile.stamen.com/toner-lite/5/5/6.png
## Map from URL : http://tile.stamen.com/toner-lite/5/6/6.png
## Map from URL : http://tile.stamen.com/toner-lite/5/7/6.png
## Map from URL : http://tile.stamen.com/toner-lite/5/8/6.png
## Map from URL : http://tile.stamen.com/toner-lite/5/9/6.png
## Map from URL : http://tile.stamen.com/toner-lite/5/10/6.png
## Map from URL : http://tile.stamen.com/toner-lite/5/4/7.png
## Map from URL : http://tile.stamen.com/toner-lite/5/5/7.png
## Map from URL : http://tile.stamen.com/toner-lite/5/6/7.png
## Map from URL : http://tile.stamen.com/toner-lite/5/7/7.png
## Map from URL : http://tile.stamen.com/toner-lite/5/8/7.png
## Map from URL : http://tile.stamen.com/toner-lite/5/9/7.png
## Map from URL : http://tile.stamen.com/toner-lite/5/10/7.png
## Map from URL : http://tile.stamen.com/toner-lite/5/4/8.png
## Map from URL : http://tile.stamen.com/toner-lite/5/5/8.png
## Map from URL : http://tile.stamen.com/toner-lite/5/6/8.png
## Map from URL : http://tile.stamen.com/toner-lite/5/7/8.png
## Map from URL : http://tile.stamen.com/toner-lite/5/8/8.png
## Map from URL : http://tile.stamen.com/toner-lite/5/9/8.png
## Map from URL : http://tile.stamen.com/toner-lite/5/10/8.png
## Map from URL : http://tile.stamen.com/toner-lite/5/4/9.png
## Map from URL : http://tile.stamen.com/toner-lite/5/5/9.png
## Map from URL : http://tile.stamen.com/toner-lite/5/6/9.png
## Map from URL : http://tile.stamen.com/toner-lite/5/7/9.png
## Map from URL : http://tile.stamen.com/toner-lite/5/8/9.png
## Map from URL : http://tile.stamen.com/toner-lite/5/9/9.png
## Map from URL : http://tile.stamen.com/toner-lite/5/10/9.png
## Map from URL : http://tile.stamen.com/toner-lite/5/4/10.png
## Map from URL : http://tile.stamen.com/toner-lite/5/5/10.png
## Map from URL : http://tile.stamen.com/toner-lite/5/6/10.png
## Map from URL : http://tile.stamen.com/toner-lite/5/7/10.png
## Map from URL : http://tile.stamen.com/toner-lite/5/8/10.png
## Map from URL : http://tile.stamen.com/toner-lite/5/9/10.png
## Map from URL : http://tile.stamen.com/toner-lite/5/10/10.png
## Map from URL : http://tile.stamen.com/toner-lite/5/4/11.png
## Map from URL : http://tile.stamen.com/toner-lite/5/5/11.png
## Map from URL : http://tile.stamen.com/toner-lite/5/6/11.png
## Map from URL : http://tile.stamen.com/toner-lite/5/7/11.png
## Map from URL : http://tile.stamen.com/toner-lite/5/8/11.png
## Map from URL : http://tile.stamen.com/toner-lite/5/9/11.png
## Map from URL : http://tile.stamen.com/toner-lite/5/10/11.png
## Map from URL : http://tile.stamen.com/toner-lite/5/4/12.png
## Map from URL : http://tile.stamen.com/toner-lite/5/5/12.png
## Map from URL : http://tile.stamen.com/toner-lite/5/6/12.png
## Map from URL : http://tile.stamen.com/toner-lite/5/7/12.png
## Map from URL : http://tile.stamen.com/toner-lite/5/8/12.png
## Map from URL : http://tile.stamen.com/toner-lite/5/9/12.png
## Map from URL : http://tile.stamen.com/toner-lite/5/10/12.png
## Map from URL : http://tile.stamen.com/toner-lite/5/4/13.png
## Map from URL : http://tile.stamen.com/toner-lite/5/5/13.png
## Map from URL : http://tile.stamen.com/toner-lite/5/6/13.png
## Map from URL : http://tile.stamen.com/toner-lite/5/7/13.png
## Map from URL : http://tile.stamen.com/toner-lite/5/8/13.png
## Map from URL : http://tile.stamen.com/toner-lite/5/9/13.png
## Map from URL : http://tile.stamen.com/toner-lite/5/10/13.png
ggmap(map) #prints the map for viewing

Note: large ggmaps can grow massively in size (150MB) since they are pieced together

Retrieving GoogleMap

When retreiving with googlemap, here we only need to center it.

map1 = get_googlemap(center=c(lon=-79.91, lat=43.26),zoom=13, size=c(640,640),scale=2,maptype='terrain') #'satellite','roadmap','hybrid'  are the other maptype offered
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=43.26,-79.91&zoom=13&size=640x640&scale=2&maptype=terrain&sensor=false
ggmap(map1) #hamilton for example, lon,lat very sensitive, so need to convert hours/minutes into a decimal

#zoom must be whole number, so a bit finnicky

#mcmaster satellite map
mcmastermap = get_googlemap(center=c(lon=-79.9192, lat=43.2609), zoom=15, maptype = 'satellite'); ggmap(mcmastermap)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=43.2609,-79.9192&zoom=15&size=640x640&scale=2&maptype=satellite&sensor=false

#you can get as close as the database permits
fireball = get_googlemap(center=c(lon=-79.92, lat=43.2609), zoom=19, maptype = 'satellite'); ggmap(fireball)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=43.2609,-79.92&zoom=19&size=640x640&scale=2&maptype=satellite&sensor=false