Using ggmap after July 2018

By Martin John Hadley | 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 (because they’re very good).

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 use account, this is now no longer possible. See the pricing page for explicit details from Google themselves.

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. I’ve summarised the pricing policy as of December 2018 below:

Let’s make our maps

We’ll break this into two stages:

  1. Preparing our Google Maps Platform account

  2. Setting up and using ggmap

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, leave the other things for another tutorial.

  1. Create a new project for tracking your ggmap-incurred expenses, I suggest using the name ggmap

  1. You’ll be prompted to create a billing account, click “create a billing account”

  2. Select your country, agree to the terms and conditions, and make a decision about marketing. Please note you’ll likely be told you’re getting $300 per month rather than $200 for 12 months, it’s because Google are trying to keep devs happy during the roll out.

  1. Fill in your payment details!

  2. As of 2018 there’s a modal dialog that you want to click through, I assume this will be designed away in the future so don’t worry if you don’t see it.

  1. Success! Now you have an API key to use in R. You’ll want to copy it down somewhere.

You need to have your API key with you EVERY time that you use ggmap for the time being. Make sure you keep a note of it from above, or else you can find it with these steps:

  1. Navigate to https://console.cloud.google.com/apis/dashboard and select the project (I went with ggmap) you created from the pull down list:

  1. Select credentials from the left-hand nav and you’ll be shown your API key

Setting up and using ggmap

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

  1. There’s a dev version of the ggmap package that uses the new APIs, which we can install as follows:
devtools::install_github("dkahle/ggmap", ref = "tidyup")
  1. You must now restart R.

  2. Load the library and provide your API key

register_google(key = "My-FAKE-KEY")
  1. Now we can get our base maps using get_googlemap()
base_map <- get_googlemap(center = c(2.2945, 48.858222), maptype = "roadmap")
ggmap(base_map)

comments powered by Disqus