class: center, middle, inverse, title-slide # ggmap ## RaukR, 2018. ### Sebastian DiLorenzo ### 09 June, 2018 --- class: spaced ## ggmap .center.large[Plotting maps in R with `ggplot2`] .pull-left-40[ * https://github.com/dkahle/ggmap * Author: David Kahle + Baylor University, Waco, Texas ] .pull-right-50[ ```r ggmap(baylor.map, extent = "device") ``` <img src="ggmap_Sebastian_files/figure-html/unnamed-chunk-3-1.png" style="display: block; margin: auto auto auto 0;" /> ] ??? The R package ggmap was create by David Kahle of Baylor University in Waco, Texas, as seen plotted here using ggmap. Its description on github is "Plotting maps in R with ggplot2", which is precisely why it is a good package to use together with ggplot2. --- ## Quick-theory: Add data to a map .center[ ```r ggmap(baylor.map) ``` <img src="ggmap_Sebastian_files/figure-html/unnamed-chunk-4-1.png" style="display: block; margin: auto;" /> ] ??? When plotting on maps you are using a coordinate system, usually longitudes and latitudes, as the axes. So the information you are adding to this map, for example, has to be something like crime, restaurants, parks or something else where we know the location. --- ## The functions of ggmap .pull-left-50[ * `get_map()` + input: location + output: *ggmap* object + `source` + Google, Stamen, OpenStreetMap and CloudMade + `maptype` + `zoom` ] ??? ggmap has several useful functions that you can try hands-on in the exercise. get_map() is used to query one of several map servers such as google, stamen, open street map and cloudmade. You input a string used to search for the location, and the output is a ggmap class object. You can change between several types of maps, and how zoomed in or out you are. -- .pull-left-50[ * `ggmap()` + input: *ggmap* object + output: *ggplot* object ] ??? The confusingly named ggmap() is the function this time, not the package, used to plot the ggmap object returned from get_map(). And the great thing here is that the output is a ggplot object, that means you can follow a call to ggmap() by any of the ggplot functions such as geom_point or geom_density. -- .pull-right-50[ * `geocode()` + input: location + output: latitude and longitude ] ??? geocode() takes a location and returns latitude and longitude rather than a map object. This is useful for creating data to plot on top of your map. -- .pull-right-50[ * `trek()` + input: *from* and *to* locations + output: multiple latitudes and longitudes + `transit` ] ??? trek() is a function that take a From and a To location and asks the same servers as get_map used if they can give us a route between them. A route in this case being a bunch of lat/lon coordinates. --- ## Example .pull-left-50.smallish[ ```r #Get the map visby.watercolor.map <- get_map( "Campus Gotland, Visby", zoom = 15, maptype = "watercolor") #Inspect ggmap(visby.watercolor.map, extent = "device") ``` ] .pull-right-50[ <img src="ggmap_Sebastian_files/figure-html/unnamed-chunk-6-1.png" style="display: block; margin: auto 0 auto auto;" /> ] ??? Now for a quick example before the exerice. Here I have started by saving a ggmap object, visby.watercolor.map, using get_map(). Because I thought it was cute and didnt have a bunch of text on it, I use the maptype watercolor. --- ## Example .pull-left-50.smallish[ ```r #Get the map visby.watercolor.map <- get_map( "Campus Gotland, Visby", zoom = 15, maptype = "watercolor") #Create a tibble of Visby's # most important locations visby.locations <- tibble( location = c("Mullbärsgården, Visby", "Visby Hostel, Visby", "Glassmagasinet, Visby")) #Inspect ggmap(visby.watercolor.map, extent = "device") ``` ] .pull-right-50[ <img src="ggmap_Sebastian_files/figure-html/unnamed-chunk-8-1.png" style="display: block; margin: auto 0 auto auto;" /> ] ??? Now there are three important places in visby that I want to add to the map, and those are Mullbärsgården, where some of you live, Visby hostel, where some others of you live, and glassmagasinet, they make icecream. I have saved those locations in a tibble called visby.locations. --- ## Example .pull-left-50.smallish[ ```r #Get the map visby.watercolor.map <- get_map( "Campus Gotland, Visby", zoom = 15, maptype = "watercolor") #Create a tibble of Visby's # most important locations visby.locations <- tibble( location = c("Mullbärsgården, Visby", "Visby Hostel, Visby", "Glassmagasinet, Visby")) #Get the geocode, lat/lon, # of the locations visby.geo <- geocode( visby.locations$location) #Inspect ggmap(visby.watercolor.map, extent = "device") ``` ] .pull-right-50[ <img src="ggmap_Sebastian_files/figure-html/unnamed-chunk-10-1.png" style="display: block; margin: auto 0 auto auto;" /> ] ??? Next I use the tibble as input for ggmaps geocode() function to get the latitude and longitude for those places. --- ## Example .pull-left-50.smallish[ ```r #Get the map visby.watercolor.map <- get_map( "Campus Gotland, Visby", zoom = 15, maptype = "watercolor") #Create a tibble of Visby's # most important locations visby.locations <- tibble( location = c("Mullbärsgården, Visby", "Visby Hostel, Visby", "Glassmagasinet, Visby")) #Get the geocode, lat/lon, # of the locations visby.geo <- geocode( visby.locations$location) #Create a data.frame of the data # for easier plotting visby.places <- cbind(visby.locations, visby.geo) #Inspect ggmap(visby.watercolor.map, extent = "device") ``` ] .pull-right-50[ <img src="ggmap_Sebastian_files/figure-html/unnamed-chunk-12-1.png" style="display: block; margin: auto 0 auto auto;" /> ] ??? And then we bind them together to a single object with cbind. --- ## Example .pull-left-50.smallish[ ```r #Get the map visby.watercolor.map <- get_map( "Campus Gotland, Visby", zoom = 15, maptype = "watercolor") #Create a tibble of Visby's # most important locations visby.locations <- tibble( location = c("Mullbärsgården, Visby", "Visby Hostel, Visby", "Glassmagasinet, Visby")) #Get the geocode, lat/lon, # of the locations visby.geo <- geocode( visby.locations$location) #Create a data.frame of the data # for easier plotting visby.places <- cbind(visby.locations, visby.geo) #Plot important locations ggmap(visby.watercolor.map, extent="device") + geom_point( data = visby.places, aes(x = lon, y = lat), color = 'red', size = 5) ``` ] .pull-right-50[ <img src="ggmap_Sebastian_files/figure-html/unnamed-chunk-14-1.png" style="display: block; margin: auto 0 auto auto;" /> ] ??? We can now update our plot, using ggplots geom_point() to place red dots on the locations. --- ## Example .pull-left-50.smallish[ ```r #Get the map visby.watercolor.map <- get_map( "Campus Gotland, Visby", zoom = 15, maptype = "watercolor") #Create a tibble of Visby's # most important locations visby.locations <- tibble( location = c("Mullbärsgården, Visby", "Visby Hostel, Visby", "Glassmagasinet, Visby")) #Get the geocode, lat/lon, # of the locations visby.geo <- geocode( visby.locations$location) #Create a data.frame of the data # for easier plotting visby.places <- cbind(visby.locations, visby.geo) #Plot important locations ggmap(visby.watercolor.map, extent="device") + geom_point( data = visby.places, aes(x = lon, y = lat), color = 'red', size = 5) + geom_text(data = visby.places, aes(label = location), hjust=0, vjust=-1) ``` ] .pull-right-50[ <img src="ggmap_Sebastian_files/figure-html/unnamed-chunk-16-1.png" style="display: block; margin: auto 0 auto auto;" /> ] ??? And incase you dont know which one is which, lets use geom_text() to put labels on them. Great, but how do I get from Mullbärsgården to Glassmagasinet? --- ## Example .pull-left-50.smallish[ ```r #Create the route with trek() goto_icecream <- trek( "Mullbärsgården, Visby", "Glassmagasinet, Visby", structure = "route", mode = "bicycling") #Plot important locations ggmap(visby.watercolor.map, extent="device") + geom_point( data = visby.places, aes(x = lon, y = lat), color = 'red', size = 5) + geom_text(data = visby.places, aes(label = location), hjust=0, vjust=-1) ``` ] .pull-right-50[ <img src="ggmap_Sebastian_files/figure-html/unnamed-chunk-18-1.png" style="display: block; margin: auto 0 auto auto;" /> ] ??? Well lets clear some space here and use ggmap function trek(), from Mullbärsgården, to Glassmagasinet. --- ## Example .pull-left-50.smallish[ ```r #Create the route with trek() goto_icecream <- trek( "Mullbärsgården, Visby", "Glassmagasinet, Visby", structure = "route", mode = "bicycling") #Plot important locations and routes ggmap(visby.watercolor.map, extent="device") + geom_point( data = visby.places, aes(x = lon, y = lat), color = 'red', size = 5) + geom_text(data = visby.places, aes(label = location), hjust=0, vjust=-1) + geom_path(aes(x = lon, y = lat), colour = "blue", size = 1.5, alpha = .5, data = goto_icecream, lineend = "round") ``` ] .pull-right-50[ <img src="ggmap_Sebastian_files/figure-html/unnamed-chunk-20-1.png" style="display: block; margin: auto 0 auto auto;" /> ] ??? Finally, we use that trek and ggplot geom_path() to add the route to our plot. Now that you have a feel for it, we are ready for the exercise. --- name: report ## Session * This presentation was created in RStudio using [`remarkjs`](https://github.com/gnab/remark) framework through R package [`xaringan`](https://github.com/yihui/xaringan). * For R Markdown, see <http://rmarkdown.rstudio.com> * For R Markdown presentations, see <https://rmarkdown.rstudio.com/lesson-11.html> ```r R.version ``` ``` ## _ ## platform x86_64-apple-darwin16.7.0 ## arch x86_64 ## os darwin16.7.0 ## system x86_64, darwin16.7.0 ## status ## major 3 ## minor 5.0 ## year 2018 ## month 04 ## day 23 ## svn rev 74626 ## language R ## version.string R version 3.5.0 (2018-04-23) ## nickname Joy in Playing ``` --- name: end-slide class: end-slide # Thank you