A smooth transition between choropleth and cartogram



This post describes how to make a smooth transition GIF between a choropleth map and a cartogram. It starts by doing a basic map of Africa and then distorts country size using the cartogram library. ggplot2 is used to build a good looking choropleth map. Animation is made possible thanks to the tweenR and gganimate packages.

Cartogram section Data to Viz

Goal and Packages


At the end of this tutorial, you should get a gif file containing the following animation.

Before we start, make sure you’ve got the following libraries:

A basic map of Africa


The maptools library provides all the information we need to draw a map of Africa.

All the country boundaries are stored in the world_simpl object. Let’s load this object, keep only Africa, and draw a basic representation. This requires only 3 lines of code.

Compute cartogram boundaries


The afr object is a spatial object. Thus it has a data slot that gives a few information concerning each region. You can visualise this info typing afr@data in our case.

You will see a column called POP2005, providing the number of inhabitants per country in 2005.

Using this information we can use the cartogram library to build… a cartogram! Basically, it will distort the shape of every country proportionally to its number of inhabitants.

The output is a new geospatial object that we can map like we’ve done before. As you can see, Nigeria appears way bigger on this map, since it has a population of about 141M inhabitants.

A nicer representation using ggplot2


Let’s improve the appearance of the previous maps using the ggplot2 library.

Note that ggplot2 uses data frame and not geospatial object. The transformation to a data frame is done using the tidy() function of the broom package. Since it does not transfer the data slot automatically, we merge it afterward.

The geom_polygon() function is used to draw map data. See the graph #327 of the gallery for more explanation on choropleth maps with ggplot2.

Compute several intermediate maps


The goal being to make a smooth animation between the 2 maps, we need to create a multitude of intermediate maps using interpolation.

This is possible thanks to the awesome tweenr library. (See a few examples in the animation section of the gallery).

At the end we’ve got a big data frame which contains enough information to draw 30 maps. Three of these maps are presented above.

Make the animation with gganimate


The last step consists at building the 30 maps and compile them in a .gif file. This is done using the gganimate library. This library uses another aesthetic: frame. A new plot is made for each frame, that allows us to build the gif afterwards.

Note: This code uses the old version of gganimate. It needs to be updated. Please drop me a message if you can help me with that!

Done! You should have the gif in your working directory.

Conclusion


This post uses several concepts that are extensively described in the R graph gallery:

If you are interested in dataviz, feel free to visit the gallery, or to follow me on twitter!

Related chart types


Map
Choropleth
Hexbin map
Cartogram
Connection
Bubble map



Contact

This document is a work by Yan Holtz. Any feedback is highly encouraged. You can fill an issue on Github, drop me a message on Twitter, or send an email pasting yan.holtz.data with gmail.com.

Github Twitter