Using Google Maps in R: part two

Google Maps train stations in the Netherlands

Using Google Maps in R: part two! In this blog series about using Google Maps in R, I will guide you through examples on how to use the Google Maps API.

In part one I showed you examples using the package mapsapi. This second part is about exploring the package ggmap.

Getting your API key(s)

Whether you’re working with mapsapi or ggmap, you will need API key(s). In the previous blogpost I explained a little about how to enable your API key(s) on Google Cloud Platform (GCP). So if you don’t know how to get the keys, head over to the first part.

Once you have those precious keys, you’re ready to go!

Visualizing train stations with ggmap

In the Netherlands, a lot of people commute to work by train. To facilitate traveling by train, there are a lot of stations throughout the Netherlands. But where are they located exactly?

To figure this out, I downloaded a dataset containing all train stations in the Netherlands (source: “Rijden de Treinen”). We’re going to use this dataset to plot all the train station on a map from the Netherlands.

To get a static map from a country, place, or region, you need to enable the Maps Static API on GCP.

Let’s see how it works:

# Libraries
library(ggmap)
library(dplyr)

# Your private key
key = "XXX"

# This sets your google maps key for this session
register_google(key = key)

# Get a static map
map <- get_googlemap("the netherlands", zoom = 7)

# Make a plot to display stations in the Netherlands
data_stations <- read.csv("data/stations-2020-01-nl.csv")

ggmap(map) +
  geom_point(data = data_stations, 
             aes(x = geo_lng, y = geo_lat, color = type)) +
  ggtitle("Stations in the Netherlands") +
  theme_void()

The function get_googlemap gives you the static map. You can play around with the zoom argument to get closer or further away from you target. One annoying thing I found out was that zoom only takes integers. The Netherlands is a small country. A zoom setting of 7 is a bit too wide, but a setting of 8 chops of parts of the country. The struggle…

The code above results in the following plot:

Google Maps train stations in the Netherlands

That’s easy!

Since ggmap converts the static map to a ggplot object, you can easily add elements. The only thing you need are longitudes and latitudes.

Visualizing addresses with ggmap

It might be the case that you don’t have longitudes or latitudes in your dataset. You may have places of interest like “Tilburg University”, or you may have addresses like “Dorpsstraat, Oegstgeest”. How do you convert those to longitudes and latitudes?

In the following example I took a dataset containing the ten biggest Albert Heijn supermarkets in the Netherlands (source: Distrifood).

The original dataset only contains the street name and the place name, but no longitudes and latitudes. Luckily there are two functions that can help you out: geocode and mutate_geocode. The first one returns a tibble. The second one can be easily integrated in a pipe to add the longitudes and latitudes to your dataframe.

In the example below I show you how:

# Get a static map
map <- get_googlemap("tilburg, the netherlands", zoom = 8)

# Load the data
data_supermarkets <- read.csv("data/albertheijn-top10.csv", sep = ";",
                              stringsAsFactors = FALSE)

# Addresses have to be converted to a geolocation
data_supermarkets <- data_supermarkets %>%
  mutate_geocode(Straat_volledig) %>%
  mutate(vvo = as.numeric(vvo))

# Make the plot
ggmap(map) +
  geom_point(data = data_supermarkets, 
             aes(x = lon, y = lat, size = vvo),
             colour = "navy")  +
  ggtitle("Top 10 biggest Albert Heijn supermarkets") +
  scale_size_continuous(name = "Sales surface area (m2)",
                        range = c(3, 7)) +
  theme_void()

Since the top part of the Netherlands does not have a giant supermarket, I went for a different “center”. Tilburg lies more South in the Netherlands. 

The plot we get from this code is:

Not sure if Google Maps can find the location you’re looking for? You can always try to search for the location on Google Maps itself. For example, a search for a street called “Kalverstraat” results in the closest “Kalverstraat” to my location (Amsterdam). But I might be looking for the “Kalverstraat” in another city. Therefore, my search needs to be: “Kalverstraat, Harmelen” if I want to get a hit on a street called “Kalverstraat” in, guess what, Harmelen.

The more specific you are, the more likely it will be that the intended location is returned.

Wrap-up

In this blog I showed you two working examples with ggmap. It’s easy to use and works great together with ggplot. 

The only, perhaps, difficult thing about using the Google Maps API in R is that you need to look carefully at which APIs you actually need. Different functions from different packages require access to different APIs. As soon as you figured that out, you can basically map everything you want.

Good luck, and happy mapping!

PS: the data and the code are available on GitHub.

Geef een reactie

Het e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *