Google Maps and ggmap

[This article was first published on Software for Exploratory Data Analysis and Statistical Modelling » R Environment, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 their blog: Software for Exploratory Data Analysis and Statistical Modelling » R Environment.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)