Bug when Creating Reference Maps with Choroplethr

Last week the Census Bureau published a free course I created on using Choroplethr to map US Census Data. Unfortunately, a few people have reported problems when following one of the examples in the course. This post describes that issues and provides instructions for working around it.

Where does the bug occur?

The bug occurs in Module 2 Lesson 4, when creating this map:

The code I provide to create that map can be seen here:

state_choropleth(df_pop_state,
    num_colors = 1,
    zoom = c("california", "oregon", "washington"),
    reference_map = TRUE)

Note that this map combines a standard choropleth (a map that uses color to indicate state population) with a reference map from Google maps (a map that shows terrain, large cities, and so on).

What’s the problem?

After I completed production of the course, google added a requirement that you must register an API key before getting their maps. Since Choroplethr requests reference maps without an API key, Google now refuses the request.

Behind the scenes, Choroplethr uses the ggmap package to request the maps. Here is how David Kahle, the author of ggmap, describes the issue in the help file for ggmap’s register_google function:

As of mid-2018, the Google Maps Platform requires a registered API key. While this alleviates previous burdens (e.g. query limits), it creates some challenges as well. The most immediate challenge for most R users is that ggmap functions that use Google’s services no longer function out of the box, since the user has to setup an account with Google, enable the relevant APIs, and then tell R about the user’s setup.

The help file goes into more detail, and I encourage you to read it.

Workaround

Getting the above example to work is a three step process.

1. Get an API key from Google by visiting the Google Maps Platform. This is free, although they require that you enter a credit card.

2. Register your key with the ggmap package by typing the following:

library(ggmap)
register_google("<your api key>")

3. Install the mapproj package by typing the following

install.packages("mapproj")

Future Work

While the above steps solve the problem, I do not consider the fix to be particularly elegant. I am currently exploring other solutions, and will create a blog post if I find a better solution.