Gráficos de líneas exportaciones e importaciones Europa

library(dplyr)
library(ggplot2)

IE3 <- IE3 %>%
  mutate(
    OBS_VALUE = as.numeric(gsub(",", ".", gsub("[^0-9,\\.]", "", OBS_VALUE))),
    TIME_PERIOD = as.numeric(TIME_PERIOD)
  )

# Lista de países de la UE
paises_ue_lista <- c("Germany", "Spain", "France", "Italy", "Poland", "Sweden", "Netherlands",
                     "Portugal", "Greece", "Ireland", "Austria", "Belgium", "Croatia", "Hungary",
                     "Romania", "Finland", "Denmark", "Czechia", "Estonia", "Slovakia", "Slovenia",
                     "Lithuania", "Latvia", "Bulgaria", "Luxembourg")

# Detectar países con datos disponibles
paises_presentes <- IE3 %>%
  filter(TIME_PERIOD %in% 2006:2012 | TIME_PERIOD %in% 2018:2023,
         na_item %in% c("Exports of goods and services", "Imports of goods and services")) %>%
  distinct(geo) %>%
  pull(geo)

# Clasificación UE / No UE
paises_total <- data.frame(
  geo = paises_presentes,
  grupo = ifelse(paises_presentes %in% paises_ue_lista, "UE", "No UE")
)

# Filtrar y preparar datos
IE_crisis_pandemia <- IE3 %>%
  filter(geo %in% paises_total$geo,
         TIME_PERIOD %in% 2006:2012 | TIME_PERIOD %in% 2018:2023,
         na_item %in% c("Exports of goods and services", "Imports of goods and services"))

datos_combinados <- IE_crisis_pandemia %>%
  mutate(flujo = ifelse(na_item == "Exports of goods and services", "Exportaciones", "Importaciones")) %>%
  left_join(paises_total, by = "geo")

datos_combinados$grupo <- factor(datos_combinados$grupo, levels = c("UE", "No UE"))

# --- GRÁFICO UE ---
grafico_ue <- datos_combinados %>%
  filter(grupo == "UE") %>%
  ggplot(aes(x = TIME_PERIOD, y = OBS_VALUE/1000, color = flujo)) +
  geom_line(linewidth = 1.1) +
  geom_vline(xintercept = 2008, linetype = "dashed", color = "red", linewidth = 0.8) +
  geom_vline(xintercept = 2020, linetype = "dashed", color = "blue", linewidth = 0.8) +
  facet_wrap(~geo, ncol = 3, scales = "free_y") +
  labs(
    title = "Exportaciones e importaciones (2006–2012 y 2018–2023)",
    subtitle = "Países de la Unión Europea (líneas: 2008 y 2020)",
    x = "Año", y = "Valor en miles de millones €",
    color = "Tipo de flujo"
  ) +
  scale_x_continuous(breaks = seq(2006, 2023, by = 2)) +
  theme_minimal(base_size = 13) +
  theme(
    plot.title = element_text(size = 18, face = "bold"),
    plot.subtitle = element_text(size = 14),
    strip.text = element_text(size = 11, face = "bold"),
    axis.text.x = element_text(angle = 45, hjust = 1)
  )

# --- GRÁFICO No UE ---
grafico_no_ue <- datos_combinados %>%
  filter(grupo == "No UE") %>%
  ggplot(aes(x = TIME_PERIOD, y = OBS_VALUE/1000, color = flujo)) +
  geom_line(linewidth = 1.1) +
  geom_vline(xintercept = 2008, linetype = "dashed", color = "red", linewidth = 0.8) +
  geom_vline(xintercept = 2020, linetype = "dashed", color = "blue", linewidth = 0.8) +
  facet_wrap(~geo, ncol = 3, scales = "free_y") +
  labs(
    title = "Exportaciones e importaciones (2006–2012 y 2018–2023)",
    subtitle = "Países fuera de la Unión Europea (líneas: 2008 y 2020)",
    x = "Año", y = "Valor en miles de millones €",
    color = "Tipo de flujo"
  ) +
  scale_x_continuous(breaks = seq(2006, 2023, by = 2)) +
  theme_minimal(base_size = 13) +
  theme(
    plot.title = element_text(size = 18, face = "bold"),
    plot.subtitle = element_text(size = 14),
    strip.text = element_text(size = 11, face = "bold"),
    axis.text.x = element_text(angle = 45, hjust = 1)
  )

# Mostrar
print(grafico_ue)

print(grafico_no_ue)