1 Introducción

A continuación, se presenta una forma fácil de mapear a la población total de la Ciudad de México a nivel de Área geoestadística Básica (AGEB) utilizando datos del Censo de Población y Vivienda 2020 y del Marco Geoestadístico del Instituto Nacional de Estadística y Geografía (INEGI).


Descarga de Datos Antes de comenzar hay que descargar los archivos SHP del marco geoestadistico del INEGI https://www.inegi.org.mx/app/biblioteca/ficha.html?upc=889463807469

Después de la descarga hay que seleccionar los archivos correspondientes a la Ciudad de México con clave 09

Asimismo, se descargan los datos de la población a nivel AGEB de la página: https://www.inegi.org.mx/programas/ccpv/2020/#microdatos

2 Cargar librerías

Se recuerda que hay que instalar los programas de cada una de las librerías.

library(tmap)       # Dibujar el mapa
library(sf)         # Leer shapefiles
library(pryr)       # Tamaño de objetos
library(readr)      # Cargar CSV
library(reshape2)   # dcast
library(dplyr)      # joins
library(shiny)      #crea aplicaciones we
library(shinyjs)    #funciones javascript
library(kableExtra) #tablas html
library(colorblindcheck) #paletas de colores
library(plotly)     #grafico interactivo

3 Directorio de trabajo

En una carpeta hay que colocar los shapefiles y las bases de datos que se utilizaran.

setwd("C:/Users/ad016807/Downloads/cartografia_cdmx")

4 Importar el shapefile de la AGEB (09a) y el de Municipio

# Urbana

shp_ageb <- st_read(
"C:/Users/ad016807/Downloads/cartografia_cdmx/conjunto_de_datos/09a.shp",
layer = "09a",
options = "ENCODING=WINDOWS-1252"
)
## options:        ENCODING=WINDOWS-1252 
## Reading layer `09a' from data source 
##   `C:\Users\ad016807\Downloads\cartografia_cdmx\conjunto_de_datos\09a.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 2431 features and 5 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: 2778446 ymin: 794914.6 xmax: 2820181 ymax: 846749.5
## Projected CRS: MEXICO_ITRF_2008_LCC
shp_mun<- st_read("C:/Users/ad016807/Downloads/cartografia_cdmx/conjunto_de_datos/09mun.shp", layer="09mun", options= "ENCODING=WINDOWS-1252")
## options:        ENCODING=WINDOWS-1252 
## Reading layer `09mun' from data source 
##   `C:\Users\ad016807\Downloads\cartografia_cdmx\conjunto_de_datos\09mun.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 16 features and 4 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: 2776218 ymin: 786788.9 xmax: 2820850 ymax: 846749.5
## Projected CRS: MEXICO_ITRF_2008_LCC

5 Importar datos del Censo a nivel AGEB

ageb2020 <- read.csv(
"C:/Users/ad016807/Downloads/cartografia_cdmx/RESAGEBURB_09CSV20_2.csv"
)

head(ageb2020 [,1:10])
##   ENTIDAD          NOM_ENT MUN      NOM_MUN LOC      NOM_LOC AGEB MZA
## 1       9 Ciudad de México   2 Azcapotzalco   1 Azcapotzalco 0010   1
## 2       9 Ciudad de México   2 Azcapotzalco   1 Azcapotzalco 0010   2
## 3       9 Ciudad de México   2 Azcapotzalco   1 Azcapotzalco 0010   3
## 4       9 Ciudad de México   2 Azcapotzalco   1 Azcapotzalco 0010   4
## 5       9 Ciudad de México   2 Azcapotzalco   1 Azcapotzalco 0010   5
## 6       9 Ciudad de México   2 Azcapotzalco   1 Azcapotzalco 0010   6
##          CVEGEO POBTOT
## 1 0900200010010    159
## 2 0900200010010    145
## 3 0900200010010    124
## 4 0900200010010    158
## 5 0900200010010    154
## 6 0900200010010    153

6 Unir shapefiles de AGEB con datos del Censo 2020

ageb_cdmx <- merge(
shp_ageb,
ageb2020,
by = "CVEGEO"
)

7 Mapa temático. Población total por AGEB

tmap_mode("plot")
## ℹ tmap modes "plot" - "view"
## ℹ toggle with `tmap::ttm()`
tm_shape(ageb_cdmx) +
  tm_polygons(
    fill = "POBTOT",
    id = "AGEB",
    fill.scale = tm_scale_intervals(
      style = "quantile",
      values = "brewer.rd_pu"
    ),
    fill.legend = tm_legend(title = "POBLACIÓN TOTAL")
  ) +
  tm_borders(col = "gray", fill_alpha = 0.07)+
  
  tm_shape(shp_ageb) +
  tm_borders(
    col = "gray",
    lwd = 1,
    fill_alpha = 0.2
  ) +
  tm_title(
    "2020. CDMX. POBLACIÓN TOTAL A NIVEL AGEB",
    position = c("left", "bottom")
  )
## [plot mode] fit legend/component: Some legend items or map compoments do not
## fit well, and are therefore rescaled.
## ℹ Set the tmap option `component.autoscale = FALSE` to disable rescaling.