Creating a Seamless Shift from Choropleth Maps to Cartograms

Ambuj Shukla
R-evolution
Published in
6 min readSep 16, 2023

--

In this blog post, we’ll walk through the process of generating a seamless transition GIF that smoothly transforms a choropleth map into a cartogram. We begin by creating a foundational map of Africa and proceed to adjust the sizes of countries using the cartogram library. To enhance the visual appeal of our choropleth map, we employ the versatile ggplot2. The magic of animation is achieved through the utilization of the tweenR and gganimate packages.

Goal and Packages

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

Final Piece

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

# Please Ignore, specific to a bug in the gallery
library(pacman)
pacman::p_unload(pacman::p_loaded(), character.only = TRUE)

# Load libraries
library(dplyr) # data wrangling
library(cartogram) # for the cartogram
library(ggplot2) # to realize the plots
library(broom) # from geospatial format to data frame
library(tweenr) # to create transition dataframe between 2 states
library(gganimate) # To realize the animation
library(maptools) # world boundaries coordinates
library(viridis) # for a nice color palette

A basic map of Africa

--

--