Eye-catching animated maps in R — a simple introduction
By: Stefan Haring
Tags: Visualization, Maps, Charts, R, Shiny

Eye-catching animated maps in R — a simple introduction

Photo by Timo Wielink on Unsplash

Every day, we as data scientists and data analysts have to work with different kinds of data. And we all know that visualization of the data and our findings is key, especially when presenting it to co-workers or clients. After all, it is far easier to tell a story with a chart than it is with plain numbers or text. When you’re presenting data via a dashboard, not only static visualization becomes important, you will also want to display changes in your data dynamically.

In this post, I will demonstrate how you can easily animate charts based on geospatial data using the leaflet and shiny libraries in R.

1. Introduction

While there are many ways to draw maps in R (e.g. with ggplot2 or tmap), the leaflet library is my personal favourite due to the following reasons:

simplicity

high degree of customization

beautiful, interactive maps

works on all major desktop and mobile platforms

The leaflet R library is built on leaflet.js, which is the leading open-source JavaScript library for mobile-friendly interactive maps. The R implementation lets us produce simple maps in just a few lines. For example, with just one line of code you can display the following map, zooming in on beautiful Vienna, Austria:

Basic Leaflet Map (Image by Author)

Even though it was quite simple to create this map, it doesn’t show any data yet. Eventually what we would like is something like this:

Basic Leaflet Map with data (Image by author)

We want to overlay our map with data (like in the example above we’ve added some circle markers) or even colour our map according to some underlying data (a so called Choropleth). And finally, we want to be able to animate changes in the data over time to include our map in a beautiful dashboard.

Basic Animation (Image by author)

In the following sections I will briefly demonstrate how to produce an animation similar to the one above and also how to create an animated Choropleth.

2. Data

To get started, we require two types of data: we need to display our desired map and the data that we are interested in displaying.

For the map data, we have two main options with leaflet:

tiles

geospatial data in the form of either GeoJSON or TopJSON files

We’ve already seen how to use the basic tiles in the above charts, simply using the addTiles() function which defaults to OpenStreetMap Tiles. We could also be using other third-party tile sets using the addProviderTiles() function. For a full list of available tiles, see here.

We can also read geospatial data from GeoJSON or TopJSON files and convert them into spatial objects in R. To create our Choropleth we will have to make use of a function called addPolygons() which requires us to use such spatial data. While this whole process sounds complicated and intimidating at first, it is actually pretty straightforward.

For this article, we will focus on displaying data on country level. We can easily get the required spatial data from www.thematicmapping.org which provides the “World Borders Dataset”. The following code snippet shows how to download, unzip and load this data all from R. Obviously you only have to download the dataset once.

The resulting spatial polygons data frame contains information about all countries, e.g. country code, name, longitude and latitude coordinates. The basic data included in the object can be accessed as follows:

World Borders Dataset Overview (Image by author)

Important for drawing a Choropleth is the polygon data contained in the object. This is essentially the data used to draw the country borders.

Polygon data (Image by author)

This is all the data we need in terms to create the basic map that we are going to be using.