June 2, 2015

Plotting maps from Google

I did’nt resist the temptation of trying the script detailed in this excellent tutorial on using ggmap, another groundbreaking package by Hadley Wickham (http://had.co.nz/):

http://rpubs.com/monicagutierrez/mapasgoogle

So, here my exercise, which is dedicated to Venice, a town I love.

# first load the necessary packages

library(knitr)
library(rmarkdown)
## Warning: package 'rmarkdown' was built under R version 3.1.3
library(ggmap)
## Warning: package 'ggmap' was built under R version 3.1.3
## Loading required package: ggplot2
# Now we will assign an address that Google can read

locus="Basilica di San Marco, Venezia, Italy"

# You may also use the coordinates (latitude and longitude), if you have them
# locus=c(lat = 45.4344700786,lon = 12.3394828851)
# Now we require the information from Google, by also zooming on the map


myMap <- get_map(location=locus, zoom= 18, source="google", 
                 maptype="roadmap", crop=TRUE)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=Basilica+di+San+Marco,+Venezia,+Italy&zoom=18&size=640x640&scale=2&maptype=roadmap&language=en-EN&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Basilica+di+San+Marco,+Venezia,+Italy&sensor=false
# And now, the map, in the standard road format

ggmap(myMap)

plot of chunk unnamed-chunk-4

# Now we want a view from the satellite

myMap <- get_map(location=locus, zoom= 18, 
                 source="google", maptype="satellite", crop=TRUE)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=Basilica+di+San+Marco,+Venezia,+Italy&zoom=18&size=640x640&scale=2&maptype=satellite&language=en-EN&sensor=false
ggmap(myMap)

plot of chunk unnamed-chunk-5

# OK... Now, more zoom

myMap <- get_map(location=locus, zoom= 19, 
                 source="google", maptype="hybrid", crop=TRUE)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=Basilica+di+San+Marco,+Venezia,+Italy&zoom=19&size=640x640&scale=2&maptype=hybrid&language=en-EN&sensor=false
ggmap(myMap)

plot of chunk unnamed-chunk-6

# Now, something old school, a vintage watercolor map!

myMap <- get_map(location=locus, zoom= 15, 
                 source="stamen", maptype="watercolor", crop=TRUE)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=Basilica+di+San+Marco,+Venezia,+Italy&zoom=15&size=640x640&maptype=terrain&sensor=false
## Map from URL : http://tile.stamen.com/watercolor/15/17505/11730.jpg
## Map from URL : http://tile.stamen.com/watercolor/15/17506/11730.jpg
## Map from URL : http://tile.stamen.com/watercolor/15/17507/11730.jpg
## Map from URL : http://tile.stamen.com/watercolor/15/17508/11730.jpg
## Map from URL : http://tile.stamen.com/watercolor/15/17505/11731.jpg
## Map from URL : http://tile.stamen.com/watercolor/15/17506/11731.jpg
## Map from URL : http://tile.stamen.com/watercolor/15/17507/11731.jpg
## Map from URL : http://tile.stamen.com/watercolor/15/17508/11731.jpg
## Map from URL : http://tile.stamen.com/watercolor/15/17505/11732.jpg
## Map from URL : http://tile.stamen.com/watercolor/15/17506/11732.jpg
## Map from URL : http://tile.stamen.com/watercolor/15/17507/11732.jpg
## Map from URL : http://tile.stamen.com/watercolor/15/17508/11732.jpg
ggmap(myMap)

plot of chunk unnamed-chunk-7

# Now we finish with a black and white classic map...



myMap <- get_map(location=locus, zoom= 15, 
                 source="stamen", maptype="toner", crop=TRUE)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=Basilica+di+San+Marco,+Venezia,+Italy&zoom=15&size=640x640&maptype=terrain&sensor=false
## Map from URL : http://tile.stamen.com/toner/15/17505/11730.png
## Map from URL : http://tile.stamen.com/toner/15/17506/11730.png
## Map from URL : http://tile.stamen.com/toner/15/17507/11730.png
## Map from URL : http://tile.stamen.com/toner/15/17508/11730.png
## Map from URL : http://tile.stamen.com/toner/15/17505/11731.png
## Map from URL : http://tile.stamen.com/toner/15/17506/11731.png
## Map from URL : http://tile.stamen.com/toner/15/17507/11731.png
## Map from URL : http://tile.stamen.com/toner/15/17508/11731.png
## Map from URL : http://tile.stamen.com/toner/15/17505/11732.png
## Map from URL : http://tile.stamen.com/toner/15/17506/11732.png
## Map from URL : http://tile.stamen.com/toner/15/17507/11732.png
## Map from URL : http://tile.stamen.com/toner/15/17508/11732.png
ggmap(myMap)

plot of chunk unnamed-chunk-8

I hope you’ve enjoyed this!

Twitter: @AntoViral

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.