8.3 Animated maps

Faceted maps, described in Section 8.2.6, can show how spatial distributions of variables change (e.g., over time), but the approach has disadvantages.Facets become tiny when there are many of them.Furthermore, the fact that each facet is physically separated on the screen or page means that subtle differences between facets can be hard to detect.

Animated maps solve these issues.Although they depend on digital publication, this is becoming less of an issue as more and more content moves online.Animated maps can still enhance paper reports: you can always link readers to a web-page containing an animated (or interactive) version of a printed map to help make it come alive.There are several ways to generate animations in R, including with animation packages such as gganimate, which builds on ggplot2 (see Section 8.6).This section focusses on creating animated maps with tmap because its syntax will be familiar from previous sections and the flexibility of the approach.

Figure 8.16 is a simple example of an animated map.Unlike the faceted plot, it does not squeeze multiple maps into a single screen and allows the reader to see how the spatial distribution of the world’s most populous agglomerations evolve over time (see the book’s website for the animated version).

Animated map showing the top 30 largest urban agglomerations from 1950 to 2030 based on population projects by the United Nations. Animated version available online at: geocompr.robinlovelace.net.
Figure 8.16: Animated map showing the top 30 largest urban agglomerations from 1950 to 2030 based on population projects by the United Nations. Animated version available online at: geocompr.robinlovelace.net.

The animated map illustrated in Figure 8.16 can be created using the same tmap techniques that generate faceted maps, demonstrated in Section 8.2.6.There are two differences, however, related to arguments in tm_facets():

  • along = "year" is used instead of by = "year".
  • free.coords = FALSE, which maintains the map extent for each map iteration.
    These additional arguments are demonstrated in the subsequent code chunk:
  1. urb_anim = tm_shape(world) + tm_polygons() +
  2. tm_shape(urban_agglomerations) + tm_dots(size = "population_millions") +
  3. tm_facets(along = "year", free.coords = FALSE)

The resulting urb_anim represents a set of separate maps for each year.The final stage is to combine them and save the result as a .gif file with tmap_animation().The following command creates the animation illustrated in Figure 8.16, with a few elements missing, that we will add in during the exercises:

  1. tmap_animation(urb_anim, filename = "urb_anim.gif", delay = 25)

Another illustration of the power of animated maps is provided in Figure 8.17.This shows the development of states in the United States, which first formed in the east and then incrementally to the west and finally into the interior.Code to reproduce this map can be found in the script 08-usboundaries.R.

Animated map showing population growth, state formation and boundary changes in the United States, 1790-2010. Animated version available online at geocompr.robinlovelace.net.
Figure 8.17: Animated map showing population growth, state formation and boundary changes in the United States, 1790-2010. Animated version available online at geocompr.robinlovelace.net.