Basic mapping

Michael Taylor

2018/12/16

knitr::opts_chunk$set(cache=TRUE)

Load dataset canada.cities from the maps package.

data('canada.cities', package = 'maps')
str(canada.cities)
## 'data.frame':    916 obs. of  6 variables:
##  $ name       : chr  "Abbotsford BC" "Acton ON" "Acton Vale QC" "Airdrie AB" ...
##  $ country.etc: chr  "BC" "ON" "QC" "AB" ...
##  $ pop        : int  157795 8308 5153 25863 643 1090 1154 11972 1427 3604 ...
##  $ lat        : num  49.1 43.6 45.6 51.3 68.2 ...
##  $ long       : num  -122.3 -80 -72.6 -114 -135 ...
##  $ capital    : int  0 0 0 0 0 0 0 0 0 0 ...
(toronto <- canada.cities[canada.cities$name=="Toronto ON", c('name', 'lat', 'long')])
##           name   lat   long
## 830 Toronto ON 43.65 -79.38
library(ggmap)
## Loading required package: ggplot2
## Google Maps API Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it: see citation("ggmap") for details.
google_api <- secret::get_secret("google_api", key = "~/.ssh/id_rsa", vault = "~/.vault")
register_google(google_api)
to_map <- get_map(location = c(lon=toronto[[3]], 
                               lat=toronto[[2]]),
                  source = "google",
                  zoom = 12,
                  scale = 1)
## Source : https://maps.googleapis.com/maps/api/staticmap?center=43.65,-79.38&zoom=12&size=640x640&scale=1&maptype=terrain&language=en-EN&key=xxx
ggmap(to_map)

to_map_stamen <- get_map(location = c(lon=toronto[[3]], 
                               lat=toronto[[2]]),
                  source = "stamen",
                  maptype = "toner",
                  zoom = 12,
                  scale = 1)
## Source : https://maps.googleapis.com/maps/api/staticmap?center=43.65,-79.38&zoom=12&size=640x640&scale=2&maptype=terrain&key=xxx
## Source : http://tile.stamen.com/toner/12/1143/1493.png
## Source : http://tile.stamen.com/toner/12/1144/1493.png
## Source : http://tile.stamen.com/toner/12/1145/1493.png
## Source : http://tile.stamen.com/toner/12/1146/1493.png
## Source : http://tile.stamen.com/toner/12/1143/1494.png
## Source : http://tile.stamen.com/toner/12/1144/1494.png
## Source : http://tile.stamen.com/toner/12/1145/1494.png
## Source : http://tile.stamen.com/toner/12/1146/1494.png
## Source : http://tile.stamen.com/toner/12/1143/1495.png
## Source : http://tile.stamen.com/toner/12/1144/1495.png
## Source : http://tile.stamen.com/toner/12/1145/1495.png
## Source : http://tile.stamen.com/toner/12/1146/1495.png
## Source : http://tile.stamen.com/toner/12/1143/1496.png
## Source : http://tile.stamen.com/toner/12/1144/1496.png
## Source : http://tile.stamen.com/toner/12/1145/1496.png
## Source : http://tile.stamen.com/toner/12/1146/1496.png
ggmap(to_map_stamen)

sessionInfo()
## R version 3.5.1 (2018-07-02)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 18.04.1 LTS
## 
## Matrix products: default
## BLAS: /home/michael/anaconda3/lib/R/lib/libRblas.so
## LAPACK: /home/michael/anaconda3/lib/R/lib/libRlapack.so
## 
## locale:
## [1] en_CA.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] ggmap_2.7.904        ggplot2_3.0.0        RevoUtils_11.0.1    
## [4] RevoUtilsMath_11.0.0
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.18      compiler_3.5.1    pillar_1.3.0     
##  [4] plyr_1.8.4        bindr_0.1.1       bitops_1.0-6     
##  [7] tools_3.5.1       secret_1.0.0      digest_0.6.15    
## [10] jsonlite_1.5      evaluate_0.11     tibble_1.4.2     
## [13] gtable_0.2.0      png_0.1-7         pkgconfig_2.0.1  
## [16] rlang_0.2.1       curl_3.2          yaml_2.2.0       
## [19] blogdown_0.9.8    xfun_0.4.11       bindrcpp_0.2.2   
## [22] httr_1.3.1        withr_2.1.2       stringr_1.3.1    
## [25] dplyr_0.7.6       knitr_1.20        RgoogleMaps_1.4.2
## [28] rprojroot_1.3-2   grid_3.5.1        tidyselect_0.2.4 
## [31] glue_1.3.0        R6_2.2.2          jpeg_0.1-8       
## [34] rmarkdown_1.10    bookdown_0.7      purrr_0.2.5      
## [37] magrittr_1.5      backports_1.1.2   scales_0.5.0     
## [40] codetools_0.2-15  htmltools_0.3.6   assertthat_0.2.0 
## [43] colorspace_1.3-2  labeling_0.3      stringi_1.2.4    
## [46] openssl_1.0.2     lazyeval_0.2.1    munsell_0.5.0    
## [49] rjson_0.2.20      crayon_1.3.4

References

knitr::write_bib(.packages(), "packages.bib") 
## Warning in citation(pkg, auto = if (pkg == "base") NULL else TRUE): no date
## field in DESCRIPTION file of package 'ggmap'
## Warning in citation(pkg, auto = if (pkg == "base") NULL else TRUE): could
## not determine year for 'ggmap' from package DESCRIPTION file

Kahle, David, Hadley Wickham, and Scott Jackson. 2018. Ggmap: Spatial Visualization with Ggplot2. https://github.com/dkahle/ggmap.

R Core Team. 2018. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.