Using ggmap after July 2018

ggmap is an awesome package for creating maps with ggplot2. If you’re reading instructions written before July 2018 you will fail to replicate the results — here’s what changed and how to fix it.
R
dataviz
GIS
Published

December 5, 2018

ggmap is an awesome package for creating maps with ggplot2. If you’ve seen a nice looking map built with ggplot2 I can almost guarantee you that it uses ggmap to look that good. The ggmap package is used to obtain base maps from the APIs of a number of online mapping tools, the one we care about here is the base maps from Google Maps.

If you’re reading instructions for using ggmap written before July 2018 you will fail to replicate the results unless you make some changes to your workflow. You will need to give Google your billing information, but can create maps for free.

What’s changed?

Before July 2018 it was possible to use the Google Maps API for free without configuration or a user account; this is now no longer possible. See the pricing page for explicit details from Google.

You must do the following to obtain base maps from Google:

  • Have a Google Cloud account.
  • Enable the Google Maps Platform.
  • Provide Google with your billing details.

You will then be allocated $200 of free usage per month, every month. All API calls will be made against these limits.

If you’re exclusively making static maps with ggmap then you’re extremely unlikely to ever be charged by Google. The pricing as of December 2018:

Number of times you run get_googlemap Price per function call
0–100,000 $0.002 each ($2 per 1,000)
100,001–500,000 $0.0016 each ($1.60 per 1,000)

Let’s make our maps

Preparing our Google Maps Platform account

  1. Navigate to https://cloud.google.com/maps-platform/ and click “Get Started”
  2. Select “Maps” in the dialog.
  3. Create a new project called ggmap.
  4. Provide your billing details — you’ll be allocated $200/month of free usage.
  5. Copy your API key somewhere safe.

You can always retrieve your API key later at https://console.cloud.google.com/apis/dashboard.

Setting up and using ggmap

The instructions for these steps can be found in Github Issue #51.

  1. Install the dev version of ggmap that uses the new APIs:
devtools::install_github("dkahle/ggmap", ref = "tidyup")
  1. Restart R.

  2. Load the library and provide your API key:

library("ggmap")
register_google(key = "My-FAKE-KEY")
  1. Get your base maps using get_googlemap():
base_map <- get_googlemap(center = c(2.2945, 48.858222), maptype = "roadmap")
ggmap(base_map)