Reproduce The Economist-styled charts

Importing and checking data

This work is to reproduce a chart following The Economist style. You can see the original chart at its Website.

First, we create a data set that includes the number of laboratory-acquired infections and the virus types.

# Clear R environment 

rm(list = ls())

# Load some R packages

library(readxl)                                      
library(dplyr)
library(ggplot2)

# Generate the dataset

virus_names <- c("Hantavirus", "Tularemia", "Dengue", "Ebola", "E. coli", 
                 "Tuberculosis", "Salmonella", "Vaccinia", "Brucella")

my_number <- c(6, 7, 7, 9, 11, 15, 17, 18, 54)

data.frame(virus = virus_names, n = my_number) -> data

# Check the data

str(data)
'data.frame':   9 obs. of  2 variables:
 $ virus: chr  "Hantavirus" "Tularemia" "Dengue" "Ebola" ...
 $ n    : num  6 7 7 9 11 15 17 18 54

We can see that the column virus is character class. We should convert it into factor class as following

# Convert the class

data %>% 
  arrange(n) %>%
  mutate(virus = factor(virus, levels = virus)) -> data

# Recheck the data

str(data)
'data.frame':   9 obs. of  2 variables:
 $ virus: Factor w/ 9 levels "Hantavirus","Tularemia",..: 1 2 3 4 5 6 7 8 9
 $ n    : num  6 7 7 9 11 15 17 18 54

Drawing the basic chart

Now we can draw a very basic chart as following

# Draw the first chart

data %>% 
  ggplot(aes(y = virus, x = n)) +
  geom_col() -> p1

# Show the first chart

p1

Preparing colors and fonts

The next step is to prepare for colors and fonts. We download the chart at The Economist and use the image to find out color codes through the website imagecolorpicker.com. We use the font Roboto Slab that is similar to The Economist font.

# Colors

bar_color <- c('#066f9f')
red_icon <- c('#ee1c25')
grid_line_color <- c('#d9dfe2')

# Font

library(showtext)

my_font <- "Roboto Slab"

font_add_google(name = my_font, family = my_font)

showtext_auto()

Completing the chart

We draw the second chart by a little long lines of following codes.

# Codes for the second chart

p2 <- p1 +
  labs(title = "Escape artists",
       subtitle = "Numer of laboratory-acquired infections, 1970-2021",
       caption = "Sources: Laboratory-Acquired Infection Database; American Biological Safety Association\nThe Economist") +
  geom_col(fill = bar_color) +
  theme(panel.background = element_rect(fill = "white")) +
  theme(axis.title = element_blank()) + 
  theme(axis.ticks = element_blank()) +     
  scale_x_continuous(breaks = seq(0, 55, 5), limits = c(0, 55), position = "top") + 
  theme(axis.text.y = element_blank()) +  
  theme(panel.grid.major.x = element_line(color = grid_line_color)) +   
  theme(plot.title = element_text(size = 20, face = "bold", color = "black", family = my_font)) + 
  theme(plot.subtitle = element_text(size = 15, color = "black", family = my_font)) + 
  theme(plot.caption = element_text(size = 10, color = "grey30", family = my_font, hjust = 0, lineheight = 2)) 

# Show the chart

p2

Now we install the library shadowtext to draw and adjust the virus names on the Y columns.

# Install the library

library(shadowtext)

# Draw the third chart
p3 <- p2 +
  geom_shadowtext( data = subset(data, n < 8), aes(n, y = virus, label = virus),
    hjust = 0, nudge_x = 0.3, colour = bar_color, bg.colour = "white", bg.r = 0.2,
    family = my_font, size = 2) +
  geom_text(data = subset(data, n >= 8), aes(0, y = virus, label = virus), hjust = 0,
    nudge_x = 0.3, colour = "white", family = my_font,
    size = 2)

# Show the third chart

p3

At the last step we install library grid to add the red tags on the chart.

# Install the library

library(grid)

# Make space for red tags

p4 <- p3 + 
  theme(plot.margin = margin(0.08, 0, 0.1, 0.01, "npc"))

# Create a line red tag 

  grid.lines(x = c(0, 1), y = 1, gp = gpar(col = "#e5001c", lwd = 4)) 
  
# Create a squared red tag
  
  grid.rect(x = 0, y = 1, width = 0.05, height = 0.025, just = c("left", "top"),
  gp = gpar(fill = "#e5001c", lwd = 0))

Saving the chart

The end result of the whole process concludes two themes, of which one stems from the package ggplot2 and the other from grid package. To capture the chart, click the button Export and save it as your expected format. The chart looks like following.