Motivation

The Economist

Topic: Average annual hours worked by persons engaged

Year: 1988-2017

Reference: Nguyen Chi Dung

Data Processing

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

# Set working directory
setwd("D:/0 - My documents/TOOLS/R/The Economist/Annual hours worked")

# Pacman: Load necessary packages
library(pacman)
pacman::p_load(
  tidyverse,
  skimr,
  summarytools,
  pwt9,
  ggthemes,
  grid)

# Load Penn World Table Data: 
data("pwt9.0")

# Prepare data for ploting: 

some_countries <- c("France", "Vietnam", "Japan", "Singapore", "United States", "South Korea")

df_plot <- pwt9.1 %>% 
  mutate(country = as.character(country)) %>% 
  mutate(country = case_when(country == "United States of America" ~ "United States", 
                             country == "Republic of Korea" ~ "South Korea", 
                             country == "Viet Nam" ~ "Vietnam", 
                             TRUE ~ country)) %>% 
  select(year, country, avh) %>% 
  na.omit() %>% 
  filter(year >= 1986) %>% 
  filter(country %in% some_countries)

Data Visualization

# Make a draft graph: 

my_colors <-  c("#04536e", "#7c2817", "#f15c42", "#3d6a51", "#eca324", "#12a4dc")

label_y <- c(1986, rep("", 3), 1990, rep("", 3), 1994, rep("", 3), 1998, rep("", 3), 
             2002, rep("", 3), 2006, rep("", 3), 2010, rep("", 3), 2014, rep("", 2), 2017)

df_text <- df_plot %>% 
  filter(year == 1987) %>% 
  filter(country %in% c("France", "Vietnam", "Japan", "Singapore", "United States", "South Korea")) %>% 
  arrange(desc(avh))
  
View(df_text)

graph <- df_plot %>% 
  ggplot(aes(year, avh, group = country, color = country)) +
  geom_line(size = 1, show.legend = FALSE) +
  theme_hc() +
  scale_color_manual(values = my_colors) +
  scale_x_continuous(limits = c(1986, 2017), breaks = seq(1986, 2017, 1), 
                     labels = label_y, expand = c(0, 0)) +
  scale_y_continuous(limits = c(1450, 3000)) +
  theme(panel.grid.minor = element_blank()) +
  theme(panel.grid.major = element_line(color = "grey40", size = 0.2))+
  geom_text(data = df_text, aes(year, avh + 90, label = country), size = 5, 
            hjust = 0, show.legend = FALSE) +
  theme(plot.margin = unit(c(0.7, 1, 1, 1), "cm")) +
  labs(title = "Average annual hours worked by persons engaged", 
       subtitle = "Average annual hours worked is defined as the total number of hours actually worked per year divided\nby the average number of people in employment per year.", 
       caption = "Source: The Penn World Table (PWT)") +
  theme(plot.title = element_text(size = 30, color = "grey10", face = "bold"))+
  theme(plot.subtitle = element_text(size = 15, color = "grey20", hjust = 0, margin = margin(b = 6)))+
  theme(plot.caption = element_text(size = 12, color = "grey30", hjust = 1, margin = margin(t = 6))) +
  theme(axis.title.x = element_blank()) +
  theme(axis.title.y = element_blank()) +
  theme(axis.text.x = element_text(size = 15, color = "grey30")) +
  theme(axis.text.y = element_text(size = 15, color = "grey30"))

graph

grid.rect(x = 0.012, y = 0.9, hjust = 1, vjust = 0, gp = gpar(fill = "grey10", lwd = 0, col = "transparent"))
grid.rect(x = 1, y = 1 - 0.008, hjust = 1, vjust = 0,  gp = gpar(fill = "grey10", lwd = 0, col = "transparent"))