Introduction to ‘ggmap()’

This code through will explore a common, yet powerful, way of visualizing geospatial data through a package called ‘ggmap’. This is a preferred and familiar form of map data because it uses the most popular mapping service already available to us: Google Maps. Even better - it has the ability to be overlaid with the familiar plotting frameworks of ‘ggplot2’(Khale, Wickham).

Content Overview

By the end of this code through, you should have a basic understanding of how to install/setup ggmap and begin overlaying geospatial data onto maps to create compelling visualizations.

Learning Objectives

The following learning objectives will be covered:

How to install and set up ggmap and the Google Maps API.
Popular functions and their uses within ggmap.
How to customize your geodata and overlay it onto the maps.
Sneak-peak into more advanced map visualization techniques.

Install / API

Before we can start, it’s important to get a personal API key from Google Maps.

An API key is a unique code that allows your project to make calls for data from Google Maps. It is important that Google grants API keys to individuals to avoid attacks on their servers and charge those who use the service extensively. In our case, it is free to sign-up, and Google offers a trial which comps your first $200 of credit per month.

Click here for a website that offers a great guide on how to sign up for free (Juliver).


For the sake of this code-through, I will not be sharing my exact API, but you can refer to its usage in the code as ‘your_secret_api’.

Getting Started

First, you will install and load the ‘ggmap’ package, then register your newly acquired API with Google Maps. From there, you can call any location using the get_map() function. We’ll set the zoom to 12.

#Load library
library(ggmap)

# Set your Google API key
# Syntax:   register_google(key = "your_secret_api")


Sneak Peak: Blending it all with ggplot2

Finally, this example demonstrates how to combine ggmap’s mapping capabilities with ggplot2’s customizability to create a detailed map. We’ll plot the location of a specific place — for example, Ooh Ahh Point, a popular stopping point on the Grand Canyon I visited last summer — and integrate it with ggplot2.

library(ggmap)
library(ggplot2)

# Get map data from Google Maps for the Grand Canyon area, on Ooh Aah Point
map_data <- get_map(location = 'Grand Canyon, Arizona', zoom = 12, source = "google", maptype = 'terrain')

# Create a base map using ggmap
base_map <- ggmap(map_data)

# Overlay with ggplot2: add a point for Ooh Aah Point
# Latitude and longitude for Ooh Aah Point
ooh_aah_point_lat <- 36.0594
ooh_aah_point_lon <- -112.1091

# Add the point with a custom layer
final_map <- base_map + 
  geom_point(aes(x = ooh_aah_point_lon, y = ooh_aah_point_lat), color = "blue", size = 5) +
  geom_text(aes(x = ooh_aah_point_lon, y = ooh_aah_point_lat, label = "Ooh Aah Point"), 
            vjust = -1, color = "blue", size = 3) +
  ggtitle("Grand Canyon, Arizona: Ooh Aah Point") +
  theme_minimal()

# Display the map
print(final_map)

Further Resources

Now the fun begins! You can learn more about ggmap and its install on CRAN here.


More Resources

CRAN PDF

What is ggmap?

A Guide to Using ggmap in R

Map Platorm - Google Maps for Developers



Works Cited

This code through references and cites the following sources:


D. Kahle and H. Wickham. ggmap: Spatial Visualization with ggplot2. The R Journal, 5(1), 144-161. URL https://journal.r-project.org/archive/2013-1/kahle-wickham.pdf. 2013.

H. Wickham. ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York, 2016. Google Maps Platform Documentation. URL https://developers.google.com/maps/documentation.

R Core Team. R: A language and environment for statistical computing. R Foundation for Statistical Computing, Vienna, Austria. URL https://www.R-project.org/.

J. Juliver. Google Maps API: How to Get Started URL https://blog.hubspot.com/website/google-maps-api#:~:text=Without%20API%20keys%2C%20it%20would,first%209 0%20days%20of%20use.

I. Bernado A Guide to Using ggmap in R URL https://builtin.com/data-science/ggmap