How to Build Interactive Google Maps With R Shiny - A Complete Guide

Visualizing spatial data in R is oftentimes more challenging than it initially seems. There’s a ton of moving parts, like map center point, zoom, map type, marker size, and so on. And then there's cartographic design and managing aesthetic appeal along with the functional elements. It goes without saying, but you have to get all of them right to have a cohesive map. That’s where this article comes in. We’ll show you how to build interactive Google Maps with R, and how to include them in R Shiny dashboards. With Google Maps in R, you can give your users a familiar map and retain customizability by placing it within a Shiny dashboard. To follow along, you’ll have to set up a Google Cloud account. It's fairly simple, but we'll show you how.
Looking for a Google Maps alternative? Check our guide on building interactive maps with R and Leaflet.Table of contents:
- How to Set up Google Cloud Account
- Build Interactive Google Maps with R
- Add Interactive Google Maps to R Shiny Dashboards
- Conclusion
How to Set up Google Cloud Account
As the name suggests, Google Maps are developed by Google and offered as an API through their Cloud Platform. You’ll have to register an account and set up billing. Yes, you do need to put in credit card information, but they won’t charge you a dime without your knowledge. Once you have an account and a project setup, you can enable APIs required for communication between R and Google. These are:- Maps JavaScript API
- Maps Static API
- Geocoding API


Build Interactive Google Maps with R
You’ll need thegoogleway
library to follow along, so make sure to have it installed. Dataset-wise, you’ll use the US airports dataset from Kaggle:


Basic interactive Google Map with R
Let’s start with the basics. We’ll add markers to every latitude and longitude combination from the dataset. You can use thegoogle_map()
function from googleway
package to specify the API key and the dataset, and then call the add_markers()
function to add the points:

Change marker color
Thegoogleway
library is quite restrictive with marker colors. You can go with red (default), blue, green, or lavender. To make matters worse, the package requires an additional column in the dataset for the color value. It’s useful if you’re using some conditional logic to color the markers, but a complete overkill if you aren’t.

google
way to build interactive Google Maps with R.
Add mouse over
Adding mouse over effect to your interactive Google Maps with R is strongly advised if you want to make maps users will love. The mouse over effect represents what happens as the user pulls the mouse cursor over a marker. The code snippet below shows you how to add an airport name, stored in theAIRPORT
column:

Add info window
Unlike mouse overs, info windows will activate only if the user clicks on a marker in your interactive map. Keep in mind — the info window also has to be closed manually by the user. To spice things up, we’ll add an additional column that contains the airport name, city, and state. You can add and customize yours easily with thepaste0()
function.

googleway
. Next, you’ll see how to embed them to an R Shiny dashboard.
Add Interactive Google Maps to R Shiny Dashboards
You’ll now see how easy it is to add interactive Google Maps to R Shiny dashboards. The one you’ll see below allows the user to specify a list of states from which the airports are displayed. Keep in mind:- App UI — You can use the
google_mapOutput()
function as a placeholder for the interactive Google map. - App Server — Use the
renderGoogle_map()
function to display the map. The data is prepared earlier as a reactive component to match the user-selected states and display the airport, city, and state info on mouse over.

googleway
library configures the location and the zoom level automatically for you. The map was zoomed to New York by default and changed instantly when we added more states. Overall, we have a decent-looking map for 30-something lines of code.
Learn how to use R to analyze large geolocation databases and build a Corona Risk heat map - CoronaRank.The questions left to address are the speed and the looks. Luckily, we have dedicated articles on these topics, written with having R Shiny dashboards in mind: These two are excellent reads if you’ve enjoyed this article, as they allow you to take your R Shiny dashboards to the next level.
Conclusion
And there you have it — How to build, tweak, and style interactive Google Maps with R. You’ve learned a lot today, but there’s always room to improve. Here are a couple of challenges you can do next:- Use custom icons instead of the default markers
- Show airports as a heat map instead of markers
- Calculate distances between airports using the Google Maps API