4  Economic data over time

library(tidyverse)
D <- read_csv("data_macro/inflation_WDI.csv")
oecd <- read_csv("data_macro/oced_codes.csv")

#biggerText <- jzPack::biggerText

# Merge in OECD indicators
D <- left_join(D,oecd)

In how many countries has inflation exceeded 10% in 2022:

D %>% 
  filter(year == 2022) %>%
  mutate(over10 = ifelse(inflation > 10, 1, 0)) %>%
  count(over10)
# A tibble: 2 × 2
  over10     n
   <dbl> <int>
1      0    82
2      1    45
D %>% 
  filter(year %in% c(2010:2022)) %>%
  mutate(over10 = ifelse(inflation > 10, 1, 0)) %>%
  group_by(year) %>%
  summarise(n = n(),
            over10 = sum(over10)) %>%
  ggplot(aes(x=year,y=over10)) +
  geom_col() +
  labs(y="", x="Year",
       title = "Number of countries with inflation 10%+",
       caption = "Data: WDI")

D %>% 
  filter(year %in% c(2010:2022)) %>%
  mutate(over10 = ifelse(inflation > 10, 1, 0)) %>%
  group_by(year) %>%
  summarise(n = n(),
            prop = sum(over10)/n) %>% 
  ggplot(aes(x=year,
             y=prop)) +
  geom_col() +
  labs(y="", x="Year",
       title = "Proportion of countries where inflation exceeded 10%",
       caption = "Data: WDI")

Ther are some obivous problems with the x-axis here, so let’s solve that, plus a few minor issues:

barplot_V2 <-  D %>% 
  filter(year %in% c(2010:2022)) %>%
  mutate(over10 = ifelse(inflation > 10, 1, 0)) %>%
  group_by(year) %>%
  summarise(n = n(),
            prop = sum(over10)/n) %>% 
  ggplot(aes(x=year,
             y=prop)) +
  geom_col() +
  labs(y="", x="Year",
       title = "Proportion of countries where inflation exceeded 10%",
       caption = "Data: WDI") +
  scale_x_continuous(breaks = c(2010:2022)) +
  scale_y_continuous(labels = scales::percent) +
  theme(text = element_text(size=15)) +
  theme_bw() 

barplot_V2

We improves the labels of both axies, and increased the font size.

The new version is reasonably good, but there are still unnecessary gridlines, right?

You could either use theme_classic() instead of theme_bw().

barplot_V2 +
  theme_classic() 

Or we can keep theme_bw() and make some adjustments:

barplot_V2 +
  theme(panel.grid.minor.x = element_blank(),
        panel.grid.major.x = element_blank())

D %>% 
  filter(!is.na(inflation)) %>%
  mutate(over10 = ifelse(inflation > 10, 1, 0)) %>%
  group_by(year) %>%
  ggplot(aes(x=year,
             y=over10)) +
  geom_col() +
  theme_bw() + 
  theme(text = element_text(size=14)) +
  labs(y="", x="Year",
       subtitle = "Number of countries where inflation exceeded 10%",
       caption = "Data: WDI.")


5 Zomm in on OECD countries and increase the time horizon

D %>% filter(OECD==1) %>%
  filter(!is.na(inflation)) %>%
  mutate(over10 = ifelse(inflation > 10, 1, 0)) %>%
  group_by(year) %>%
  summarise(n = n(),
            prop = sum(over10)/n) %>% 
  ggplot(aes(x=year,
             y=prop)) +
  geom_col() +
  theme_bw() + 
  theme(text = element_text(size=15)) +
  scale_y_continuous(labels = scales::percent) +
  labs(y="", x="Year",
       title = "Proportion of countries where inflation exceeded 10%\n(OECD countries only)")

D %>% 
  mutate(over5 = ifelse(inflation > 5, 1, 0)) %>%
  group_by(year) %>%
  summarise(n = n(),
            prop = sum(over5)/n) %>% 
  ggplot(aes(x=year,
             y=prop)) +
  geom_line() + geom_point() +
  theme_bw() + 
  theme(text = element_text(size=15)) +
  scale_y_continuous(labels = scales::percent) +
  labs(y="", x="Year",
       subtitle = "Global proportion of countries where inflation exceeded 5%",
       caption = "Data: WDI.")

D %>% filter(OECD==1) %>%
  filter(!is.na(inflation)) %>%
  group_by(year) %>%
  summarise(M = median(inflation)) %>%
  ggplot(aes(x=year,
             y=M)) +
  # geom_point() + geom_line() +
  geom_col() +
  theme_bw() + 
  theme(text = element_text(size=15)) +
  labs(y="", x="Year",
       title = "Median inflation in OECD countries")