Geocoding with ggmap and the Google API
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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()
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.
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.