Objetivo

Este script muestra un Caso de Estudio de aplicación de programación en R para la visualización de datos referentes al Padron Vehicular publicado por el INEGI. El objetivo es crear gráficas que representen el crecimiento del número total de vehículos para una entidad federativa y un período de tiempo dados. Actualmente el perído disponible para consulta va de 1980 a 2020, y el nivel de desagregación es el de Municipio.

Descarga de información

Librerías

library(readr)
library(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(ggplot2)

Las bases de datos necesarias pueden ser descargadas desde la página: https://www.inegi.org.mx/programas/vehiculosmotor/

url_autos <- "https://www.inegi.org.mx/contenidos/programas/vehiculosmotor/datosabiertos/vmrc_anual_csv.zip"
file_autos <- "vmrc_anual_csv.zip"

if (!file.exists(file_autos)) {
      download.file(url_autos, file_autos, mode = "wb")
}

if (!dir.exists("vmrc_anual_csv")) {
      unzip(file_autos)
}

Selección de acuerdo a período de tiempo

carga_base_autos <- function(a_inicio, a_final) {
      df_vehiculos <- NULL
      for (anio in c(a_inicio:a_final)) {
            file_nom <- paste("vmrc_", anio,".csv", sep = "")
            setwd("conjunto_de_datos")
            df_vehiculos <- rbind(read_csv(file_nom, na = "*"), df_vehiculos)
            setwd("D:/WRI/comunidad_R/Ejercios")
      }
      return(df_vehiculos)
}

df_veh_periodo <- carga_base_autos(2010, 2020) # Modificar años según lo requerido
## Rows: 2352 Columns: 18
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr  (5): PROD_EST, COBERTURA, ID_ENTIDAD, ID_MUNICIPIO, ESTATUS
## dbl (13): ANIO, AUTO_OFICIAL, AUTO_PUBLICO, AUTO_PARTICULAR, CAM_PAS_OFICIAL...
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 2353 Columns: 18
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr  (5): PROD_EST, COBERTURA, ID_ENTIDAD, ID_MUNICIPIO, ESTATUS
## dbl (13): ANIO, AUTO_OFICIAL, AUTO_PUBLICO, AUTO_PARTICULAR, CAM_PAS_OFICIAL...
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 2353 Columns: 18
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr  (5): PROD_EST, COBERTURA, ID_ENTIDAD, ID_MUNICIPIO, ESTATUS
## dbl (13): ANIO, AUTO_OFICIAL, AUTO_PUBLICO, AUTO_PARTICULAR, CAM_PAS_OFICIAL...
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 2353 Columns: 18
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr  (5): PROD_EST, COBERTURA, ID_ENTIDAD, ID_MUNICIPIO, ESTATUS
## dbl (13): ANIO, AUTO_OFICIAL, AUTO_PUBLICO, AUTO_PARTICULAR, CAM_PAS_OFICIAL...
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 2357 Columns: 18
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr  (5): PROD_EST, COBERTURA, ID_ENTIDAD, ID_MUNICIPIO, ESTATUS
## dbl (13): ANIO, AUTO_OFICIAL, AUTO_PUBLICO, AUTO_PARTICULAR, CAM_PAS_OFICIAL...
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 2356 Columns: 18
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr  (5): PROD_EST, COBERTURA, ID_ENTIDAD, ID_MUNICIPIO, ESTATUS
## dbl (13): ANIO, AUTO_OFICIAL, AUTO_PUBLICO, AUTO_PARTICULAR, CAM_PAS_OFICIAL...
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 2356 Columns: 18
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr  (5): PROD_EST, COBERTURA, ID_ENTIDAD, ID_MUNICIPIO, ESTATUS
## dbl (13): ANIO, AUTO_OFICIAL, AUTO_PUBLICO, AUTO_PARTICULAR, CAM_PAS_OFICIAL...
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 2361 Columns: 18
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr  (5): PROD_EST, COBERTURA, ID_ENTIDAD, ID_MUNICIPIO, ESTATUS
## dbl (13): ANIO, AUTO_OFICIAL, AUTO_PUBLICO, AUTO_PARTICULAR, CAM_PAS_OFICIAL...
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 2365 Columns: 18
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr  (5): PROD_EST, COBERTURA, ID_ENTIDAD, ID_MUNICIPIO, ESTATUS
## dbl (13): ANIO, AUTO_OFICIAL, AUTO_PUBLICO, AUTO_PARTICULAR, CAM_PAS_OFICIAL...
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 2365 Columns: 18
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr  (5): PROD_EST, COBERTURA, ID_ENTIDAD, ID_MUNICIPIO, ESTATUS
## dbl (13): ANIO, AUTO_OFICIAL, AUTO_PUBLICO, AUTO_PARTICULAR, CAM_PAS_OFICIAL...
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 2365 Columns: 18
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr  (5): PROD_EST, COBERTURA, ID_ENTIDAD, ID_MUNICIPIO, ESTATUS
## dbl (13): ANIO, AUTO_OFICIAL, AUTO_PUBLICO, AUTO_PARTICULAR, CAM_PAS_OFICIAL...
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.

Filtrado de acuerdo a Entidad Federativa

El data frame generado contiene la información completa de la base de datos de INEGI para el período de tiempo indicado. En el siguiente paso uniremos a este data frame, las etiqueteas de nombre para cada Entidad Federativa. Esto para facilitar la selección de los datos si no se conoce la clave de entidad que utiliza INEGI.

claves_entidades <- read_csv("catalogos/tc_entidad.csv")
## Rows: 33 Columns: 2
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (2): ID_ENTIDAD, NOM_ENTIDAD
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
df_veh_periodo <- left_join(df_veh_periodo, claves_entidades)
## Joining, by = "ID_ENTIDAD"
filtra_Entidad <- function(Entidad) {
      filter(df_veh_periodo, NOM_ENTIDAD == Entidad)
}
df_veh_Entidad <- filtra_Entidad("Jalisco") # Moficar nombre de Entidad según lo requerido.

Agrupación de data frame y generación de resumen

df_veh_Entidad <- group_by(df_veh_Entidad, ANIO)
vehPart_Entidad <- summarise(df_veh_Entidad, TOT_VEH_PAR = sum(AUTO_PARTICULAR))

Genera tu gráfica

nom_edo <- unique(df_veh_Entidad$NOM_ENTIDAD)

ggplot(vehPart_Entidad, aes(x=as.numeric(ANIO), y=TOT_VEH_PAR/1000)) +
      geom_smooth()+
      #geom_line(lty="dotted",color='red', size=0.75) +
      geom_point(shape=24, fill="red", size=2) +
      labs(title = paste("Evolución del Parque Vehicular del Estado de", nom_edo),
          x="Año", y="Número de vehíclos en Miles", caption = "Fuente de Datos: INEGI")
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'