En esta prĆ”ctica se utilizarĆ” el lenguaje de programación R y la librerĆa sf para la lectura y procesamiento de información geogrĆ”fica proveniente del Marco GeoestadĆstico Nacional del INEGI. A partir de esta información se construirĆ” un mapa de localización de la Ciudad de MĆ©xico.
# Borra todos los objetos del entorno
rm(list = ls())
# Libera memoria
gc()
## used (Mb) gc trigger (Mb) max used (Mb)
## Ncells 543671 29.1 1210115 64.7 686460 36.7
## Vcells 993539 7.6 8388608 64.0 1876162 14.4
# Muestra en quƩ carpeta estƔs trabajando
getwd()
## [1] "C:/Users/brand/Desktop/iCloudDrive/DESK2025SAID/CLASES 20262/LABORATORIO 8/MARKDOWNL8"
#Ubicar carpeta en la pestaƱa de Session > Set working > Choose
install.packages(c("sf", "ggplot2", "ggspatial", "dplyr", "jpeg", "cowplot", "prettymapr", "raster"))
library(sf)
## Warning: package 'sf' was built under R version 4.4.3
## Linking to GEOS 3.13.0, GDAL 3.10.1, PROJ 9.5.1; sf_use_s2() is TRUE
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.4.3
library(ggspatial)
## Warning: package 'ggspatial' was built under R version 4.4.3
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.4.3
##
## Adjuntando el paquete: '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.4.3
library(cowplot)
## Warning: package 'cowplot' was built under R version 4.4.3
library(prettymapr)
## Warning: package 'prettymapr' was built under R version 4.4.3
library(raster)
## Warning: package 'raster' was built under R version 4.4.3
## Cargando paquete requerido: sp
## Warning: package 'sp' was built under R version 4.4.3
##
## Adjuntando el paquete: 'raster'
## The following object is masked from 'package:dplyr':
##
## select
cdmx <- st_read("CDMXMUN.shp", quiet = TRUE)
cdmx
## 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
## First 10 features:
## CVEGEO CVE_ENT CVE_MUN NOMGEO geometry
## 1 09003 09 003 CoyoacƔn POLYGON ((2800219 820661.6,...
## 2 09007 09 007 Iztapalapa POLYGON ((2812453 823708.1,...
## 3 09008 09 008 La Magdalena Contreras POLYGON ((2792518 818323.1,...
## 4 09009 09 009 Milpa Alta POLYGON ((2814877 806710.5,...
## 5 09010 09 010 Ćlvaro Obregón POLYGON ((2794396 824857.6,...
## 6 09011 09 011 TlƔhuac POLYGON ((2816579 817396, 2...
## 7 09012 09 012 Tlalpan POLYGON ((2793795 814557.6,...
## 8 09013 09 013 Xochimilco POLYGON ((2804015 816753, 2...
## 9 09006 09 006 Iztacalco POLYGON ((2808146 826363.2,...
## 10 09015 09 015 CuauhtƩmoc POLYGON ((2800483 832381.3,...
nombre_alc <- "NOMGEO"
cdmx <- st_transform(cdmx, 3857)
cdmx
## Simple feature collection with 16 features and 4 fields
## Geometry type: POLYGON
## Dimension: XY
## Bounding box: xmin: -11061250 ymin: 2160615 xmax: -11013980 ymax: 2224849
## Projected CRS: WGS 84 / Pseudo-Mercator
## First 10 features:
## CVEGEO CVE_ENT CVE_MUN NOMGEO geometry
## 1 09003 09 003 CoyoacƔn POLYGON ((-11035576 2196958...
## 2 09007 09 007 Iztapalapa POLYGON ((-11022512 2199947...
## 3 09008 09 008 La Magdalena Contreras POLYGON ((-11043805 2194621...
## 4 09009 09 009 Milpa Alta POLYGON ((-11020314 2181735...
## 5 09010 09 010 Ćlvaro Obregón POLYGON ((-11041674 2201566...
## 6 09011 09 011 TlƔhuac POLYGON ((-11018269 2193109...
## 7 09012 09 012 Tlalpan POLYGON ((-11042527 2190571...
## 8 09013 09 013 Xochimilco POLYGON ((-11031627 2192700...
## 9 09006 09 006 Iztacalco POLYGON ((-11027031 2202880...
## 10 09015 09 015 CuauhtƩmoc POLYGON ((-11035046 2209482...
bbox_cdmx <- st_bbox(cdmx)
bbox_cdmx
## xmin ymin xmax ymax
## -11061252 2160615 -11013983 2224849
plot(st_geometry(cdmx))
mapa_road <- ggplot() +
annotation_map_tile(type = "cartolight", zoom = 11) +
geom_sf(
data = cdmx,
fill = "grey60",
color = "black",
alpha = 0.45,
linewidth = 0.4
) +
geom_sf_text(
data = cdmx,
aes(label = NOMGEO),
size = 3,
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 de la Ciudad de México",
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: 11
ggsave(
filename = "Mapa_CDMX.png",
plot = mapa_road,
width = 13,
height = 8.5,
units = "in",
dpi = 300
)