# Load required libraries
library(tidyverse)
library(here)
library(ggplot2)
library(haven)
# Load custom IMF themes and colors
source(here("utils/theme_and_colors_IMF.R"))
source(here("utils/Add_text_to_figure_panel.R"))
# Import the WEO data
weo <- read_dta(here::here("databases/WEOApr2023Pub.dta"))
# Define the selected countries
selected_countries <- c("Euro area", "Japan", "United Kingdom", "Canada",
"Australia", "New Zealand", "Switzerland",
"Sweden", "Norway")
# Filter and preprocess the data
weo_filtered <- weo %>%
filter(
country %in% selected_countries,
!is.na(year) & year >= 1990 & year <= 2024, # Filter for years 1990–2024
!is.na(ngdppch), # Filter non-missing GDP growth
!is.na(le_pch) # Filter non-missing employment growth
) %>%
select(country, year, ngdppch, le_pch)
# Ensure the filtered data is not empty
if (nrow(weo_filtered) == 0) {
stop("No data available for the specified countries and year range.")
}
# Add regression stats to each panel
weo_with_regression <- weo_filtered %>%
group_by(country) %>%
summarize(
regression = list(lm(le_pch ~ ngdppch, data = cur_data())),
r_squared = summary(lm(le_pch ~ ngdppch, data = cur_data()))$r.squared,
coef_intercept = coef(lm(le_pch ~ ngdppch, data = cur_data()))[1],
coef_slope = coef(lm(le_pch ~ ngdppch, data = cur_data()))[2],
x_position = min(ngdppch, na.rm = TRUE), # Place at the minimum x value
y_position = max(le_pch, na.rm = TRUE) # Place at the maximum y value
) %>%
ungroup()
# Merge R² and regression coefficients back into the main data for annotation
weo_annotated <- weo_filtered %>%
left_join(weo_with_regression %>% select(country, r_squared, coef_intercept, coef_slope, x_position, y_position), by = "country")
# Create the panel chart with regression lines, R², and equation
fig_gdp_vs_employment <- ggplot(weo_annotated, aes(x = ngdppch, y = le_pch)) +
geom_point(shape = 21, fill = blue, color = blue, alpha = 0.6, size = 2.5) + # Smaller scatter plot points in IMF blue
geom_smooth(method = "lm", se = FALSE, color = blue, linetype = "dashed", linewidth = 1) + # Dashed regression line in IMF blue
facet_wrap(~ country, scales = "free") + # One panel per country
labs(
title = "Relationship Between GDP Growth and Employment Growth",
subtitle = "(1990–2024, In Percent)",
x = "GDP Growth (%)",
y = "Employment Growth (%)",
caption = "Sources: IMF WEO April 2023 database."
) +
theme_imf_panel() + # Use IMF panel theme for consistent style
theme(
plot.caption = element_text(size = 8, hjust = 0),
plot.title = element_text(size = 16, face = "bold", hjust = 0, color = blue), # Title in IMF blue
plot.subtitle = element_text(size = 12, hjust = 0, color = blue), # Subtitle in IMF blue
strip.text = element_text(size = 12, face = "bold", hjust = 0, color = blue), # Subplot titles in IMF blue
axis.text.x = element_text(angle = 0, hjust = 0.5), # Horizontal x-axis labels
strip.background = element_blank(), # Remove background for facet labels
panel.spacing = unit(1, "lines"), # Add spacing between panels
panel.border = element_blank(), # Remove aggressive black borders
legend.position = "none" # Remove legend
) +
# Add regression equation and R² as text annotations
geom_text(
data = weo_with_regression,
aes(
x = x_position, # Place at the minimum x value (left)
y = y_position, # Place at the maximum y value (top)
label = paste0(
"y = ", round(coef_slope, 2), "x + ", round(coef_intercept, 2), "\n",
"R² = ", round(r_squared, 2)
)
),
inherit.aes = FALSE, hjust = -0.1, vjust = 1.1, size = 3.5, color = "black"
)