library(readxl)
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.4.3
## Warning: package 'ggplot2' was built under R version 4.4.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 4.0.3 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(scales)
## Warning: package 'scales' was built under R version 4.4.3
##
## Adjuntando el paquete: 'scales'
##
## The following object is masked from 'package:purrr':
##
## discard
##
## The following object is masked from 'package:readr':
##
## col_factor
gdp_spp <- read_excel(
"C:/Users/emili/OneDrive/Documentos/gdp_per_capita_usd_2021_spp_terminada_v2.xlsx"
)
# ---------------------------------------------------------
# Data by country
# ---------------------------------------------------------
gdp_mex <- gdp_spp %>%
filter(country_name == "MEXICO")
gdp_bra <- gdp_spp %>%
filter(country_name == "BRAZIL")
gdp_arg <- gdp_spp %>%
filter(country_name == "ARGENTINA")
# ---------------------------------------------------------
# Compact plot: one figure per country
# ---------------------------------------------------------
plot_gdp_country <- function(data, country, color) {
ggplot(
data,
aes(
x = year,
y = gdp_per_capita
)
) +
# Historical trend
geom_line(
color = color,
linewidth = 0.65
) +
# Most recent observation
geom_point(
data = data %>%
filter(year == max(year, na.rm = TRUE)),
color = color,
size = 1.5
) +
facet_wrap(
~state_name,
ncol = 5,
scales = "fixed"
) +
scale_x_continuous(
breaks = c(1980, 2000, 2024),
limits = c(1980, 2024),
expand = expansion(mult = c(0.02, 0.02))
) +
scale_y_continuous(
labels = label_dollar(
prefix = "$",
big.mark = ",",
accuracy = 1000
),
expand = expansion(mult = c(0.02, 0.05))
) +
labs(
title = paste0("Subnational GDP per Capita — ", country),
subtitle = "Constant 2021 USD",
x = NULL,
y = NULL
) +
theme_minimal(base_size = 10) +
theme(
# Title
plot.title = element_text(
face = "bold",
size = 14,
margin = margin(b = 2)
),
plot.subtitle = element_text(
size = 9,
color = "grey40",
margin = margin(b = 8)
),
# Facets
strip.text = element_text(
face = "bold",
size = 8
),
strip.background = element_blank(),
# Grid
panel.grid.minor = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_line(
linewidth = 0.25,
color = "grey85"
),
# Axes
axis.text.x = element_text(
size = 7,
color = "grey35"
),
axis.text.y = element_text(
size = 7,
color = "grey35"
),
# Spacing
panel.spacing = unit(0.7, "lines"),
plot.margin = margin(
8, 8, 8, 8
)
)
}
mex_plot <- plot_gdp_country(
gdp_mex,
"Mexico",
"#CE1126"
)
bra_plot <- plot_gdp_country(
gdp_bra,
"Brazil",
"#009739"
)
arg_plot <- plot_gdp_country(
gdp_arg,
"Argentina",
"#2C7FB8"
)
mex_plot

bra_plot

arg_plot
