Google Maps and ggmap

December 22nd, 2013

The ggmap package can be used to access maps from the Google Maps API and there are a number of examples on various statistics related blogs. These include here, here and here.


Fast Tube by Casper

The ggmap package has a function get_map that can download maps from various sources including Google Maps.

require(ggmap)

The first example specifies the longitude and latitude close to the London 2012 Olympic park from Google and selects the satellite map type. The extent=”device” argument stretches the map to fill the whole graphics device.

mapImageData1 <- get_map(location = c(lon = -0.016179, lat = 51.538525),
    color = "color",
    source = "google",
    maptype = "satellite",
    zoom = 17)
 
ggmap(mapImageData1,
    extent = "device",
    ylab = "Latitude",
    xlab = "Longitude")
London 2012 Olympic Stadium Google Map Example 1

London 2012 Olympic Stadium Google Map Example 1

The second example is based on the terrain map type which looks slightly odd.

mapImageData2 <- get_map(location = c(lon = -0.016179, lat = 51.538525),
    color = "color",
    source = "google",
    maptype = "terrain",
    zoom = 16)
 
ggmap(mapImageData2,
    extent = "device",
    ylab = "Latitude",
    xlab = "Longitude")
London 2012 Olympic Stadium Google Map Example 2

London 2012 Olympic Stadium Google Map Example 2

The third example is roadmap and is uncluttered and provides an overview of the surroundings.

mapImageData3 <- get_map(location = c(lon = -0.016179, lat = 51.538525),
    color = "color",
    source = "google",
    maptype = "roadmap",
    zoom = 16)
 
ggmap(mapImageData3,
    extent = "device",
    ylab = "Latitude",
    xlab = "Longitude")
London 2012 Olympic Stadium Google Map Example 3

London 2012 Olympic Stadium Google Map Example 3

The final example is a combination of the satellite image and some road and location names.

mapImageData4 <- get_map(location = c(lon = -0.016179, lat = 51.538525),
    color = "color",
    source = "google",
    maptype = "hybrid",
    zoom = 15)
 
ggmap(mapImageData4,
    extent = "device",
    ylab = "Latitude",
    xlab = "Longitude")
London 2012 Olympic Stadium Google Map Example 4

London 2012 Olympic Stadium Google Map Example 4

Comments are closed.