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 Utility Functions

geocode()

Suppose you want to collect location data (if you’re assembling a dataset, or just in general). Geocode takes an address and returns coordinates.

geocode("mcmaster university", output="more")
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=mcmaster%20university&sensor=false
## Warning: geocode failed with status OVER_QUERY_LIMIT, location = "mcmaster
## university"
##   lon lat
## 1  NA  NA

output=“more” can be specified if you want more extensive information other than just the lat/lon

revgeocode()

You can also use this package as an atlas of sorts:

coordinates = geocode("mcmaster university")%>%as.numeric()
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=mcmaster%20university&sensor=false
revgeocode(coordinates)
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?latlng=43.260879,-79.9192254&sensor=false
## [1] "University Ave, Hamilton, ON L8S, Canada"

Distances

We can also measure map distances using mapdist(from=‘x’,to=‘y’).

Lesser known, or cities that don’t have a unique name require the country (in many cases).

mapdist(from="toronto",to="hamilton") #observe, won't work
## by using this function you are agreeing to the terms at :
## http://code.google.com/apis/maps/documentation/distancematrix/
## Information from URL : http://maps.googleapis.com/maps/api/distancematrix/json?origins=toronto&destinations=hamilton&mode=driving&sensor=false
##      from       to km miles minutes hours
## 1 toronto hamilton NA    NA      NA    NA
x=c('toronto','boston'); y=c('hamilton, canada','cleveland')
mapdist(x,y)
## by using this function you are agreeing to the terms at :
## http://code.google.com/apis/maps/documentation/distancematrix/
## Information from URL : http://maps.googleapis.com/maps/api/distancematrix/json?origins=boston&destinations=cleveland&mode=driving&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/distancematrix/json?origins=toronto&destinations=hamilton+canada&mode=driving&sensor=false
##      from               to       m       km     miles seconds minutes
## 1 toronto hamilton, canada   67896   67.896  42.19057    2958    49.3
## 2  boston        cleveland 1029786 1029.786 639.90902   34860   581.0
##       hours
## 1 0.8216667
## 2 9.6833333

Route planning (the hard way)

We can use route() or trek(). trek() tends to hug the roads more closely.

routeplan= route(
  from='mcmaster university',
  to='guelph university',
  alternatives= TRUE
)
## Information from URL : http://maps.googleapis.com/maps/api/directions/json?origin=mcmaster+university&destination=guelph+university&mode=driving&units=metric&alternatives=true&sensor=false
qmap('puslinch, canada', zoom = 10, maptype = 'roadmap',
     base_layer=ggplot(aes(x=startLon,y=startLat),data=routeplan))+
  geom_leg(
    aes(x=startLon,y=startLat,xend=endLon,yend=endLat,
           colour=route),
alpha=.7, size=2, data=routeplan) +
  labs(x="longitude",y="latitude",colour="Route")+
  facet_wrap(~route,ncol=2)+
  theme(legend.position='bottom')
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=puslinch,+canada&zoom=10&size=640x640&scale=2&maptype=roadmap&language=en-EN&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=puslinch,%20canada&sensor=false
## Warning: `panel.margin` is deprecated. Please use `panel.spacing` property
## instead

Here we used ggplot for the first time, by adding layers to qmap. We will explore it in more detail in the next section.