Reproduce the Figures

Every figure in Make It Visible was drawn in R with ggplot2 from a public dataset. The pages in this part contain the code, one page per chapter, in the order the chapter draws the figures. Each page begins with a chunk that sets where the data and the theme load from. The chapter’s setup chunk comes next, then one chunk per figure.

Running the Code

Install R and the packages below, open a new script, and paste in the first two chunks of any page. After that, every figure chunk on the page runs on its own.

Code
install.packages(c("tidyverse", "ggrepel", "patchwork", "scales", "ggridges",
                   "treemapify", "sf", "rnaturalearth", "rnaturalearthdata",
                   "plotly", "datasauRus", "HistData", "ragg",
                   "ggalluvial", "igraph", "tidygraph", "ggraph", "graphlayouts"))

The data load from the companion site at viz.marginoferrormedia.com/data, and the theme from viz.marginoferrormedia.com/figures/mv-theme.R. To work offline, download the data folder and the theme file from the repository and set two environment variables before you source anything.

Code
Sys.setenv(MV_DATA = "path/to/data", MV_THEME = "path/to/mv-theme.R")

The Theme

mv-theme.R contains everything the figures share. theme_mv() sets the canvas, the type, and the grid. mv_set_chapter(n) picks the chapter’s accent color, and mv_accent() returns it. mv_source() builds the caption so that every figure names its source. mv_label_end() places direct labels at the end of lines with ggrepel. Reading the file shows how a house style for charts is built. Chapter 11 of this companion explains the ggplot2 it uses.

Changing a Figure

The code is meant to be changed. Change the country, the year, or the variable, and the chart redraws. Three things keep the result accurate. Keep the source line. If you cut the series or the sample, say so in the caption, as the originals do. If the change alters what the chart shows, change the title to match.