1 Progrmación de SIG en R Studio

1.1 Descripción

El objetivo es que el estudiante no solo replique el código, sino que comprenda el papel de cada comando en la construcción de un mapa y su utilidad para el anÔlisis espacial aplicado a la economía.


2 Preparar el ambiente de trabajo

2.1 Limpiar el entorno

# Borra todos los objetos del entorno
rm(list = ls())
# Libera memoria
gc()
##          used (Mb) gc trigger (Mb) max used (Mb)
## Ncells 531154 28.4    1187390 63.5   660385 35.3
## Vcells 966735  7.4    8388608 64.0  1769426 13.5
# Muestra en quƩ carpeta estƔs trabajando
getwd()
## [1] "C:/Users/brand/iCloudDrive/DESK2025SAID/CLASES 20262/LABORATORIO 8/practica12EXAMEN"
#Ubicar carpeta en la pestaƱa de Session > Set working > Choose

3 Instalar y cargar paqueterĆ­as

3.1 Instalar paqueterĆ­as

install.packages(c("sf", "ggplot2", "ggspatial", "dplyr", "jpeg", "cowplot", "prettymapr", "raster"))

3.2 Cargar paqueterĆ­as

library(sf)
## Warning: package 'sf' was built under R version 4.3.3
## Linking to GEOS 3.11.2, GDAL 3.8.2, PROJ 9.3.1; sf_use_s2() is TRUE
library(ggplot2)
library(ggspatial)
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.3.3
## 
## 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(jpeg)
## Warning: package 'jpeg' was built under R version 4.3.3
library(cowplot)
library(prettymapr)
## Warning: package 'prettymapr' was built under R version 4.3.3
library(raster)
## Warning: package 'raster' was built under R version 4.3.3
## Loading required package: sp
## Warning: package 'sp' was built under R version 4.3.3
## 
## Attaching package: 'raster'
## The following object is masked from 'package:dplyr':
## 
##     select

4 Cargar archivo shapefile

4.1 cargar shp

michoacan <- st_read("MICHMUN.shp", quiet = TRUE)
michoacan
## Simple feature collection with 113 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 2317015 ymin: 658225.4 xmax: 2702183 ymax: 932180.3
## Projected CRS: MEXICO_ITRF_2008_LCC
## First 10 features:
##    CVEGEO CVE_ENT CVE_MUN             NOMGEO                       geometry
## 1   16078      16     078     Santa Ana Maya MULTIPOLYGON (((2599874 899...
## 2   16079      16     079 Salvador Escalante MULTIPOLYGON (((2523311 833...
## 3   16080      16     080            Senguio MULTIPOLYGON (((2678579 871...
## 4   16081      16     081          Susupuato MULTIPOLYGON (((2671282 814...
## 5   16082      16     082          TacƔmbaro MULTIPOLYGON (((2564764 824...
## 6   16083      16     083          TancĆ­taro MULTIPOLYGON (((2465103 837...
## 7   16013      16     013          CarƔcuaro MULTIPOLYGON (((2598735 799...
## 8   16012      16     012         Buenavista MULTIPOLYGON (((2441049 826...
## 9   16099      16     099           Tuzantla MULTIPOLYGON (((2646257 827...
## 10  16106      16     106          YurƩcuaro MULTIPOLYGON (((2478394 928...

5 Preparar la capa geogrƔfica

5.1 Identificar campo de los municipios

nombre_mun <- "NOMGEO"
michoacan <- st_transform(michoacan, 3857)
michoacan
## Simple feature collection with 113 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -11548080 ymin: 2027411 xmax: -11138970 ymax: 2319831
## Projected CRS: WGS 84 / Pseudo-Mercator
## First 10 features:
##    CVEGEO CVE_ENT CVE_MUN             NOMGEO                       geometry
## 1   16078      16     078     Santa Ana Maya MULTIPOLYGON (((-11247893 2...
## 2   16079      16     079 Salvador Escalante MULTIPOLYGON (((-11329794 2...
## 3   16080      16     080            Senguio MULTIPOLYGON (((-11164166 2...
## 4   16081      16     081          Susupuato MULTIPOLYGON (((-11172638 2...
## 5   16082      16     082          TacƔmbaro MULTIPOLYGON (((-11285745 2...
## 6   16083      16     083          TancĆ­taro MULTIPOLYGON (((-11391715 2...
## 7   16013      16     013          CarƔcuaro MULTIPOLYGON (((-11249806 2...
## 8   16012      16     012         Buenavista MULTIPOLYGON (((-11417261 2...
## 9   16099      16     099           Tuzantla MULTIPOLYGON (((-11199086 2...
## 10  16106      16     106          YurƩcuaro MULTIPOLYGON (((-11377715 2...
bbox_michoacan <- st_bbox(michoacan)
bbox_michoacan
##      xmin      ymin      xmax      ymax 
## -11548075   2027411 -11138966   2319831
plot(st_geometry(michoacan))


6 Construcción del mapa

6.1 Mapa con cuadrĆ­cula de coordenadas

mapa_road <- ggplot() +

  annotation_map_tile(type = "cartolight", zoom = 8) +

  geom_sf(
    data = michoacan,
    fill = "grey60",
    color = "black",
    alpha = 0.45,
    linewidth = 0.3
  ) +

  geom_sf_text(
    data = michoacan,
    aes(label = NOMGEO),
    size = 2,
    color = "white",
    fontface = "bold"
  ) +

  annotation_scale(
    location = "bl",
    style = "bar",
    text_cex = 0.7
  ) +

  annotation_north_arrow(
    location = "tr",
    which_north = "true",
    style = north_arrow_fancy_orienteering()
  ) +

  coord_sf(
    crs = 4326,
    label_graticule = "SW",
    expand = FALSE
  ) +

  labs(
    title = "Mapa de localización municipal de MichoacÔn de Ocampo",
    caption = "Fuente: Elaboración propia con base en el Marco Geoestadístico Nacional, INEGI 2022"
  ) +

  theme_minimal() +

  theme(
    panel.grid.major = element_line(color = "grey70", linewidth = 0.4),
    panel.grid.minor = element_blank(),
    plot.title = element_text(size = 18, face = "bold", hjust = 0.5),
    plot.caption = element_text(size = 9, hjust = 0.5),
    axis.title = element_blank()
  )

mapa_road
## Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
## give correct results for longitude/latitude data
## Zoom: 8
## Fetching 9 missing tiles
##   |                                                                              |                                                                      |   0%  |                                                                              |========                                                              |  11%  |                                                                              |================                                                      |  22%  |                                                                              |=======================                                               |  33%  |                                                                              |===============================                                       |  44%  |                                                                              |=======================================                               |  56%  |                                                                              |===============================================                       |  67%  |                                                                              |======================================================                |  78%  |                                                                              |==============================================================        |  89%  |                                                                              |======================================================================| 100%
## ...complete!


7 Exportar mapa

7.1 Guardar mapa en PNG

ggsave(
  filename = "Mapa_MICHOACAN.png",
  plot = mapa_road,
  width = 13,
  height = 8.5,
  units = "in",
  dpi = 300
)