class: center, middle, inverse, title-slide # ggmap ## RaukR, 2019. ### Sebastian DiLorenzo ### NBIS, SciLifeLab ### 13 June, 2019 --- exclude: true count: false <link href="https://fonts.googleapis.com/css?family=Roboto|Source+Sans+Pro:300,400,600|Ubuntu+Mono&subset=latin-ext" rel="stylesheet"> --- ## 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.svg" 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. Another reason is that maps are often useful for biological data, wether it is plotting where a sample is from, or migratory patterns or something else. --- ## Quick-theory: Add data to a map .center[ ```r ggmap(baylor.map) ``` <img src="ggmap_Sebastian_files/figure-html/unnamed-chunk-4-1.svg" 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, samples 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 can change between several types of maps, and how zoomed in or out you are. Now, we need a google api key for a lot of these functions. Something that we do not have! So we will use a special version of get_map() called get_stamenmap to circumvent google. --- ## The functions of ggmap .pull-left-50[ * `get_stamenmap()` + input: location + output: *ggmap* object + `source` + Stamen + `maptype` + `zoom` ] --- ## The functions of ggmap .pull-left-50[ * `get_stamenmap()` + input: location + output: *ggmap* object + `source` + Stamen + `maptype` + `zoom` * `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. Google api dependent regrettably. -- .pull-right-50[ * `trek(), route() & mapdist()` + input: *from* and *to* locations + output: multiple latitudes and longitudes + `mode = c("driving", "walking", "bicycling", "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. Route() is similar to trek() but is allowed to go more offroad whereas mapdist returns more info on distances. --- ## Example .pull-left-50.smallish[ ```r #Get the map visby.watercolor.map <- get_stamenmap( c(left = 18.265071, bottom = 57.631000, right = 18.306766, top = 57.649192), zoom = 14, maptype="watercolor") #Inspect visby.watercolor.map %>% ggmap(extent = "device") ``` ] .pull-right-50[ <img src="ggmap_Sebastian_files/figure-html/unnamed-chunk-6-1.svg" style="display: block; margin: auto 0 auto auto;" /> ] ??? Now for a quick example before the exercise. Here I have started by saving a ggmap object, visby.watercolor.map, using get_stamenmap(). 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_stamenmap( c(left = 18.265071, bottom = 57.631000, right = 18.306766, top = 57.649192), zoom = 14, maptype="watercolor") #Create a tibble of Visby's # most important locations visby.places <- tibble( location = c("Mullbärsgården, Visby", "Visby Hostel, Visby", "Glassmagasinet, Visby"), lon = c(18.29203,18.28998,18.28720), lat = c(57.64182,57.63851,57.63729)) #Inspect visby.watercolor.map %>% ggmap(extent = "device") ``` ] .pull-right-50[ <img src="ggmap_Sebastian_files/figure-html/unnamed-chunk-8-1.svg" 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.places and manually added longitude and latitude coordinates for the places. --- ## Example .pull-left-50.smallish[ ```r #Get the map visby.watercolor.map <- get_stamenmap( c(left = 18.265071, bottom = 57.631000, right = 18.306766, top = 57.649192), zoom = 14, maptype="watercolor") #Create a tibble of Visby's # most important locations visby.places <- tibble( location = c("Mullbärsgården, Visby", "Visby Hostel, Visby", "Glassmagasinet, Visby"), lon = c(18.29203,18.28998,18.28720), lat = c(57.64182,57.63851,57.63729)) #Plot important locations visby.watercolor.map %>% ggmap(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-10-1.svg" 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_stamenmap( c(left = 18.265071, bottom = 57.631000, right = 18.306766, top = 57.649192), zoom = 14, maptype="watercolor") #Create a tibble of Visby's # most important locations visby.places <- tibble( location = c("Mullbärsgården, Visby", "Visby Hostel, Visby", "Glassmagasinet, Visby"), lon = c(18.29203,18.28998,18.28720), lat = c(57.64182,57.63851,57.63729)) #Plot important locations visby.watercolor.map %>% ggmap(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-12-1.svg" 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 #Get the map visby.watercolor.map <- get_stamenmap( c(left = 18.265071, bottom = 57.631000, right = 18.306766, top = 57.649192), zoom = 14, maptype="watercolor") #Create a tibble of Visby's # most important locations visby.places <- tibble( location = c("Mullbärsgården, Visby", "Visby Hostel, Visby", "Glassmagasinet, Visby"), lon = c(18.29203,18.28998,18.28720), lat = c(57.64182,57.63851,57.63729)) #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-14-1.svg" 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-16-1.svg" 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-darwin15.6.0 ## arch x86_64 ## os darwin15.6.0 ## system x86_64, darwin15.6.0 ## status ## major 3 ## minor 6.0 ## year 2019 ## month 04 ## day 26 ## svn rev 76424 ## language R ## version.string R version 3.6.0 (2019-04-26) ## nickname Planting of a Tree ``` --- name: end-slide class: end-slide # Thank you