Motivation

Recreate VISUAL CAPITALIST chart:U.S. Monetary Base (2007–2023)

Data Processing

# Clear R environment: 
rm(list = ls())

# Setwd
setwd("D:/0 - My documents/TOOLS/R/Visual Capitalist/U.S. Monetary Base")

# Load data
library(rvest)
library(tidyverse)

# Specify the URL
url <- "https://posts.voronoiapp.com/money/US-Monetary-Base-20072023-718"

# Read the HTML content of the webpage -> Change to numeric -> Rename variables
webpage <- read_html(url)
monetary <- html_table(html_nodes(webpage, "table"))[[1]]
monetary <- monetary[2:189, ]

# Rename monetary
names(monetary) <- c("date","cc","rb","mb")

monetary <- monetary %>% 
  mutate(cc = as.numeric(gsub(",", "", cc))) %>% 
  mutate(rb = as.numeric(gsub(",", "", rb))) %>% 
  mutate(mb = as.numeric(gsub(",", "", mb)))

monetary <- monetary %>% 
  mutate(cc_chart = cc + rb)

Visualization

# Visualization
library(ggdark)

library(extrafont)
library(ggtext)

font_import(pattern = "Philosopher")
y
fonts()
loadfonts(device = "win")
windowsFonts()

ggplot(monetary) +
  geom_area(aes(x = seq_along(date), y = cc_chart), fill = "#9AD0C2", color = NA)+
  geom_line(aes(x = seq_along(date), y = mb), color = "white", linewidth = 1.5)+
  geom_area(aes(x = seq_along(date), y = cc), fill = "#2D9596", color = NA)+
  # Set theme
  dark_theme_minimal()+
  # Adjust background
  theme(axis.ticks = element_blank()) + 
  theme(axis.title = element_blank()) +
  theme(panel.grid.major.x = element_blank())+
  theme(panel.grid.minor = element_blank())+
  theme(panel.grid.major.y = element_line(color = "grey40", linewidth = 0.1))+
  # Scale x,y
  scale_y_continuous(breaks = c(0, 1000, 2000, 3000, 4000, 5000, 6000, 7000),
                     expand = c(0, 0),
                     limits = c(0, 7000),
                     labels = c("-", "1,000", "2,000", "3,000", "4,000", "5,000", "6,000", "7,000"),
                     position = "right")+
  scale_x_continuous(
    breaks = seq(0, 194, by = 12),
    expand = c(0, 0),
    limits = c(0, 194),
    labels = c("2007-12", "", "2009-12", "", "2011-12", "", "2013-12", "", "2015-12", "", "2017-12", "", "2019-12", "", "2021-12", "", "2023-12"))+
  # Axis ticks
  theme(axis.ticks.x = element_line(color = "grey40", linewidth = 0.2))+
  # Adjust plot margin
  theme(plot.margin = unit(c(0.3, 0.5, 0.4, 0.5), "cm"))+
  # Add title, sub, caption
  labs(title = "U.S. Monetary Base", 
       subtitle = "The monetary base is  the sum of currency in curculation and required\nreserves held by banks and other depository institutions at the Federal Reserve.", 
       caption = c("Sources: YouGov"))+
  # Adjust title, sub, caption
  theme(plot.title = element_text(size = 20, color = "white", family = "#9Slide03 SFU SwissBT"))+
  theme(plot.subtitle = element_text(size = 8, color = "white", family = "#9Slide03 SFU DIN"))+
  theme(plot.caption = element_text(size = 7, color = "white", family = "#9Slide03 SFU DIN"))+
  theme(axis.text = element_text(size = 7, color = "grey40", family = "#9Slide03 SFU DIN"))+
  theme(plot.caption = element_text(hjust = 0))+
  # Add text
  geom_text(label = "Currency in Circulation",
            x = 133,
            y = 800,
            size = 3,
            color = "white",
            family = "#9Slide03 SFU DIN")+
  geom_text(label = "Reserve Balances",
            x = 133,
            y = 2800,
            size = 3,
            color = "black",
            family = "#9Slide03 SFU DIN")+
  geom_text(label = "Monetary Balances",
            x = 130,
            y = 5800,
            size = 3,
            color = "white",
            family = "#9Slide03 SFU DIN")+
  geom_text(label = "US$ Billion",
            x = 1,
            y = 6900,
            size = 2,
            color = "grey80",
            family = "#9Slide03 SFU DIN",
            hjust = 0)+
  geom_text(label = "QE1",
            x = 1,
            y = 1600,
            size = 2.5,
            color = "grey80",
            family = "#9Slide03 SFU DIN",
            hjust = 0)+
  geom_text(label = "QE2",
            x = 34,
            y = 2600,
            size = 2.5,
            color = "grey80",
            family = "#9Slide03 SFU DIN")+
  geom_text(label = "QE3",
            x = 61,
            y = 3600,
            size = 2.5,
            color = "grey80",
            family = "#9Slide03 SFU DIN")+
  geom_text(label = "QE4",
            x = 140,
            y = 4700,
            size = 2.5,
            color = "grey80",
            family = "#9Slide03 SFU DIN")

# Save chart
ggsave("monetary.png", width = 5, height = 5,dpi = 300,units = c("in"))