# Create the data frame with estimated values
data <- data.frame(
Country = c("CHL", "BRA", "PRY", "PER", "COL", "URY", "MEX"),
Credit_to_Private_Sector = c(80, 70, 60, 50, 45, 25, 20)
)
# Define the LA7 average
la7_average <- 50
# Reorder the Country factor based on Credit_to_Private_Sector values (from largest to smallest)
data$Country <- factor(data$Country, levels = data$Country[order(-data$Credit_to_Private_Sector)])
# Create a dummy data frame for the LA7 average line legend
dummy_line <- data.frame(Country = c(NA), Credit_to_Private_Sector = c(NA))
# Create the bar chart using IMF-style formatting
ggplot(data, aes(x = Country, y = Credit_to_Private_Sector)) +
geom_bar(stat = "identity", fill = ifelse(data$Country == "URY", "#900000", "#4B82AD"),
color = "grey70", size = 0.4) + # Softer grey borders with thinner lines
geom_hline(aes(yintercept = la7_average, linetype = "LA7 average"), color = "black", linewidth = 1.2) +
scale_linetype_manual(name = "", values = "dashed") + # Add dashed line in the legend
labs(
title = "Credit to Private Sector: 2023 1/", # Custom title
subtitle = "(Percent of GDP)", # Custom subtitle
x = "", # Removing X axis label
y = "", # Removing Y axis label
caption = "1/ Latest observation available for PER is 2022.\nSources: IMF staff calculations." # IMF-style caption
) +
theme_imf() + # Apply IMF theme
theme(
plot.title = element_text(hjust = 0, size = 16, face = "bold", color = "#4B82AD"), # Left-align title with corrected IMF blue
plot.subtitle = element_text(hjust = 0, size = 12, color = "#4B82AD"), # Left-align subtitle with IMF blue
plot.caption = element_text(size = 8, hjust = 0, color = "black"), # Smaller caption font size
legend.position = c(0.75, 0.95), # Position the legend inside the plot area
legend.direction = "horizontal", # Arrange the legend horizontally
legend.background = element_rect(fill = "transparent"), # Transparent background for the legend
legend.text = element_text(size = 9, color = "black"), # Slightly larger legend text
panel.grid.major = element_blank(), # Remove grid lines
panel.grid.minor = element_blank(), # Remove grid lines
axis.text.x = element_text(size = 10, color = "black"), # Set x-axis text size and color
axis.text.y = element_text(size = 10, color = "black"), # Set y-axis text size and color
axis.line = element_line(size = 0.3, color = "grey80"), # Softer grey axis lines
plot.margin = margin(t = 10, b = 10, l = 10, r = 10) # Adjust margins to match IMF style
) +
guides(linetype = guide_legend(override.aes = list(color = "black"))) # Ensure the dashed line appears black in the legend