library(tidyverse)
library(scales)
library(patchwork)
# --- 1. FUNCIÓN DE FORMATEO EN ESPAÑOL ---
# Esta función asegura que usemos "Mil", "millones", etc.
formatear_moneda_es <- function(x) {
case_when(
is.na(x) ~ "",
x >= 1e12 ~ paste(round(x/1e12, 1), "billones"),
x >= 1e9 ~ paste(round(x/1e9, 1), "miles de millones"),
x >= 1e6 ~ paste(round(x/1e6, 1), "millones"),
x >= 1e3 ~ paste(round(x/1e3, 1), "Mil"),
TRUE ~ as.character(round(x, 0))
)
}
# --- 2. CARGA Y LIMPIEZA ---
df_tec_raw <- read.csv("gasto_tecnologia.csv", check.names = FALSE)
df_tec_clean <- df_tec_raw %>%
select(
Sigla = 1,
Departamento = 2,
Gasto_Tec = 3,
Ingresos = 4,
Relacion_Gasto = 5
) %>%
mutate(
Sigla = str_to_upper(str_trim(Sigla)),
Relacion_Gasto = as.numeric(str_replace_all(Relacion_Gasto, "[^0-9,.-]", "") %>%
str_replace(",", ".")) / 100,
Gasto_Tec = as.numeric(Gasto_Tec),
Ingresos = as.numeric(Ingresos)
) %>%
filter(!is.na(Relacion_Gasto), Ingresos > 0)
# --- 3. ESTADÍSTICAS Y POSICIÓN DE UNIMOS ---
percentil_gasto <- ecdf(df_tec_clean$Gasto_Tec)
percentil_ingreso <- ecdf(df_tec_clean$Ingresos)
percentil_relacion <- ecdf(df_tec_clean$Relacion_Gasto)
df_tec_stats <- df_tec_clean %>%
mutate(
p_gasto = percentil_gasto(Gasto_Tec),
p_ingreso = percentil_ingreso(Ingresos),
p_relacion = percentil_relacion(Relacion_Gasto)
)
unimos_tec <- df_tec_stats %>% filter(str_detect(Sigla, "UNIMOS"))
# --- 4. COMPONENTES VISUALES MEJORADOS ---
# P1: Gasto Tecnología
p1 <- ggplot(df_tec_stats, aes(x = Gasto_Tec)) +
geom_histogram(bins = 40, fill = "#D5D8DC", color = "white") +
geom_vline(data = unimos_tec, aes(xintercept = Gasto_Tec), color = "#E67E22", linewidth = 1.5) +
geom_text(data = unimos_tec,
aes(x = Gasto_Tec, y = Inf, label = paste0("UNIMOS: $", formatear_moneda_es(Gasto_Tec))),
vjust = 2, color = "#E67E22", fontface = "bold", size = 4) +
scale_x_log10(labels = function(x) paste0("$", formatear_moneda_es(x))) +
labs(title = "Distribución: Gasto Total en Tecnología", x = "Gasto TI", y = "Entidades") +
theme_minimal()
# P2: Ingresos Totales
p2 <- ggplot(df_tec_stats, aes(x = Ingresos)) +
geom_histogram(bins = 40, fill = "#ABB2B9", color = "white") +
geom_vline(data = unimos_tec, aes(xintercept = Ingresos), color = "#E67E22", linewidth = 1.5) +
geom_text(data = unimos_tec,
aes(x = Ingresos, y = Inf, label = paste0("UNIMOS: $", formatear_moneda_es(Ingresos))),
vjust = 2, color = "#E67E22", fontface = "bold", size = 4) +
scale_x_log10(labels = function(x) paste0("$", formatear_moneda_es(x))) +
labs(title = "Distribución: Ingresos Totales", x = "Ingresos", y = "Entidades") +
theme_minimal()
# P3: Eficiencia (%)
p3 <- ggplot(df_tec_stats, aes(x = Relacion_Gasto)) +
geom_histogram(bins = 40, fill = "#2E4053", color = "white") +
geom_vline(data = unimos_tec, aes(xintercept = Relacion_Gasto), color = "#E67E22", linewidth = 1.5) +
geom_text(data = unimos_tec,
aes(x = Relacion_Gasto, y = Inf,
label = paste0("Inversión: ", round(Relacion_Gasto * 100, 2), "%",
"\nPercentil: ", round(p_relacion * 100, 1), "%")),
vjust = 2, hjust = -0.1, color = "#E67E22", fontface = "bold", size = 4.5) +
scale_x_continuous(labels = percent_format(accuracy = 1), limits = c(0, 0.15)) +
labs(title = "Eficiencia: Gasto TI sobre Ingresos (%)", x = "Porcentaje de Inversión", y = "Entidades") +
theme_minimal()
# P4: Correlación con etiquetas de datos
p4 <- ggplot(df_tec_stats, aes(x = Ingresos, y = Gasto_Tec)) +
geom_point(alpha = 0.3, color = "#2E4053") +
geom_point(data = unimos_tec, color = "#E67E22", size = 5) +
geom_text(data = unimos_tec,
aes(label = paste0("UNIMOS\nIngresos: $", formatear_moneda_es(Ingresos),
"\nGasto TI: $", formatear_moneda_es(Gasto_Tec))),
vjust = -1, color = "#E67E22", fontface = "bold", size = 3.5) +
geom_smooth(method = "lm", color = "#D5D8DC", linetype = "dashed", se = FALSE) +
scale_x_log10(labels = function(x) paste0("$", formatear_moneda_es(x))) +
scale_y_log10(labels = function(x) paste0("$", formatear_moneda_es(x))) +
labs(title = "Correlación: Escala de Ingresos vs. Inversión",
subtitle = "Resaltado de valores exactos para UNIMOS", x = "Ingresos", y = "Inversión TI") +
theme_minimal()
# --- 5. ENSAMBLAJE FINAL ---
(p1 + p2) / (p3 + p4) +
plot_annotation(
title = "Dashboard Estratégico de Gasto Tecnológico - Cooperativa Unimos",
subtitle = "Comparativa de escala operativa frente a inversión en infraestructura tecnológica"
) &
theme(plot.title = element_text(face = "bold", size = 16))