Resumen Ejecutivo


Este análisis de la industria automotriz se enfoca en dos entidades representativas: Aguascalientes, un hub consolidado de manufactura automotriz, y Morelos, una región con potencial logístico y productivo.

La amenaza de aranceles estadounidenses nos obligan buscar diversificar nuestros socios estratégicos y reducir nuestra dependencia comercial.Por medio de un análisis de datos y técnicas de clusterización, hemos identificado nuevas oportunidades, especialmente en atraer toda la relocalización de la producción de Nissan a Morelos y Aguascalientes. Candaá y el mercado latinoamericano siendo los potencialmente mejores socios para diversificar exportaciones.


Librerías


# Establecer directorio
setwd("~/Downloads/Final")

# Librerías 

if (!require(haven)) install.packages("haven")
## Loading required package: haven
library(haven)

if (!require(ggplot2)) install.packages("ggplot2")
## Loading required package: ggplot2
library(ggplot2)

if (!require(scales)) install.packages("scales")
## Loading required package: scales
library(scales)

if (!require(comtradr)) install.packages("comtradr")
## Loading required package: comtradr
library(comtradr)

if (!require(showtext)) install.packages("showtext")
## Loading required package: showtext
## Loading required package: sysfonts
## Loading required package: showtextdb
library(showtext)

if (!require(dplyr)) install.packages("dplyr")
## Loading required package: dplyr
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(dplyr)

if (!require(readxl)) install.packages("readxl")
## Loading required package: readxl
library(readxl)

if (!require(stringr)) install.packages("stringr")
## Loading required package: stringr
library(stringr)

if (!require(tidyr)) install.packages("tidyr")
## Loading required package: tidyr
library(tidyr)

if (!require(igraph)) install.packages("igraph")
## Loading required package: igraph
## 
## Attaching package: 'igraph'
## The following object is masked from 'package:tidyr':
## 
##     crossing
## The following objects are masked from 'package:dplyr':
## 
##     as_data_frame, groups, union
## The following objects are masked from 'package:stats':
## 
##     decompose, spectrum
## The following object is masked from 'package:base':
## 
##     union
library(igraph)

if (!require(tidygraph)) install.packages("tidygraph")
## Loading required package: tidygraph
## 
## Attaching package: 'tidygraph'
## The following object is masked from 'package:igraph':
## 
##     groups
## The following object is masked from 'package:stats':
## 
##     filter
library(tidygraph)

if (!require(ggraph)) install.packages("ggraph")
## Loading required package: ggraph
library(ggraph)

if (!require(grid)) install.packages("grid")
## Loading required package: grid
library(grid)

if (!require(sysfonts)) install.packages("sysfonts")
library(sysfonts)

if (!require(ggimage)) install.packages("ggimage")
## Loading required package: ggimage
library(ggimage)

if (!require(sf)) install.packages("sf")
## Loading required package: sf
## Linking to GEOS 3.13.0, GDAL 3.8.5, PROJ 9.5.1; sf_use_s2() is TRUE
library(sf)

if (!require(rnaturalearth)) install.packages("rnaturalearth")
## Loading required package: rnaturalearth
library(rnaturalearth)

if (!require(viridis)) install.packages("viridis")
## Loading required package: viridis
## Loading required package: viridisLite
## 
## Attaching package: 'viridis'
## The following object is masked from 'package:scales':
## 
##     viridis_pal
library(viridis)

if (!require(tidyverse)) install.packages("tidyverse")
## Loading required package: tidyverse
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats   1.0.0     ✔ readr     2.1.5
## ✔ lubridate 1.9.4     ✔ tibble    3.2.1
## ✔ purrr     1.0.4     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ lubridate::%--%()       masks igraph::%--%()
## ✖ tibble::as_data_frame() masks igraph::as_data_frame(), dplyr::as_data_frame()
## ✖ readr::col_factor()     masks scales::col_factor()
## ✖ purrr::compose()        masks igraph::compose()
## ✖ igraph::crossing()      masks tidyr::crossing()
## ✖ purrr::discard()        masks scales::discard()
## ✖ tidygraph::filter()     masks dplyr::filter(), stats::filter()
## ✖ dplyr::lag()            masks stats::lag()
## ✖ purrr::simplify()       masks igraph::simplify()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(tidyverse)

if (!require(countrycode)) install.packages("countrycode")
## Loading required package: countrycode
library(countrycode)


Industria automotriz: motor económico de México 🛠️



Análisis macroeconómico: Estadísticas y datos


# Tendencia: Crecimiento sostenido 

set_primary_comtrade_key("4dfa11c2a4374c0cbb5bc4403252b281")

billion <- function(value) {
  value / 1e9
}

# Fuente Lato
font_add_google("Lato", "lato")
showtext_auto()

# Paleta de colores
color_guinda <- "#9F2241"
color_guinda_claro <- "#F8C3C3"
color_fondo <- "#FFFFFF"
color_texto <- "#1A1A1A"
color_verde <- "#D4C19C" 
color_gris <- "#D9D9D9"

# Años de interés
years <- c(2014, 2018, 2022, 2024)

# Iniciar una lista vacia para guardar datos 
MEX_data_list <- list()

# Loop por cada año y obtener datos
for (yr in years) {
  data <- ct_get_data(
    reporter = "MEX",
    partner = "all_countries",
    flow_direction = c("Import", "Export"),
    start_date = yr,
    end_date = yr,
    commodity_code = "8703", 
    frequency = "A"
  )
  MEX_data_list[[as.character(yr)]] <- data
}
## Waiting 5s for throttling delay ■■■■■■■ Waiting 5s for throttling delay ■■■■■■■
## Waiting 5s for throttling delay ■■■■■■■■ Waiting 5s for throttling delay
## ■■■■■■■■■ Waiting 5s for throttling delay ■■■■■■■■■■■ Waiting 5s for throttling
## delay ■■■■■■■■■■■■ Waiting 5s for throttling delay ■■■■■■■■■■■■■ Waiting 5s for
## throttling delay ■■■■■■■■■■■■■■ Waiting 5s for throttling delay ■■■■■■■■■■■■■■■
## Waiting 5s for throttling delay ■■■■■■■■■■■■■■■■■ Waiting 5s for throttling
## delay ■■■■■■■■■■■■■■■■■■ Waiting 5s for throttling delay ■■■■■■■■■■■■■■■■■■■
## Waiting 5s for throttling delay ■■■■■■■■■■■■■■■■■■■■■ Waiting 5s for throttling
## delay ■■■■■■■■■■■■■■■■■■■■■ Waiting 5s for throttling delay
## ■■■■■■■■■■■■■■■■■■■■■■■ Waiting 5s for throttling delay
## ■■■■■■■■■■■■■■■■■■■■■■■■ Waiting 5s for throttling delay
## ■■■■■■■■■■■■■■■■■■■■■■■■■ Waiting 5s for throttling delay
## ■■■■■■■■■■■■■■■■■■■■■■■■■■■ Waiting 5s for throttling delay
## ■■■■■■■■■■■■■■■■■■■■■■■■■■■ Waiting 5s for throttling delay
## ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ Waiting 5s for throttling delay
## ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ Waiting 5s for throttling delay ■■■■■■■ Waiting
## 5s for throttling delay ■■■■■■■ Waiting 5s for throttling delay ■■■■■■■■■
## Waiting 5s for throttling delay ■■■■■■■■■ Waiting 5s for throttling delay
## ■■■■■■■■■■■ Waiting 5s for throttling delay ■■■■■■■■■■■■ Waiting 5s for
## throttling delay ■■■■■■■■■■■■■ Waiting 5s for throttling delay ■■■■■■■■■■■■■■■
## Waiting 5s for throttling delay ■■■■■■■■■■■■■■■ Waiting 5s for throttling delay
## ■■■■■■■■■■■■■■■■■ Waiting 5s for throttling delay ■■■■■■■■■■■■■■■■■■ Waiting 5s
## for throttling delay ■■■■■■■■■■■■■■■■■■■ Waiting 5s for throttling delay
## ■■■■■■■■■■■■■■■■■■■■ Waiting 5s for throttling delay ■■■■■■■■■■■■■■■■■■■■■■
## Waiting 5s for throttling delay ■■■■■■■■■■■■■■■■■■■■■■■ Waiting 5s for
## throttling delay ■■■■■■■■■■■■■■■■■■■■■■■■ Waiting 5s for throttling delay
## ■■■■■■■■■■■■■■■■■■■■■■■■■ Waiting 5s for throttling delay
## ■■■■■■■■■■■■■■■■■■■■■■■■■■ Waiting 5s for throttling delay
## ■■■■■■■■■■■■■■■■■■■■■■■■■■■■ Waiting 5s for throttling delay
## ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ Waiting 5s for throttling delay
## ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ Waiting 6s for throttling delay ■■■■■■ Waiting
## 6s for throttling delay ■■■■■■■ Waiting 6s for throttling delay ■■■■■■■■
## Waiting 6s for throttling delay ■■■■■■■■■ Waiting 6s for throttling delay
## ■■■■■■■■■■ Waiting 6s for throttling delay ■■■■■■■■■■■ Waiting 6s for
## throttling delay ■■■■■■■■■■■■ Waiting 6s for throttling delay ■■■■■■■■■■■■■
## Waiting 6s for throttling delay ■■■■■■■■■■■■■■ Waiting 6s for throttling delay
## ■■■■■■■■■■■■■■■ Waiting 6s for throttling delay ■■■■■■■■■■■■■■■■ Waiting 6s for
## throttling delay ■■■■■■■■■■■■■■■■■ Waiting 6s for throttling delay
## ■■■■■■■■■■■■■■■■■■ Waiting 6s for throttling delay ■■■■■■■■■■■■■■■■■■■ Waiting
## 6s for throttling delay ■■■■■■■■■■■■■■■■■■■■ Waiting 6s for throttling delay
## ■■■■■■■■■■■■■■■■■■■■■ Waiting 6s for throttling delay ■■■■■■■■■■■■■■■■■■■■■■
## Waiting 6s for throttling delay ■■■■■■■■■■■■■■■■■■■■■■■ Waiting 6s for
## throttling delay ■■■■■■■■■■■■■■■■■■■■■■■■ Waiting 6s for throttling delay
## ■■■■■■■■■■■■■■■■■■■■■■■■■ Waiting 6s for throttling delay
## ■■■■■■■■■■■■■■■■■■■■■■■■■■ Waiting 6s for throttling delay
## ■■■■■■■■■■■■■■■■■■■■■■■■■■■ Waiting 6s for throttling delay
## ■■■■■■■■■■■■■■■■■■■■■■■■■■■■ Waiting 6s for throttling delay
## ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ Waiting 6s for throttling delay
## ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
# Combinar todos los datos en un dataframe
MEX <- bind_rows(MEX_data_list)

# Procesar y resumir datos
data1 <- MEX %>%
  filter(partner_iso != "WLD") %>%
  mutate(trade_value_usd_bn = billion(primary_value)) %>%
  group_by(flow_desc, ref_year) %>%
  summarise(trade_value_usd_bn = sum(trade_value_usd_bn, na.rm = TRUE), .groups = 'drop')

# Colores institucionales
colores_flujo <- c(
  "Import" = "#9F2241",  
  "Export" = "#D4C19C"   
)

ggplot(data1, aes(x = ref_year, y = trade_value_usd_bn, color = flow_desc)) +
  geom_line(size = 1.2) +
  geom_point(size = 3) +
  scale_y_continuous(
  labels = scales::label_number(suffix = "B"),
  breaks = seq(0, 100, 20)) +
  scale_color_manual(values = colores_flujo,
                     labels = c(
                       "Import" = "Importaciones",
                       "Export" = "Exportaciones"
                     )) +
  scale_x_continuous(breaks = seq(2014, 2024, 2), expand = c(0.01, 0.01)) +
  labs(
    title = "Importaciones y Exportaciones de México (Sector automotriz)", 
    x = "Año",
    y = "Valor (miles de millones USD)",
    caption = "Data: UN Comtrade",
    color = "Flujo Comercial"
  ) +
  theme_bw(base_family = "lato") +
  theme(
    plot.title = element_text(size = 16, face = "bold", hjust = 0.5),
    axis.title = element_text(size = 12, face = "bold"),
    axis.text = element_text(size = 10),
    legend.title = element_text(size = 12),
    legend.text = element_text(size = 10),
    plot.caption = element_text(size = 10, face = "italic", hjust = 1)
  )
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

## IED MX 199 - 2024 

ied <- read_csv("Data/Flujo-anual-de-IED-en-Fabricacion-de-Automoviles-y-Camiones.csv")
## Rows: 26 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Industry Group, Year_
## dbl (3): Industry Group ID, Time ID, Investment (USD)
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Limpiar datos
ied <- ied %>%
  mutate(`Time ID` = as.numeric(`Time ID`)) %>%
  filter(!is.na(`Investment (USD)`))

# Crear gráfico
ggplot(ied, aes(x = `Time ID`, y = `Investment (USD)`)) +
  geom_line(color = color_guinda, size = 1.5) +
  geom_point(color = color_guinda, size = 4) +
  scale_y_continuous(
    labels = label_number(scale = 1e-9, suffix = "B", accuracy = 0.1),
    expand = expansion(mult = c(0, 0.1))
  ) +
  scale_x_continuous(breaks = unique(ied$`Time ID`))+
  labs(
    title = "Evolución Anual de la Inversión Extranjera Directa (IED)",
    subtitle = "Fabricación de Automóviles y Camiones en México",
    x = "Año",
    y = "Inversión (miles de mllones de USD)",
    caption = "Fuente: Secretaría de Economía"
  ) +
  theme_minimal(base_size = 14, base_family = "Arial") +
  theme(
    plot.background = element_rect(fill = color_fondo, color = NA),
    panel.grid.major = element_line(color = color_gris),
    panel.grid.minor = element_blank(),
    plot.title = element_text(size = 20, face = "bold", hjust = 0.5, color = color_texto),
    plot.subtitle = element_text(size = 14, hjust = 0.5, margin = margin(b = 15), color = color_texto),
    plot.caption = element_text(size = 10, hjust = 1, face = "italic", color = color_texto),
    axis.text.x = element_text(angle = 45, hjust = 1, color = color_texto),
    axis.text.y = element_text(color = color_texto),
    axis.title.x = element_text(margin = margin(t = 15), face = "bold", color = color_texto),
    axis.title.y = element_text(margin = margin(r = 15), face = "bold", color = color_texto)
  )
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Arial' not found, will use 'sans' instead


Situación arancelaria: el inicio de la era Trump 🇺🇸



Comercio internacional


# Top 5 socios comerciales 

# Procesamiento
top_n <- 5
yr <- 2024

top_partners <- MEX %>% 
  filter(partner_iso != "WLD", ref_year == yr) %>%
  group_by(partner_iso) %>%
  summarise(total = sum(primary_value), .groups = "drop") %>%
  arrange(desc(total)) %>%
  slice_head(n = top_n) %>%
  pull(partner_iso)

data_top <- MEX %>%
  filter(ref_year == 2024, partner_iso %in% c("USA", "CHN", "CAN", "DEU", "JPN")) %>%
  mutate(
    partner_iso = as.character(as_factor(partner_iso)),  
    trade_value_usd_bn = primary_value / 1e9,
    partner_name = dplyr::recode(partner_iso,
                                 "USA" = "Estados Unidos",
                                 "CHN" = "China",
                                 "CAN" = "Canadá",
                                 "DEU" = "Alemania",
                                 "JPN" = "Japón")
  )


# Gráfico combinado
ggplot(data_top, aes(x = reorder(partner_name, -trade_value_usd_bn), y = trade_value_usd_bn, fill = flow_desc)) +
  geom_col(position = "dodge", color = "grey20", width = 0.6) +
  geom_text(
    aes(label = number(trade_value_usd_bn, accuracy = 0.1)),
    position = position_dodge(width = 0.6),
    vjust = -0.5,
    size = 3,
    family = "lato"
  ) +
  scale_fill_manual(
    values = c("Import" = color_guinda, "Export" = color_verde),
    labels = c("Import" = "Importaciones", "Export" = "Exportaciones")
  ) +
  scale_y_continuous(
    expand = expansion(mult = c(0, 0.1)),
    labels = label_number(suffix = "B")
  ) +
  labs(
    title = "Top 5 socios comerciales de México (2024)",
    subtitle = "Importaciones y exportaciones del sector automotriz (Cód. 8703)",
    x = NULL,
    y = "Valor (miles de millones USD)",
    fill = "Flujo comercial",
    caption = "Fuente: UN Comtrade"
  ) +
  theme_minimal(base_family = "lato") +
  theme(
    plot.title = element_text(size = 16, face = "bold", hjust = 0.5),
    plot.subtitle = element_text(size = 13, hjust = 0.5, color = color_texto),
    axis.text = element_text(size = 10, color = color_texto),
    axis.title = element_text(size = 12, face = "bold", color = color_texto),
    legend.position = "bottom",
    legend.title = element_text(face = "bold"),
    plot.caption = element_text(size = 10, face = "italic", hjust = 1)
  )

# Exportaciones a Canadá, China y Estados Unidos 

# Asignar colores 
colores_paises <- c(
  "CAN" = "#FF0000",  
  "CHN" = "#FFCC00",  
  "USA" = "#0033A0"   
)

# Agregar columnas para banderas 
banderas <- c(
  "CAN" = "https://flagcdn.com/w40/ca.png",
  "CHN" = "https://flagcdn.com/w40/cn.png",
  "USA" = "https://flagcdn.com/w40/us.png"
)
# Datos
q_export <- ct_get_data(
  reporter = "MEX",
  partner = c("DEU", "USA", "JPN", "CHN", "CAN"),
  flow_direction = "Export",
  start_date = 2014,
  end_date = 2024,
  commodity_code = "8703",
  frequency = "A"
)
## Waiting 3s for throttling delay ■■■■■■■■■■ Waiting 3s for throttling delay
## ■■■■■■■■■■■■ Waiting 3s for throttling delay ■■■■■■■■■■■■■■■ Waiting 3s for
## throttling delay ■■■■■■■■■■■■■■■■■■■■■ Waiting 3s for throttling delay
## ■■■■■■■■■■■■■■■■■■■■■■■ Waiting 3s for throttling delay
## ■■■■■■■■■■■■■■■■■■■■■■■■ Waiting 3s for throttling delay
## ■■■■■■■■■■■■■■■■■■■■■■■■■■ Waiting 3s for throttling delay
## ■■■■■■■■■■■■■■■■■■■■■■■■■■■■
# Gráfico de exportaciones
q_export %>%
  filter(partner_iso %in% c("USA", "CHN", "CAN")) %>%
  mutate(
    trade_value_usd_billion = primary_value / 1e9,
    flag = banderas[partner_iso]
  ) %>%
  ggplot(aes(x = ref_year, y = trade_value_usd_billion, fill = partner_iso)) +
  geom_col(position = "dodge", alpha = 0.9) +
  
  geom_image(
    aes(image = flag),
    position = position_dodge(width = 0.9),
    size = 0.035,
    by = "width"
  ) +
  scale_fill_manual(
    values = colores_paises,
    labels = c("CAN" = "Canadá", "CHN" = "China", "USA" = "Estados Unidos")
  ) +
  scale_y_continuous(labels = label_number(suffix = "B", accuracy = 0.1)) +
  scale_x_continuous(breaks = seq(2014, 2024, 2)) +
  labs(
    title = "Exportaciones de México a Canadá, China y Estados Unidos",
    subtitle = "Sector automotriz",
    x = "Año",
    y = "Valor (miles de millones USD)",
    fill = "Socio comercial",
    caption = "Fuente: UN Comtrade"
  ) +
  theme_bw(base_family = "lato") +
  theme(
    plot.title = element_text(size = 16, face = "bold", hjust = 0.5),
    plot.subtitle = element_text(size = 14, color = color_texto, hjust = 0.5),
    axis.title = element_text(size = 12, face = "bold"),
    axis.text = element_text(size = 10),
    legend.title = element_text(size = 12),
    legend.text = element_text(size = 10),
    plot.caption = element_text(size = 10, face = "italic", hjust = 1, color = color_texto)
  )

# Importaciones desde Canadá, China y Estados 

# Datos
q <- ct_get_data(
  reporter = "MEX",
  partner = c("DEU", "USA", "JPN", "CHN", "CAN"),
  flow_direction = "Import",
  start_date = 2014,
  end_date = 2024,
  commodity_code = "8703",
  frequency = "A"
)
## Waiting 3s for throttling delay ■■■■■■■■■■ Waiting 3s for throttling delay
## ■■■■■■■■■■■ Waiting 3s for throttling delay ■■■■■■■■■■■■■■ Waiting 3s for
## throttling delay ■■■■■■■■■■■■■■■■ Waiting 3s for throttling delay
## ■■■■■■■■■■■■■■■■■■■■ Waiting 3s for throttling delay ■■■■■■■■■■■■■■■■■■■■■
## Waiting 3s for throttling delay ■■■■■■■■■■■■■■■■■■■■■■■ Waiting 3s for
## throttling delay ■■■■■■■■■■■■■■■■■■■■■■■■■■ Waiting 3s for throttling delay
## ■■■■■■■■■■■■■■■■■■■■■■■■■■■■ Waiting 3s for throttling delay
## ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
q %>%
  filter(partner_iso %in% c("USA", "CHN", "CAN")) %>%
  mutate(
    trade_value_usd_billion = primary_value / 1e9,
    flag = banderas[partner_iso]
  ) %>%
  ggplot(aes(x = ref_year, y = trade_value_usd_billion, fill = partner_iso)) +
  geom_col(position = "dodge", alpha = 0.9) +

  geom_image(
    aes(image = flag),
    position = position_dodge(width = 0.9),
    size = 0.035,  
    by = "width"
  ) +
  scale_fill_manual(
    values = colores_paises,
    labels = c("CAN" = "Canadá", "CHN" = "China", "USA" = "Estados Unidos")
  ) +
  scale_y_continuous(labels = label_number(suffix = "B", accuracy = 0.1)) +
  scale_x_continuous(breaks = seq(2014, 2024, 2)) +
  labs(
    title = "Importaciones a México desde Canadá, China y Estados Unidos",
    subtitle = "Sector automotriz",
    x = "Año",
    y = "Valor (miles de millones USD)",
    fill = "Socio comercial",
    caption = "Fuente: UN Comtrade"
  ) +
  theme_bw(base_family = "lato") +
  theme(
    plot.title = element_text(size = 16, face = "bold", hjust = 0.5),
    axis.title = element_text(size = 12, face = "bold"),
    plot.subtitle = element_text(size = 14, color = color_texto, hjust = 0.5),
    axis.text = element_text(size = 10),
    legend.title = element_text(size = 12),
    legend.text = element_text(size = 10),
    plot.caption = element_text(size = 10, face = "italic", hjust = 1, color = color_texto)
  )


Importancia para otras actividades económicas


# Relación del sector automotriz con otras actividades económicas: Gráfcia Nodos

# Cargar base de datos
mip <- read_excel("Data/MIP_44_JustSCIAN.xlsx", skip = 5)
## New names:
## • `` -> `...1`
## • `` -> `...2`
## • `Total` -> `Total...3`
## • `Total` -> `Total...82`
# Renombrar y limpiar nombres
names(mip)[1] <- "from"
colnames(mip) <- str_trim(colnames(mip))

# Seleccionar columnas SCIAN y transformar a formato largo
scian_cols <- colnames(mip)[str_detect(colnames(mip), "^\\d{3}")]

mip_long <- mip %>%
  select(from, all_of(scian_cols)) %>%
  pivot_longer(cols = -from, names_to = "to", values_to = "valor") %>%
  drop_na(valor) %>%
  filter(valor > 0) %>%
  mutate(
    from_code = str_extract(from, "^\\d{3}"),
    to_code = str_extract(to, "^\\d{3}")
  )

# Crear red dirigida
network <- as_tbl_graph(mip_long, directed = TRUE)

# Nodo clave
sector_336 <- "336 - Fabricación de equipo de transporte"

# Graficar red centrada en sector 336
network %>%
  activate(nodes) %>%
  mutate(code = str_extract(name, "^\\d{3}")) %>%
  activate(edges) %>%
  filter(
    from == which(V(.)$name == sector_336) |
      to == which(V(.)$name == sector_336)
  ) %>%
  ggraph(layout = "linear") +
  geom_edge_arc(
    aes(edge_colour = valor),
    arrow = arrow(angle = 15, length = unit(0.3, "cm"), ends = "last", type = "closed"),
    edge_width = 1.1,
    show.legend = TRUE
  ) +
  scale_edge_colour_gradient(
    low = color_guinda_claro,
    high = color_guinda,
    name = "Valor económico",
    labels = label_number(scale_cut = cut_short_scale()),
    breaks = scales::pretty_breaks(n = 4)
  ) +
  geom_node_point(color = color_guinda, size = 4) +
  geom_node_label(
    aes(label = code),
    size = 3.5,
    family = "lato",
    fill = color_fondo,
    color = color_texto,
    label.size = 0.25,
    repel = TRUE,
    max.overlaps = Inf
  ) +
  theme_graph(base_family = "lato", base_size = 14) +
  labs(
    title = "Relaciones Insumo-Producto centradas en el sector 336",
    subtitle = "Fabricación de equipo de transporte\n(Millones de Pesos a Precios Básicos)",
    caption = "Fuente: INEGI, Matriz Insumo Producto 2018"
  ) +
  theme(
    plot.background = element_rect(fill = color_fondo, color = NA),
    plot.title = element_text(size = 18, face = "bold", hjust = 0.5, color = color_texto),
    plot.subtitle = element_text(size = 13, hjust = 0.5, color = color_texto),
    plot.caption = element_text(size = 10, hjust = 1, color = color_texto),
    legend.position = "bottom",
    legend.direction = "horizontal",
    legend.box.margin = margin(t = 15, r = 0, b = 0, l = 0),
    legend.title = element_text(face = "bold", color = color_texto, family = "lato", size = 11),
    legend.text = element_text(color = color_texto, family = "lato", size = 10),
    legend.key.width = unit(1, "cm")
  )


# Top 10 sectores más vinculados económicamente al sector automotriz

# Dependencia por compras AL sector 336 (ellos venden al 336)
hacia_336 <- mip_long %>%
  filter(to_code == "336") %>%
  group_by(from) %>%
  summarise(valor = sum(valor, na.rm = TRUE)) %>%
  rename(sector = from, valor_hacia_336 = valor)

# Dependencia por ventas DESDE el sector 336 (ellos compran al 336)
desde_336 <- mip_long %>%
  filter(from_code == "336") %>%
  group_by(to) %>%
  summarise(valor = sum(valor, na.rm = TRUE)) %>%
  rename(sector = to, valor_desde_336 = valor)

# Unir ambas dependencias
dependencia_total <- full_join(hacia_336, desde_336, by = "sector") %>%
  mutate(across(starts_with("valor"), ~replace_na(., 0))) %>%
  mutate(valor_total = valor_hacia_336 + valor_desde_336) %>%
  arrange(desc(valor_total)) %>%
  slice_head(n = 10)

# Ver resultado
print(dependencia_total)
## # A tibble: 10 × 4
##    sector                            valor_hacia_336 valor_desde_336 valor_total
##    <chr>                                       <dbl>           <dbl>       <dbl>
##  1 336 - Fabricación de equipo de t…        1050959.        1050959.    2101918.
##  2 334 - Fabricación de equipo de c…         203891.          68605.     272496.
##  3 331 - Industrias metálicas básic…         222255.          12690.     234945.
##  4 461 - Comercio al por menor de a…         176569           43108.     219677.
##  5 431 - Comercio al por mayor de a…         178045.          39449.     217494.
##  6 326 - Industria del plástico y d…         191794.          10083.     201877.
##  7 333 - Fabricación de maquinaria …         143195.          32745.     175940.
##  8 335 - Fabricación de accesorios,…         105097.          29624.     134721.
##  9 332 - Fabricación de productos m…         113993.          20109.     134102.
## 10 484 - Autotransporte de carga              50055.          72534.     122589.
# Reorganizar datos en formato largo para graficar más fácil
dependencia_larga <- dependencia_total %>%
  select(sector, valor_hacia_336, valor_desde_336) %>%
  pivot_longer(
    cols = starts_with("valor_"),
    names_to = "tipo_relacion",
    values_to = "valor"
  ) %>%
  mutate(
    tipo_relacion = as.character(tipo_relacion),
    tipo_relacion = dplyr::recode(tipo_relacion,
                                  "valor_hacia_336" = "Vende al sector (proveedor)",
                                  "valor_desde_336" = "Compra al sector (cliente)"
    ),
    sector = str_trim(str_remove(sector, "^\\d{3}\\s*-\\s*"))  # Limpiar nombre del sector
  ) %>%
  group_by(sector) %>%
  mutate(valor_total = sum(valor)) %>%
  ungroup()

# Identificar los 2 sectores más vinculados (mayor valor total)
top_sectores <- dependencia_larga %>%
  group_by(sector) %>%
  summarise(valor_total = sum(valor)) %>%
  slice_max(order_by = valor_total, n = 1) %>%
  pull(sector)

# Agregar etiquetas condicionales solo para los dos sectores más grandes
dependencia_larga <- dependencia_larga %>%
  mutate(
    mostrar_label = ifelse(sector %in% top_sectores, comma(valor, accuracy = 1), NA)
  )

# Graficar
# Formato 
tema_SE <- function(base_size = 12) {
  theme_minimal(base_family = "lato", base_size = base_size) +
    theme(
      plot.background = element_rect(fill = color_fondo, color = NA),
      panel.grid.major = element_line(color = color_gris),
      panel.grid.minor = element_blank(),
      plot.title = element_text(size = base_size + 4, face = "bold", color = color_texto, hjust = 0.5),
      plot.subtitle = element_text(size = base_size + 1, color = color_texto, hjust = 0.5),
      plot.caption = element_text(size = base_size - 2, color = color_texto, hjust = 1, face = "italic"),
      axis.title = element_text(face = "bold", color = color_texto),
      axis.text = element_text(color = color_texto),
      legend.position = "bottom",
      legend.title = element_text(face = "bold", color = color_texto),
      legend.text = element_text(color = color_texto)
    )
}
  
# Añadir etiquetas centradas y envolver nombres de sector
dependencia_larga <- dependencia_larga %>%
  mutate(
    mostrar_label = ifelse(sector %in% top_sectores, comma(valor, accuracy = 1), NA),
    label_y = valor / 2,
    sector_wrapped = str_wrap(sector, width = 25)
  )

# Gráfico
ggplot(dependencia_larga, aes(x = reorder(sector_wrapped, valor_total), y = valor, fill = tipo_relacion)) +
  geom_col(position = position_dodge(width = 0.7), width = 0.6, color = "grey20") +
  geom_text(
    aes(y = label_y, label = mostrar_label),
    position = position_dodge(width = 0.7),
    size = 3,
    color = "black",
    family = "lato",
    na.rm = TRUE
  ) +
  scale_fill_manual(
    values = c(
      "Compra al sector (cliente)" = color_verde,
      "Vende al sector (proveedor)" = color_guinda
    )
  ) +
  scale_y_continuous(
    labels = label_number(scale = 1, suffix = "M", accuracy = 1),
    expand = expansion(mult = c(0, 0.05))
  ) +
  coord_flip() +
  labs(
    title = "Top 10 sectores más vinculados económicamente al sector automotriz",
    subtitle = "Relaciones económicas por compras y ventas al sector",
    x = NULL,
    y = "Millones de pesos a precios básicos",
    caption = "Fuente: INEGI, Matriz Insumo Producto 2018",
    fill = "Tipo de relación"
  ) +
  tema_SE(base_size = 12) +
  theme(
    plot.margin = margin(t = 10, r = 10, b = 10, l = 25),
    legend.key.width = unit(2, "cm"),
    axis.text.y = element_text(margin = margin(r = 5))
  )


Plan México



Regiones estratégicas


## Inversión enero - diciembre 2024 por estado 
ied_2024 <- read_csv("Data/IED-segun-entidad-federativa--Periodo-enero-a-diciembre-de-2024.csv",
                locale = locale(decimal_mark = ".", grouping_mark = ","))
## Rows: 15 Columns: 6
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): State, Industry Group
## dbl (4): State ID, Year, Industry Group ID, Investment (USD)
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
ied_limpio <- ied_2024 %>%
  filter(`Industry Group` == "Fabricación de Automóviles y Camiones",
         `Investment (USD)` >= 0) %>%
  select(State, inversion = `Investment (USD)`)

equivalencias <- tibble(
  State = c("Aguascalientes", "Baja California", "Baja California Sur", "Campeche",
            "Coahuila de Zaragoza", "Colima", "Chiapas", "Chihuahua", "Ciudad de México",
            "Durango", "Guanajuato", "Guerrero", "Hidalgo", "Jalisco", "Estado de México",
            "Michoacán de Ocampo", "Morelos", "Nayarit", "Nuevo León", "Oaxaca",
            "Puebla", "Querétaro", "Quintana Roo", "San Luis Potosí",
            "Sinaloa", "Sonora", "Tabasco", "Tamaulipas", "Tlaxcala",
            "Veracruz de Ignacio de la Llave", "Yucatán", "Zacatecas"),
  name = c("Aguascalientes", "Baja California", "Baja California Sur", "Campeche",
           "Coahuila", "Colima", "Chiapas", "Chihuahua", "Ciudad de Mexico",
           "Durango", "Guanajuato", "Guerrero", "Hidalgo", "Jalisco", "Mexico",
           "Michoacan", "Morelos", "Nayarit", "Nuevo Leon", "Oaxaca",
           "Puebla", "Queretaro", "Quintana Roo", "San Luis Potosi",
           "Sinaloa", "Sonora", "Tabasco", "Tamaulipas", "Tlaxcala",
           "Veracruz", "Yucatan", "Zacatecas")
)

ied_mapa <- ied_limpio %>%
  left_join(equivalencias, by = "State")

mexico_mapa <- ne_states(country = "Mexico", returnclass = "sf")

mapa_ied <- mexico_mapa %>%
  left_join(ied_mapa, by = c("name" = "name"))

# Extraer solo Aguascalientes y Morelos para capa destacada
destacados <- mapa_ied %>%
  filter(name %in% c("Aguascalientes", "Morelos"))

# Graficar
ggplot(mapa_ied) +
  geom_sf(aes(fill = inversion), color = "white", size = 0.2) +
  scale_fill_viridis(
    option = "plasma",
    name = "Inversión Extranjera Directa (USD)",
    direction = -1,
    labels = scales::comma
  ) +
  # Capa para destacar Aguascalientes y Morelos
  geom_sf(data = destacados, fill = NA, color = "black", size = 2.5) +  # borde guinda grueso
  geom_sf(data = destacados, aes(fill = inversion), color = NA) +        # relleno normal para mantener color
  labs(
    title = "Inversión Extranjera Directa en Fabricación de Automóviles y Camiones",
    subtitle = "Por entidad federativa en México, Enero - Diciembre 2024",
    caption = "Fuente: Secretaría de Economía"
  ) +
  theme_minimal(base_size = 12) +
  theme(
    plot.title = element_text(size = 16, face = "bold", hjust = 0.5),
    plot.subtitle = element_text(size = 12, hjust = 0.5, margin = margin(b = 10)),
    plot.caption = element_text(size = 9, hjust = 1, face = "italic"),
    legend.position = "right",
    legend.title = element_text(size = 11),
    legend.text = element_text(size = 10),
    axis.text = element_blank(),
    axis.ticks = element_blank(),
    panel.grid = element_blank()
  )

## Fabricación de Automóviles y Camiones

# Cargar datos
datos <- read_csv("Data/Distribucion-de-Unidades-economicas-segun-entidades-federativas-2019.csv")
## Rows: 16 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): State, Industry Group
## dbl (3): State ID, Industry Group ID, Economic Unit
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Seleccionar y renombrar columnas relevantes
datos_limpios <- datos %>%
  select(State, unidades = `Economic Unit`)

# Crear tabla de equivalencias entre nombres del CSV y los del shapefile
equivalencias <- tibble(
  State = c("Aguascalientes", "Baja California", "Baja California Sur", "Campeche",
            "Coahuila de Zaragoza", "Colima", "Chiapas", "Chihuahua", "Ciudad de México",
            "Durango", "Guanajuato", "Guerrero", "Hidalgo", "Jalisco", "Estado de México",
            "Michoacán de Ocampo", "Morelos", "Nayarit", "Nuevo León", "Oaxaca",
            "Puebla", "Querétaro", "Quintana Roo", "San Luis Potosí",
            "Sinaloa", "Sonora", "Tabasco", "Tamaulipas", "Tlaxcala",
            "Veracruz de Ignacio de la Llave", "Yucatán", "Zacatecas"),
  name = c("Aguascalientes", "Baja California", "Baja California Sur", "Campeche",
           "Coahuila", "Colima", "Chiapas", "Chihuahua", "Ciudad de Mexico",
           "Durango", "Guanajuato", "Guerrero", "Hidalgo", "Jalisco", "Mexico",
           "Michoacan", "Morelos", "Nayarit", "Nuevo Leon", "Oaxaca",
           "Puebla", "Queretaro", "Quintana Roo", "San Luis Potosi",
           "Sinaloa", "Sonora", "Tabasco", "Tamaulipas", "Tlaxcala",
           "Veracruz", "Yucatan", "Zacatecas")
)

# Unir equivalencias con los datos
datos_mapa <- datos_limpios %>%
  left_join(equivalencias, by = "State")

# Cargar mapa de México
mexico_mapa <- ne_states(country = "Mexico", returnclass = "sf")

# Unir datos con el shapefile
mapa_datos <- mexico_mapa %>%
  left_join(datos_mapa, by = c("name" = "name"))

# Coordenadas de fábricas Nissan
nissan_coords <- data.frame(
  lon = c(-99.17287261213131, -102.28118936691124, -102.27862730540257, -102.29207190845837),
  lat = c(18.90886934393236, 21.80447038234521, 21.7420485056283, 21.72599408991313),
  planta = c("Nissan Morelos", "Nissan A1", "Nissan A2", "Compass")
)

# Marcar los estados que queremos resaltar
mapa_datos <- mapa_datos %>%
  mutate(
    resaltar = ifelse(name %in% c("Morelos", "Aguascalientes"), "Resaltado", "Normal")
  )

ggplot(mapa_datos) +
  # Capa base del mapa
  geom_sf(aes(fill = unidades), color = "white", size = 0.2) +
  
  # Capa de resaltado con línea negra gruesa
  geom_sf(
    data = filter(mapa_datos, resaltar == "Resaltado"),
    fill = NA,
    color = "black",       
    size = 2.5,            
    show.legend = FALSE
  ) +
  
  # Fábricas Nissan como triángulos negros
  geom_jitter(data = nissan_coords, 
              aes(x = lon, y = lat, color = "Fábricas automotrices (Nissan)"), 
              shape = 17, size = 2.8, width = 0.1, height = 0.1) +
  scale_color_manual(
    name = "", 
    values = c("Fábricas automotrices (Nissan)" = "black")
  ) +
  
  # Relleno del mapa con viridis
  scale_fill_viridis(
    option = "plasma",
    name = "Unidades Económicas",
    direction = -1,
    labels = scales::comma
  ) +
  
  # Estética del gráfico
  theme_minimal(base_size = 12) +
  labs(
    title = "Distribución Estatal de Unidades Económicas en la Industria Automotriz",
    subtitle = "Fabricación de Automóviles y Camiones en México",
    caption = "Fuente: INEGI, Censos Económicos 2019"
  ) +
  theme(
    plot.title = element_text(size = 16, face = "bold", hjust = 0.5),
    plot.subtitle = element_text(size = 12, hjust = 0.5, margin = margin(b = 10)),
    plot.caption = element_text(size = 9, hjust = 1, face = "italic"),
    legend.position = "right",
    legend.title = element_text(size = 11),
    legend.text = element_text(size = 10),
    axis.text = element_blank(),
    axis.ticks = element_blank(),
    axis.title = element_blank(),
    panel.grid = element_blank()
  )


Oportunidad de desarrollo económico: Morelos y Aguascalientes


# Exportaciones 
anios <- 2018:2024

# Función para leer datos de un estado
leer_datos_estado <- function(estado) {
  nombres_archivos <- paste0(estado, "/Productos-", anios, "-Clic-en-el-grafico-para-seleccionar.csv")
  
  map2_dfr(nombres_archivos, anios, ~{
    df <- read_csv(.x)
    df$anio <- .y
    df$estado <- estado
    df
  })
}

# Leer datos de ambos estados
datos_morelos <- leer_datos_estado("Morelos")
## Rows: 159 Columns: 8
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): Chapter 4 Digit, HS2 4 Digit, HS4 4 Digit
## dbl (5): Chapter 4 Digit ID, HS2 4 Digit ID, HS4 4 Digit ID, Trade Value, Share
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 123 Columns: 8
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): Chapter 4 Digit, HS2 4 Digit, HS4 4 Digit
## dbl (5): Chapter 4 Digit ID, HS2 4 Digit ID, HS4 4 Digit ID, Trade Value, Share
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 124 Columns: 8
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): Chapter 4 Digit, HS2 4 Digit, HS4 4 Digit
## dbl (5): Chapter 4 Digit ID, HS2 4 Digit ID, HS4 4 Digit ID, Trade Value, Share
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 144 Columns: 8
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): Chapter 4 Digit, HS2 4 Digit, HS4 4 Digit
## dbl (5): Chapter 4 Digit ID, HS2 4 Digit ID, HS4 4 Digit ID, Trade Value, Share
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 114 Columns: 8
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): Chapter 4 Digit, HS2 4 Digit, HS4 4 Digit
## dbl (5): Chapter 4 Digit ID, HS2 4 Digit ID, HS4 4 Digit ID, Trade Value, Share
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 112 Columns: 8
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): Chapter 4 Digit, HS2 4 Digit, HS4 4 Digit
## dbl (5): Chapter 4 Digit ID, HS2 4 Digit ID, HS4 4 Digit ID, Trade Value, Share
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 121 Columns: 8
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): Chapter 4 Digit, HS2 4 Digit, HS4 4 Digit
## dbl (5): Chapter 4 Digit ID, HS2 4 Digit ID, HS4 4 Digit ID, Trade Value, Share
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
datos_ags <- leer_datos_estado("Aguascalientes")
## Rows: 127 Columns: 8
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): Chapter 4 Digit, HS2 4 Digit, HS4 4 Digit
## dbl (5): Chapter 4 Digit ID, HS2 4 Digit ID, HS4 4 Digit ID, Trade Value, Share
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 121 Columns: 8
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): Chapter 4 Digit, HS2 4 Digit, HS4 4 Digit
## dbl (5): Chapter 4 Digit ID, HS2 4 Digit ID, HS4 4 Digit ID, Trade Value, Share
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 111 Columns: 8
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): Chapter 4 Digit, HS2 4 Digit, HS4 4 Digit
## dbl (5): Chapter 4 Digit ID, HS2 4 Digit ID, HS4 4 Digit ID, Trade Value, Share
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 123 Columns: 8
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): Chapter 4 Digit, HS2 4 Digit, HS4 4 Digit
## dbl (5): Chapter 4 Digit ID, HS2 4 Digit ID, HS4 4 Digit ID, Trade Value, Share
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 124 Columns: 8
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): Chapter 4 Digit, HS2 4 Digit, HS4 4 Digit
## dbl (5): Chapter 4 Digit ID, HS2 4 Digit ID, HS4 4 Digit ID, Trade Value, Share
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 112 Columns: 8
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): Chapter 4 Digit, HS2 4 Digit, HS4 4 Digit
## dbl (5): Chapter 4 Digit ID, HS2 4 Digit ID, HS4 4 Digit ID, Trade Value, Share
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 122 Columns: 8
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): Chapter 4 Digit, HS2 4 Digit, HS4 4 Digit
## dbl (5): Chapter 4 Digit ID, HS2 4 Digit ID, HS4 4 Digit ID, Trade Value, Share
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Combinar
datos_comb <- bind_rows(datos_morelos, datos_ags)

# Filtrar por código producto HS2 1787
datos_filtrados <- datos_comb %>%
  filter(`HS2 4 Digit ID` == 1787)

# Colores para estados
colores <- c("Morelos" = "#800000", "Aguascalientes" = "#004B8D")

# Gráfico combinado
ggplot(datos_filtrados, aes(x = factor(anio), y = `Trade Value`, fill = estado)) +
  geom_col(position = position_dodge(width = 0.7), width = 0.6) +
  scale_fill_manual(values = colores) +
  scale_y_continuous(labels = scales::label_number(scale = 1e-6, suffix = " M", accuracy = 1),
                     expand = expansion(mult = c(0, 0.05))) +
  labs(
    title = "Exportaciones de vehículos y autopartes",
    subtitle = "Producto HS2 1787 – Evolución anual del valor exportado",
    x = "Año",
    y = "Valor de exportación (Millones USD)",
    fill = "Estado",
    caption = "Fuente: Secretaría de Economía"
  ) +
  tema_SE(base_size = 12) +
  theme(legend.position = "top")

# Participación en las exportaciones totales 
share_morelos <- datos_morelos %>%
  filter(`HS2 4 Digit ID` == 1787, !is.na(Share)) %>%
  distinct(anio, .keep_all = TRUE) %>%
  arrange(anio)

share_ags <- datos_ags %>%
  filter(`HS2 4 Digit ID` == 1787, !is.na(Share)) %>%
  distinct(anio, .keep_all = TRUE) %>%
  arrange(anio)

# Añadir columna Estado
share_morelos <- share_morelos %>% mutate(Estado = "Morelos")
share_ags <- share_ags %>% mutate(Estado = "Aguascalientes")

# Unir datasets
share_todos <- bind_rows(share_morelos, share_ags)

# Graficar comparación
ggplot(share_todos, aes(x = anio, y = Share / 100, color = Estado)) +
  geom_line(size = 1.5) +
  geom_point(size = 3.5) +
  geom_text(
    aes(label = scales::percent(Share / 100, accuracy = 0.1)),
    vjust = -1,
    size = 3.5,
    family = "lato",
    color = "black",
    show.legend = FALSE
  ) +
  scale_x_continuous(breaks = unique(share_todos$anio)) +
  scale_y_continuous(
    labels = label_percent(),
    limits = c(
      min(share_todos$Share) * 0.0095,
      max(share_todos$Share) * 0.0105
    )
  ) +
  scale_color_manual(values = c("Morelos" = "#800000", "Aguascalientes" = "#004B8D")) +
  labs(
    title = "Participación del sector automotriz en las exportaciones totales",
    subtitle = "Comparación anual Morelos vs. Aguascalientes",
    x = "Año",
    y = "Participación (%)",
    color = "Estado",
    caption = "Fuente: Secretaría de Economía"
  ) +
  tema_SE(base_size = 12)

# Parques industriales 
# Cargar la base de datos 
INEGI_DENUE_28052025 <- readr::read_csv("Data/INEGI_DENUE_28052025.csv", 
                                        locale = locale(encoding = "ISO-8859-1"))
## Rows: 3236 Columns: 42
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (35): Clee, Nombre de la Unidad Económica, Razón social, Nombre de clase...
## dbl  (7): ID, Código de la clase de actividad SCIAN, Número exterior o kilóm...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Filtrar datos para Morelos, arreglando nombre de parque CIVAC
filtrar_espacios_productivos_MOR <- INEGI_DENUE_28052025 %>%
  mutate(`Corredor industrial, centro comercial o mercado público` = 
           if_else(`Corredor industrial, centro comercial o mercado público` == "CIVAC", 
                   "CIUDAD INDUSTRIAL DEL VALLE DE CUERNAVACA", 
                   `Corredor industrial, centro comercial o mercado público`)) %>% 
  filter(grepl("IND", `Corredor industrial, centro comercial o mercado público`, ignore.case = TRUE)) %>%
  filter(`Entidad federativa` == "MORELOS") %>%
  select(`Nombre de clase de la actividad`, `Entidad federativa`)

# Filtrar datos para Aguascalientes
filtrar_espacios_productivos_AGS <- INEGI_DENUE_28052025 %>%
  filter(grepl("IND", `Corredor industrial, centro comercial o mercado público`, ignore.case = TRUE)) %>%
  filter(`Entidad federativa` == "AGUASCALIENTES") %>%
  select(`Nombre de clase de la actividad`, `Entidad federativa`)

# Añadir columna con nombre del estado para ambos datasets
datos_MOR <- filtrar_espacios_productivos_MOR %>%
  mutate(Entidad = "MORELOS")

datos_AGS <- filtrar_espacios_productivos_AGS %>%
  mutate(Entidad = "AGUASCALIENTES")

# Unir ambos data frames
datos_comparacion <- bind_rows(datos_MOR, datos_AGS)

# Agrupar y contar actividades por estado
clasificacion_actividades_comparada <- datos_comparacion %>%
  group_by(Entidad, `Nombre de clase de la actividad`) %>%
  summarise(Total = n(), .groups = "drop")

ggplot(clasificacion_actividades_comparada, 
       aes(x = fct_reorder(`Nombre de clase de la actividad`, Total), 
           y = Total, fill = Entidad)) +
  geom_bar(stat = "identity", position = "dodge") +
  coord_flip() +
  labs(
    title = "Comparación de actividades económicas en parques industriales",
    subtitle = "Aguascalientes vs. Morelos",
    x = "Actividad Económica (SCIAN)",
    y = "Número de Establecimientos",
    fill = "Entidad",
    caption = "Fuente: INEGI DENUE"
  ) +
  theme_minimal(base_size = 12) +
  theme(
    text = element_text(family = "lato"),
    plot.title = element_text(size = 16, face = "bold", hjust = 0),
    plot.subtitle = element_text(size = 12, hjust = 0, margin = margin(b = 10)), 
    plot.caption = element_text(size = 9, hjust = 0, face = "italic", margin = margin(t = 10)),  
    axis.title.y = element_text(margin = margin(r = 10)),
    axis.title.x = element_text(margin = margin(t = 10)),
    legend.position = "bottom"
  ) +
  scale_fill_manual(values = c("MORELOS" = "#800000", "AGUASCALIENTES" = "#004B8D")) +
  scale_y_continuous(labels = scales::comma)


Nissan: La GRAN oportunidad


  • Este año Nissan cerrará plantas en Tailandia India y Japón.

  • Nuevo CEO de la marca es mexicano.

  • Ya mplean a emplean a más de 15 mil personas.

  • Hay un traslado de producción de pick-ups de Argentina a plantas mexicanas.

  • Es la marca más vendida en México desde 2009.

Ya cuenta con 3 plantas en México: 2 en Aguascalientes (A1 y A2), y una en Morelos (CIVAC).


Potencial en el mercado internacional


# Exportaciones de Nissan México
# Cargar la base ----------------------------------------------------------
exportaciones = read_excel("Data/RAIAVL_10.xlsx", skip = 7, col_names = FALSE)
## New names:
## • `` -> `...1`
## • `` -> `...2`
## • `` -> `...3`
## • `` -> `...4`
names(exportaciones) = c("anio", "mes", "detalle", "exportacion")

# Limpiar exportaciones
exportaciones = exportaciones %>%
  filter(exportacion != "-" & !is.na(exportacion)) %>%
  mutate(exportacion = as.numeric(exportacion))

# Separar la columna de detalle -------------------------------------------
exportaciones = exportaciones %>%
  separate(detalle, into = c("marca", "modelo", "tipo", "segmento", "pais_destino"), sep = " - ", remove = FALSE)

# -------------------------------------------------------------------------
# Agrupamiento por país destino
exportaciones_pais = exportaciones %>%
  group_by(pais_destino) %>%
  summarise(exportaciones = sum(exportacion, na.rm = TRUE)) %>%
  drop_na()

# Matriz de clustering por país destino
exportaciones_pais_mat = exportaciones_pais %>%
  column_to_rownames(var = "pais_destino")

distancias_pais = dist(exportaciones_pais_mat, method = "euclidean")

# Clustering por país - método Single
cluster_pais_single = hclust(distancias_pais, method = "single")
plot(cluster_pais_single, main = "País Destino - Exportaciones, Nissan, 2018-2025 - Método Single", ylab = "Distancia")
rect.hclust(cluster_pais_single, k = 6, border = 2:4)

# Clustering por país - método Ward.D2
cluster_pais_ward = hclust(distancias_pais, method = "ward.D2")
plot(cluster_pais_ward, main = "País Destino - Exportaciones,Nissan, 2018-2025 - Método Ward.D2", ylab = "Distancia")
rect.hclust(cluster_pais_ward, k = 6, border = 2:4)

# 1. Cargar y limpiar datos
exportaciones <- read_excel("Data/RAIAVL_10.xlsx", skip = 7, col_names = FALSE)
## New names:
## • `` -> `...1`
## • `` -> `...2`
## • `` -> `...3`
## • `` -> `...4`
names(exportaciones) <- c("anio", "mes", "detalle", "exportacion")

exportaciones <- exportaciones %>%
  filter(exportacion != "-" & !is.na(exportacion)) %>%
  mutate(exportacion = as.numeric(exportacion)) %>%
  separate(detalle, into = c("marca", "modelo", "tipo", "segmento", "pais_destino"), sep = " - ", remove = FALSE)

# 2. Agrupar exportaciones por país destino
exportaciones_pais <- exportaciones %>%
  group_by(pais_destino) %>%
  summarise(exportaciones = sum(exportacion, na.rm = TRUE)) %>%
  drop_na()

# 3. Preparar matriz y distancia para clustering
exportaciones_pais_mat <- exportaciones_pais %>%
  column_to_rownames(var = "pais_destino")

distancias_pais <- dist(exportaciones_pais_mat, method = "euclidean")

# 4. Clustering método Ward.D2 con 6 clusters
cluster_pais_ward <- hclust(distancias_pais, method = "ward.D2")
clusters <- cutree(cluster_pais_ward, k = 6)

# 5. Añadir cluster a la tabla original
exportaciones_pais$cluster <- factor(clusters[exportaciones_pais$pais_destino])

# 6. Filtrar top 10 países por exportaciones
exportaciones_top10 <- exportaciones_pais %>%
  arrange(desc(exportaciones)) %>%
  slice_head(n = 10)

# 7. Definir colores para clusters
colores_clusters <- c(
  "#0033a0",
  "#ff7f00",
  "#009b3a",
  "#d52b1e",
  "#6a0dad",
  "grey"
)

# 8. Graficar barras coloreadas por cluster
ggplot(exportaciones_top10, aes(x = reorder(pais_destino, exportaciones), y = exportaciones, fill = cluster)) +
  geom_col() +
  coord_flip() +
  scale_fill_manual(values = colores_clusters, name = "Cluster") +
  labs(
    title = "Exportaciones por país destino (2018–2025)",
    subtitle = "Coloreados por clúster",
    x = "País destino",
    y = "Unidades exportadas",
    caption = "Fuente: INEGI"
  ) +
  theme_minimal(base_size = 12) +
  theme(
    text = element_text(family = "lato"),
    plot.title = element_text(size = 16, face = "bold", hjust = 0.5),
    plot.subtitle = element_text(size = 12, hjust = 0.5, margin = margin(b = 10)),
    plot.caption = element_text(size = 9, hjust = 1, face = "italic"),
    axis.title.y = element_text(face ="bold", margin = margin(r = 10)),
    axis.title.x = element_text(face ="bold", margin = margin(t = 10)),
    legend.position = "right"
  ) +
  scale_y_continuous(labels = scales::comma)

# -------------------------------------------------------------------------
# Agrupamiento por modelo exportado
exportaciones_modelo = exportaciones %>%
  group_by(modelo) %>%
  summarise(exportaciones = sum(exportacion, na.rm = TRUE)) %>%
  filter(exportaciones > 100) %>%
  drop_na()

# Matriz de clustering por modelo
exportaciones_modelo_mat = exportaciones_modelo %>%
  column_to_rownames(var = "modelo")

distancias_modelo = dist(exportaciones_modelo_mat, method = "euclidean")

# Clustering por modelo - método Single
cluster_modelo_single = hclust(distancias_modelo, method = "single")
plot(cluster_modelo_single, main = "Modelo(s) - Exportaciones, Nissan, 2018-2025 - Método Single", xlab = "Modelos", ylab = "Distancia")
rect.hclust(cluster_modelo_single, k = 3, border = 2:4)

# Clustering por modelo - método Ward.D2
cluster_modelo_ward = hclust(distancias_modelo, method = "ward.D2")
plot(cluster_modelo_ward, main = "Modelo(s) - Exportaciones, Nissan, 2018-2025 - Método Ward.D2", xlab = "Modelos", ylab = "Distancia")
rect.hclust(cluster_modelo_ward, k = 3, border = 2:4)

# 1. Cargar y limpiar datos (igual que antes)
exportaciones <- read_excel("Data/RAIAVL_10.xlsx", skip = 7, col_names = FALSE)
## New names:
## • `` -> `...1`
## • `` -> `...2`
## • `` -> `...3`
## • `` -> `...4`
names(exportaciones) <- c("anio", "mes", "detalle", "exportacion")

exportaciones <- exportaciones %>%
  filter(exportacion != "-" & !is.na(exportacion)) %>%
  mutate(exportacion = as.numeric(exportacion)) %>%
  separate(detalle, into = c("marca", "modelo", "tipo", "segmento", "pais_destino"), sep = " - ", remove = FALSE)

# 2. Agrupamiento por modelo exportado
exportaciones_modelo <- exportaciones %>%
  group_by(modelo) %>%
  summarise(exportaciones = sum(exportacion, na.rm = TRUE)) %>%
  filter(exportaciones > 100) %>%
  drop_na()

# 3. Matriz y distancia para clustering
exportaciones_modelo_mat <- exportaciones_modelo %>%
  column_to_rownames(var = "modelo")

distancias_modelo <- dist(exportaciones_modelo_mat, method = "euclidean")

# 4. Clustering método Ward.D2 con 3 clusters
cluster_modelo_ward <- hclust(distancias_modelo, method = "ward.D2")
clusters_modelo <- cutree(cluster_modelo_ward, k = 3)

# 5. Añadir cluster a la tabla original
exportaciones_modelo$cluster <- factor(clusters_modelo[exportaciones_modelo$modelo])

# 6. Filtrar top 10 modelos por exportaciones para graficar 
exportaciones_top10_modelo <- exportaciones_modelo %>%
  arrange(desc(exportaciones)) %>%
  slice_head(n = 10)

# 7. Definir colores para clusters 
colores_clusters_modelo <- c(
  "#6a0dad", 
  "#d52b1e", # rojo bandera de Canadá
  "#009b3a"  # verde bandera de Brasil
)

# 8. Graficar barras coloreadas por cluster
ggplot(exportaciones_top10_modelo, aes(x = reorder(modelo, exportaciones), y = exportaciones, fill = cluster)) +
  geom_col() +
  coord_flip() +
  scale_fill_manual(values = colores_clusters_modelo, name = "Cluster") +
  labs(
    title = "Modelos más exportados por Nissan (2018–2025)",
    x = "Coloreados por clúster",
    y = "Unidades exportadas",
    caption = "Fuente: INEGI"
  ) +
  theme_minimal(base_size = 12) +
  theme(
    text = element_text(family = "lato"),
    plot.title = element_text(size = 16, face = "bold", hjust = 0.5),
    plot.subtitle = element_text(size = 12, hjust = 0.5, margin = margin(b = 10)),
    plot.caption = element_text(size = 9, hjust = 1, face = "italic"),
    axis.title.y = element_text(face = "bold", margin = margin(r = 10)),
    axis.title.x = element_text(face = "bold", margin = margin(t = 10)),
    legend.position = "right"
  ) +
  scale_y_continuous(labels = scales::comma)

# Socios comerciales 

# Años
years <- 2018:2024

# Traducciones
traducciones <- c(
  "Alemania" = "Germany",
  "Bélgica" = "Belgium",
  "Brasil" = "Brazil",
  "Canadá" = "Canada",
  "Chequia" = "Czech Republic",
  "Corea del Sur" = "South Korea",
  "Eslovaquia" = "Slovakia",
  "España" = "Spain",
  "Estados Unidos" = "United States",
  "Francia" = "France",
  "Italia" = "Italy",
  "Japón" = "Japan",
  "Noruega" = "Norway",
  "Países Bajos" = "Netherlands",
  "Panamá" = "Panama",
  "Polonia" = "Poland",
  "Reino Unido" = "United Kingdom",
  "Sudáfrica" = "South Africa",
  "Suecia" = "Sweden",
  "Tailandia" = "Thailand",
  "Turquía" = "Turkey"
)

# Función para leer datos de Morelos
leer_exportaciones_morelos <- function(year) {
  file <- paste0("Morelos/Partes-y-Accesorios-de-Vehiculos-Automotores-Destinos-comerciales-", year, ".csv")
  read_csv(file, show_col_types = FALSE) %>%
    mutate(anio = year)
}

# Función para leer datos de Aguascalientes
leer_exportaciones_ags <- function(year) {
  file <- paste0("Aguascalientes/Partes-y-Accesorios-de-Vehiculos-Automotores-Destinos-comerciales-", year, ".csv")
  read_csv(file, show_col_types = FALSE) %>%
    mutate(anio = year)
}

# Leer datos
datos_morelos <- map_dfr(years, leer_exportaciones_morelos)
datos_ags <- map_dfr(years, leer_exportaciones_ags)

# Preparar datos Morelos
datos_morelos <- datos_morelos %>%
  rename(pais = Country, valor = `Trade Value`) %>%
  mutate(
    pais_ingles = if_else(pais %in% names(traducciones), traducciones[pais], pais),
    iso_a3 = countrycode(pais_ingles, origin = "country.name", destination = "iso3c")
  )

# Preparar datos Aguascalientes
datos_ags <- datos_ags %>%
  rename(pais = Country, valor = `Trade Value`) %>%
  mutate(
    pais_ingles = if_else(pais %in% names(traducciones), traducciones[pais], pais),
    iso_a3 = countrycode(pais_ingles, origin = "country.name", destination = "iso3c")
  )
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `iso_a3 = countrycode(pais_ingles, origin = "country.name",
##   destination = "iso3c")`.
## Caused by warning:
## ! Some values were not matched unambiguously: Emiratos Árabes Unidos, Filipinas, Pakistán, Perú, República Dominicana, Rusia
# Calcular promedio anual por país para ambos estados
promedio_morelos <- datos_morelos %>%
  group_by(iso_a3) %>%
  summarise(exportaciones_promedio = mean(valor, na.rm = TRUE), .groups = "drop")

promedio_ags <- datos_ags %>%
  group_by(iso_a3) %>%
  summarise(exportaciones_promedio = mean(valor, na.rm = TRUE), .groups = "drop")

# Combinar para obtener rango global de exportaciones promedio
todos_los_datos <- bind_rows(promedio_morelos, promedio_ags)

# Obtener límites de la escala
min_val <- 5e4
max_val <- 1e9 

# Obtener mapa mundial
world <- ne_countries(scale = "medium", returnclass = "sf")

# Preparar mapas con datos unidos
mapa_morelos <- world %>%
  left_join(promedio_morelos, by = "iso_a3")

mapa_ags <- world %>%
  left_join(promedio_ags, by = "iso_a3")

# Destacados (top 25% exportaciones)
destacados_morelos <- mapa_morelos %>%
  filter(exportaciones_promedio > quantile(exportaciones_promedio, 0.75, na.rm = TRUE))

destacados_ags <- mapa_ags %>%
  filter(exportaciones_promedio > quantile(exportaciones_promedio, 0.75, na.rm = TRUE))

# Función para graficar mapa con la escala fija
graficar_mapa <- function(mapa, destacados, titulo) {
  ggplot(mapa) +
    geom_sf(aes(fill = exportaciones_promedio), color = "white", size = 0.15) +
    geom_sf(data = destacados, fill = NA, color = "#800000", size = 1.2) +
    scale_fill_viridis(
      option = "plasma",
      trans = "log10",
      limits = c(min_val, max_val),
      oob = scales::squish,
      na.value = "grey90",
      name = "Exportaciones promedio (USD)",
      labels = scales::label_comma()
    ) +
    labs(
      title = titulo,
      subtitle = "Valor promedio anual de las exportaciones del sector automotriz",
      caption = "Fuente: Secretaría de Economía"
    ) +
    theme_minimal(base_family = "Lato") +
    theme(
      plot.title = element_text(size = 16, face = "bold", hjust = 0.5),
      plot.subtitle = element_text(size = 12, hjust = 0.5, margin = margin(b = 10)),
      plot.caption = element_text(size = 9, hjust = 1, face = "italic"),
      legend.position = "right",
      legend.title = element_text(size = 11),
      legend.text = element_text(size = 10),
      axis.text = element_blank(),
      axis.ticks = element_blank(),
      panel.grid = element_blank()
    )
}

# Graficar mapas
mapa_morelos_plot <- graficar_mapa(mapa_morelos, destacados_morelos, "Socios comerciales de Morelos (2018–2024)")
mapa_ags_plot <- graficar_mapa(mapa_ags, destacados_ags, "Socios comerciales de Aguascalientes (2018–2024)")

# Mostrar gráficos
print(mapa_morelos_plot)
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Lato' not found, will use 'wqy-microhei' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'wqy-microhei' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'wqy-microhei' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'wqy-microhei' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead

print(mapa_ags_plot)
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Lato' not found, will use 'wqy-microhei' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'wqy-microhei' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'wqy-microhei' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'wqy-microhei' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family 'Lato' not found, will use 'sans' instead


Potencial en el mercado nacional


# Cargar base -----------------------------------------------------------------
datos = read_excel("Data/RAIAVL_8_9.xlsx", skip = 7, col_names = FALSE)
## New names:
## • `` -> `...1`
## • `` -> `...2`
## • `` -> `...3`
## • `` -> `...4`
names(datos) = c("anio", "mes", "detalle", "ventas")

# Limpiar ventas y eliminar registros inválidos
datos = datos %>%
  filter(ventas != "-" & !is.na(ventas)) %>%
  mutate(ventas = as.numeric(ventas))

# Separar la columna de detalle ----------------------------------------------
datos = datos %>%
  separate(detalle, into = c("marca", "modelo", "tipo", "segmento", "origen", "pais_origen"), sep = " - ", remove = FALSE)

# Agrupar por país de origen --------------------------------------------------
ventas_pais = datos %>%
  group_by(pais_origen) %>%
  summarise(ventas = sum(ventas, na.rm = TRUE)) %>%
  drop_na()

# Matriz de clustering por país -----------------------------------------------
ventas_pais_mat = ventas_pais %>%
  column_to_rownames(var = "pais_origen")

distancias_pais = dist(ventas_pais_mat, method = "euclidean")

# Clustering por país - método single
cluster_pais_single = hclust(distancias_pais, method = "single")
plot(cluster_pais_single, main = "País - Venta al público y producción (México), Nissan, 2018-2025 - Single", ylab = "Distancia")
rect.hclust(cluster_pais_single, k = 3, border = 2:4)

# Clustering por país - método ward.D2
cluster_pais_ward = hclust(distancias_pais, method = "ward.D2")
plot(cluster_pais_ward, main = "País - Venta al público y producción (México), Nissan, 2018-2025 - Ward.D2", ylab = "Distancia")
rect.hclust(cluster_pais_ward, k = 3, border = 2:4)

# Filtrar top 20 países con ventas > 0
ventas_pais_top20 <- ventas_pais %>%
  filter(!is.na(ventas), ventas > 0) %>%
  arrange(desc(ventas)) %>%
  slice_head(n = 20)

# Gráfico con formato unificado
ggplot(ventas_pais_top20, aes(x = reorder(pais_origen, ventas), y = ventas)) +
  geom_col(fill = "#006341") +
  coord_flip() +
  labs(
    title = "Ventas totales por país de origen (2018-2025)",
    x = "País de origen",
    y = "Ventas",
    caption = "Fuente: INEGI"
  ) +
  theme_minimal(base_size = 12) +
  theme(
    text = element_text(family = "lato"),
    plot.title = element_text(size = 16, face = "bold", hjust = 0.5),
    plot.subtitle = element_text(size = 12, hjust = 0.5, margin = margin(b = 10)),
    plot.caption = element_text(size = 9, hjust = 1, face = "italic"),
    axis.title.y = element_text(face = "bold", margin = margin(r = 10)),
    axis.title.x = element_text(face = "bold", margin = margin(t = 10)),
    legend.position = "none"
  ) +
  scale_y_continuous(labels = scales::comma)

# Agrupar por modelo ----------------------------------------------------------
ventas_modelo = datos %>%
  group_by(modelo) %>%
  summarise(ventas = sum(ventas, na.rm = TRUE)) %>%
  filter(ventas > 100) %>%
  drop_na()

# Matriz de clustering por modelo ---------------------------------------------
ventas_modelo_mat = ventas_modelo %>%
  column_to_rownames(var = "modelo")

distancias_modelo = dist(ventas_modelo_mat, method = "euclidean")

# Clustering por modelo - método single
cluster_modelo_single = hclust(distancias_modelo, method = "single")
plot(cluster_modelo_single, main = "Modelo - Venta al público y producción (México), Nissan, 2018-2025 - Single", xlab = "Modelos", sub = "", ylab = "Distancia")
rect.hclust(cluster_modelo_single, k = 3, border = 2:4)

# Clustering por modelo - método ward.D2
cluster_modelo_ward = hclust(distancias_modelo, method = "ward.D2")
plot(cluster_modelo_ward, main = "Modelo - Venta al público y producción (México), Nissan, 2018-2025 - Ward.D2", xlab = "Modelos", sub = "", ylab = "Distancia")
rect.hclust(cluster_modelo_ward, k = 3, border = 2:4)

# Filtrar top 20 modelos con ventas > 0
ventas_top20 <- ventas_modelo %>%
  filter(!is.na(ventas), ventas > 0) %>%
  arrange(desc(ventas)) %>%
  slice_head(n = 20)

# Gráfico con formato unificado
ggplot(ventas_top20, aes(x = reorder(modelo, ventas), y = ventas)) +
  geom_col(fill = "#D62728") +
  coord_flip() +
  labs(
    title = "Ventas por modelo (2018-2025)",
    x = "Modelo",
    y = "Unidades vendidas",
    caption = "Fuente: INEGI"
  ) +
  theme_minimal(base_size = 12) +
  theme(
    text = element_text(family = "lato"),
    plot.title = element_text(size = 16, face = "bold", hjust = 0.5),
    plot.subtitle = element_text(size = 12, hjust = 0.5, margin = margin(b = 10)),
    plot.caption = element_text(size = 9, hjust = 1, face = "italic"),
    axis.title.y = element_text(face = "bold", margin = margin(r = 10)),
    axis.title.x = element_text(face = "bold", margin = margin(t = 10)),
    legend.position = "none"
  ) +
  scale_y_continuous(labels = scales::comma)


Recomendaciones puntuales para la industria automotriz ✅


  • Atraer toda la relocalización de producción de Nissan a Morelos y Aguascalientes.
  • Explotar las ventajas comparativas y el potencial industrial de Morelos y Aguascalientes.
  • Canadá deber ser una prioridad como socio comercial por su alto consumo de vehículos Nissan, la relación económica existente y el T-MEC.
  • Sudamérica representa un nuevo mercado para diversificar nuestras exportaciones, especialmente Colombia, Chile y Brasil.


Beneficios para México 🇲🇽


  • Derrama económica hacia otros sectores fuertemente relacionados con el sector automotriz.
  • Desarrollo no solo económico, si no también social en Morelos y Aguascalientes.
  • Creación de empleos calificados.
  • Diversificación de exportaciones.
  • Reducción de importaciones.