Objetivo

Vamos a hacer un mapa temÔtico (choropleth map en la jerga técnica) con la densidad poblacional por comuna de la Región del Biobío, en base a la información disponible en geodatos abiertos del INE Chile.

Pasos previos

Lo primero es descargar los shapefile disponibles en geodatos abiertos. A la fecha de realización de este documento, estÔn disponibles en https://www.ine.gob.cl/herramientas/portal-de-mapas/geodatos-abiertos, Sección Cartografía > Censo 2017 > División Político Administrativa Y Censal > SHP.

IMPORTANTE: No olvidar descargar el archivo con el diccionario de variables.

Cargar las librerĆ­as necesarias

Se utilizarĆ” tidyverse, ggthemes, sf

library(tidyverse)
library(sf)
library(ggthemes)

Leer la información

Es importante setear el directorio de trabajo correctamente, luego se leen las capas previamente descomprimidas, quedaron en una carpeta ā€œ/DATA/biobio_shape/ā€, que estĆ” en el directorio de trabajo.

capa_chile<-st_read(paste0(getwd(),"/DATA/biobio_shape/"))##leemos las capas , las cuales quedaron en una carpeta data
## Reading layer `Comuna_Densid_Superficie' from data source 
##   `C:\Users\jugarces\OneDrive - Instituto Nacional de Estadisticas\Colaboracion con academia\DATA\biobio_shape' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 345 features and 22 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -12184460 ymin: -7666223 xmax: -7393365 ymax: -1978847
## Projected CRS: WGS 84 / Pseudo-Mercator

Ahora, y en conjunto con el diccionario de variables, podemos ver que capa_chile tiene mucha información, no solo la información sobre la geometría de las capas.

str(capa_chile)
## Classes 'sf' and 'data.frame':   345 obs. of  23 variables:
##  $ REGION    : chr  "12" "2" "12" "11" ...
##  $ NOM_REGION: chr  "REGIƓN DE MAGALLANES Y DE LA ANTƁRTICA CHILENA" "REGIƓN DE ANTOFAGASTA" "REGIƓN DE MAGALLANES Y DE LA ANTƁRTICA CHILENA" "REGIƓN DE AYSƉN DEL GENERAL CARLOS IBƁƑEZ DEL CAMPO" ...
##  $ PROVINCIA : chr  "121" "22" "123" "113" ...
##  $ NOM_PROVIN: chr  "MAGALLANES" "EL LOA" "TIERRA DEL FUEGO" "CAPITƁN PRAT" ...
##  $ COMUNA    : chr  "12102" "2202" "12303" "11303" ...
##  $ NOM_COMUNA: chr  "LAGUNA BLANCA" "OLLAGÜE" "TIMAUKEL" "TORTEL" ...
##  $ T_HOM_R   : num  191 207 343 285 522 374 412 619 494 499 ...
##  $ T_MUJ_R   : num  83 114 62 238 95 251 272 180 358 427 ...
##  $ T_POB_R   : num  274 321 405 523 617 625 684 799 852 926 ...
##  $ T_HOM_U   : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ T_MUJ_U   : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ T_POB_U   : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ T_HOM     : num  191 207 343 285 522 374 412 619 494 499 ...
##  $ T_MUJ     : num  83 114 62 238 95 251 272 180 358 427 ...
##  $ T_POB     : num  274 321 405 523 617 625 684 799 852 926 ...
##  $ T_VIV_U   : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ T_VIV_R   : num  285 439 214 320 231 363 697 372 606 445 ...
##  $ T_VIV     : num  285 439 214 320 231 363 697 372 606 445 ...
##  $ SUPERFICIE: num  3497 2921 11019 20106 9159 ...
##  $ Densidad_ : num  0.0784 0.1099 0.0368 0.026 0.0674 ...
##  $ SHAPE_Leng: num  509763 308998 2862313 12963181 6348059 ...
##  $ SHAPE_Area: num  9.34e+09 3.39e+09 3.20e+10 4.49e+10 2.51e+10 ...
##  $ geometry  :sfc_MULTIPOLYGON of length 345; first list element: List of 1
##   ..$ :List of 1
##   .. ..$ : num [1:137, 1:2] -7889811 -7889368 -7894351 -7894316 -7894240 ...
##   ..- attr(*, "class")= chr [1:3] "XY" "MULTIPOLYGON" "sfg"
##  - attr(*, "sf_column")= chr "geometry"
##  - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA NA NA NA ...
##   ..- attr(*, "names")= chr [1:22] "REGION" "NOM_REGION" "PROVINCIA" "NOM_PROVIN" ...

Mapa de Chilito

Con la información de las capas, vemos el mapa de Chile y sus islas.

ggplot(capa_chile)+geom_sf()

## Densidad de población Región del Biobío

Nos quedamos con la información que nos interesa, que es la asociada a la Región del Biobío, para ello la filtramos. Luego, creamos el grÔfico con la ayuda de ggplot2 y sf.

capa_biobio<-filter(capa_chile, REGION== "8"  )
 
###
ggplot(capa_biobio)+geom_sf(aes(fill=Densidad_) )+  
  geom_sf_text(data = capa_biobio, aes(label = NOM_COMUNA ), size=1.5)+
  scale_fill_distiller(palette = "Spectral")+labs(x="Longitud", y="Latitud")+ggtitle("Densidad de Población por comuna")