Geocoding with ggmap and the Google API

October 9, 2018
By

(This article was first published on The Devil is in the Data – The Lucid Manager, and kindly contributed to R-bloggers)

Some of the most popular articles on the Devil is in the Data show how to visualise spatial data creatively. In the old days, obtaining latitude and longitude required a physical survey, with Google maps, this has become a lot easier.

The geocode function from the ggmap package extracts longitude and latitude from Google maps, based on a location query. The example below shows the result of a simple geocoding call for the White House and Uluru. The geocode function essentially constructs a URL to obtain the data.

library(ggmap)
geocode(c("White House", "Uluru"))

Source : https://maps.googleapis.com/maps/api/geocode/json?address=White%20House
Source : https://maps.googleapis.com/maps/api/geocode/json?address=Uluru
        lon       lat
1 -77.03653  38.89768
2 131.03688 -25.34443

This function used to work fine, except for the occasional drop-out, which could be easily circumvented through repeated calls. Google recently tightened access to the database, which means you need to register an API for it to work. This article explains how to use the latest version of ggmap and a Google account to continue using this function.

The Google API

Before we can start geocoding again, we need to obtain an API key from Google. Go to the registration page, and follow the instructions (select all mapping options). The geocoding API is a free service, but you nevertheless need to associate a credit card with the account.

Geocoding with ggmap

The existing CRAN version of ggmap is 2.6.1. This version does unfortunately not yet enable using the Google API. The current version on GitHub is further advanced and fixes the issue. To install the GitHub version, you need to use the devtools package.

devtools::install_github("dkahle/ggmap")

The geocode function converts the request into a URL and captures the output into a data frame. The code snippet below shows a minimum-working-example of how you can map coordinates using ggplot. I have stored the key itself in a private text file. The plot shows the places I have lived.

library(tidyverse)
locations <- c("Hoensbroek", "Johannesburg", "Barrow-in-Furness",
               "Hong Kong", "Singapore", "Tangail", "Maastricht", "Bendigo") %>%
    geocode()
world <- map_data("world")
ggplot() +
    geom_polygon(data = world,  aes(long, lat, group = group), fill = "grey") +
    geom_point(data = locations, aes(lon, lat), colour = "red", size = 5) + 
    coord_map("ortho", orientation = c(30, 80, 0)) +
    theme_void()

Geocoding with ggmap and the Google API

Map Porn

The articles on this blog that rely on the geocode function are categorised as Map Porn because they mostly discuss having fun with maps in R. All code of these articles has been amended to function with the new method.

To leave a comment for the author, please follow the link and comment on their blog: The Devil is in the Data – The Lucid Manager.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, 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.

Search R-bloggers


Sponsors

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)