ALLUVIAL DIAGRAM

El presente grƔfico muestra la esperanza de vida desde el aƱo 2003 al 2015 agrupada por regiones.

# ESPERANZA DE VIDA AGRUPADA POR REGIƓN - WORDL DATA BANK

df_alluvial <- WorldSustainabilityDataset %>%
  filter(Year == max(Year, na.rm = TRUE)) %>%
  mutate(
    Pobreza_cat = case_when(
      SI_POV_DAY1 < 8 ~ "Baja",
      SI_POV_DAY1 < 30 ~ "Media",
      TRUE ~ "Alta"
    )
  ) %>%
  count(Region, Income, Pobreza_cat, name = "Frecuencia")

options(repr.plot.width = 20, repr.plot.height = 7)  # aumenta el ancho del grƔfico


ggplot(df_alluvial,
       aes(axis1 = Region, axis2 = Income, axis3 = Pobreza_cat, y = Frecuencia)) +
  geom_alluvium(aes(fill = Region), width = 0.2, alpha = 0.6) +  
  geom_stratum(width = 1/6, fill = "gray90", color = "black") +
  geom_text(stat = "stratum", aes(label = after_stat(stratum)), size = 2.5) +
  scale_x_discrete(limits = c("Región", "Ingreso", "Pobreza"), expand = c(0.25, 0.25)) +
  theme_minimal() +
  labs(title = "Distribución de la pobreza por región e ingreso",
       y = "Frecuencia") +
  theme(axis.text.x = element_text(size = 12, face = "bold"),
        plot.title = element_text(hjust = 0.5, size = 14))