Google Maps and ggmap

December 22, 2013
By

(This article was first published on Software for Exploratory Data Analysis and Statistical Modelling » R Environment, and kindly contributed to R-bloggers)

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.

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

To leave a comment for the author, please follow the link and comment on his blog: Software for Exploratory Data Analysis and Statistical Modelling » R Environment.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.