# Preparación de datos:
#| label: grafico-cartera-minimalista
#| warning: false
#| message: false
library(dplyr)
library(readr)
library(ggplot2)
# 1. Carga de datos
Saldo_Cartera_Bruta_Unimos <- read_csv("data/Saldo Cartera Bruta_Unimos.csv",
col_types = cols(Año = col_character(), `Nombre Mes Corto` = col_skip()))
Saldo_Cartera_Bruta_Sector <- read_csv("data/Saldo Cartera Bruta_Sector.csv",
col_types = cols(Año = col_character(), `Nombre Mes Corto` = col_skip()))
# 2. Preparación y Unión
sector_preparado <- Saldo_Cartera_Bruta_Sector %>%
mutate(Tipo = "Sector") %>%
rename(Valor = `1.1.7 Cartera Total`)
unimos_preparado <- Saldo_Cartera_Bruta_Unimos %>%
mutate(Tipo = "Unimos") %>%
rename(Valor = `1.1.7 Cartera Total`)
Cartera_Consolidada <- bind_rows(sector_preparado, unimos_preparado) %>%
mutate(Año = as.numeric(Año))
# --- EL PASO QUE FALTABA: Definir factor y crear cartera_plot ---
f_cartera <- 200 # Factor para equilibrar Billones vs Miles de Millones
cartera_plot <- Cartera_Consolidada %>%
mutate(
# Escalamos visualmente Unimos
Valor_Vis = ifelse(Tipo == "Unimos", Valor * f_cartera, Valor),
# Creamos etiquetas de texto (T para Billones, B para Mil Millones)
Etiqueta = ifelse(Tipo == "Sector",
paste0(round(Valor / 1e12, 1), "T"),
paste0(round(Valor / 1e9, 1), "B")),
X_Num = ifelse(Tipo == "Sector", 1, 2)
)
# 3. Recalcular resumen para el tooltip minimalista
resumen_minimalista <- Cartera_Consolidada %>%
group_by(Tipo) %>%
summarize(
增长_pct = (Valor[Año == 2025] - Valor[Año == 2023]) / Valor[Año == 2023] * 100,
y_max = max(Valor)
) %>%
mutate(
X_Num = ifelse(Tipo == "Sector", 1, 2),
y_vis_max = ifelse(Tipo == "Unimos", y_max * f_cartera, y_max),
Label_Text = paste0("Δ ", round(增长_pct, 1), "%")
)
# 4. Gráfico Final
ggplot(cartera_plot, aes(x = X_Num, y = Valor_Vis, fill = factor(Año))) +
annotate("rect", xmin = 1.5, xmax = 2.5, ymin = 0, ymax = Inf, fill = "#F0F7FB", alpha = 0.3) +
geom_col(position = position_dodge(width = 0.8), width = 0.7, color = "white", linewidth = 0.1) +
# Tooltip Δ %
geom_text(data = resumen_minimalista,
aes(x = X_Num, y = y_vis_max * 1.15, label = Label_Text),
inherit.aes = FALSE, size = 3.5, fontface = "bold", color = "#566573") +
geom_segment(data = resumen_minimalista,
aes(x = X_Num - 0.2, xend = X_Num + 0.2, y = y_vis_max * 1.08, yend = y_vis_max * 1.08),
inherit.aes = FALSE, color = "#AEB6BF", linewidth = 0.3) +
# Etiquetas de valores
geom_text(aes(label = Etiqueta),
position = position_dodge(width = 0.8),
vjust = -0.5, size = 2.8, color = "grey30") +
scale_x_continuous(breaks = c(1, 2), labels = c("SECTOR", "UNIMOS")) +
scale_y_continuous(expand = expansion(mult = c(0, 0.25)), labels = NULL, name = NULL) +
scale_fill_manual(values = c("2023" = "#D5DBDB", "2024" = "#5D6D7E", "2025" = "#E67E22"), name = NULL) +
labs(title = "Crecimiento Acumulado de Cartera",
subtitle = "Variación porcentual periodo 2023-2025") +
theme_void() +
theme(
plot.margin = margin(20, 20, 20, 20),
plot.title = element_text(face = "bold", size = 12, hjust = 0.5, color = "#2C3E50"),
plot.subtitle = element_text(size = 9, hjust = 0.5, color = "#7F8C8D", margin = margin(b = 20)),
axis.text.x = element_text(size = 9, face = "bold", color = "#2C3E50", margin = margin(t = 10)),
legend.position = "bottom"
)