Este es mi segundo cuaderno escrito usando R Markdown. Dicho documento ilustra el proceso llevado a cabo para realizar un mapa con polígonos que representan las ciudades de Colombia. La base de datos empleada para este fin se encuentra en el shapefile del DANE.
Para comenzar, se cargan las librerías:
#install.packages(c("rgeos", "sf"))
library(rgeos)
library(sf)
library(leaflet)
Ahora, se lee el dataset ciudades pero esta vez, es una tabla:
cities <- read.table(file= "C:/Users/LUISA CARRION/Downloads/ciudades.txt", header=FALSE, sep=";")
class(cities)
[1] "data.frame"
head(cities)
names(cities) <- c("ID", "Ciudad", "Latitud", "Longitud", "Altitud")
cities
Ahora, sse convierte el dataframe a un objeto espacial:
## el orden de las coordenadas: (i) longitud, (ii) latitud, (iii) altitud
m <- st_as_sf(cities, coords = c(5,4,6))
m
Simple feature collection with 100 features and 3 fields
geometry type: POINT
dimension: XYZ
bbox: xmin: -81.70055 ymin: 0.8302778 xmax: -70.76167 ymax: 12.58472
CRS: NA
First 10 features:
ID Ciudad
1 2338 Colombi
2 2339 Colombi
3 2340 Colombi
4 2341 Colombi
5 2342 Colombi
6 2343 Colombi
7 2344 Colombi
8 2345 Colombi
9 2346 Colombi
10 2347 Colombi
Latitud
1 Bogota
2 Cali
3 Medellin
4 Barranquilla
5 Cartagena
6 Cucuta
7 Bucaramanga
8 Pereira
9 Santa Marta
10 Ibague
geometry
1 POINT Z (-74.08334 4.6 2620)
2 POINT Z (-76.5225 3.437222 ...
3 POINT Z (-75.53611 6.291389...
4 POINT Z (-74.79639 10.96389...
5 POINT Z (-75.51444 10.39972...
6 POINT Z (-72.50528 7.883333...
7 POINT Z (-73.12583 7.129722...
8 POINT Z (-75.69611 4.813333...
9 POINT Z (-74.20167 11.24722 9)
10 POINT Z (-75.23222 4.438889...
class(m)
[1] "sf" "data.frame"
Se procede a obtener una matriz de coordenadas:
coords <- st_coordinates(m)
class(coords)
[1] "matrix" "array"
lat = coords[,2]
long = coords[,1]
alt = coords[,3]
deptos <- sf::read_sf("C:/Users/LUISA CARRION/Downloads/MGN2018_DPTO_POLITICO/MGN_DPTO_POLITICO.shp")
class(deptos)
st_crs(deptos)
Coordinate Reference System:
User input: WGS 84
wkt:
GEOGCRS["WGS 84",
DATUM["World Geodetic System 1984",
ELLIPSOID["WGS 84",6378137,298.257223563,
LENGTHUNIT["metre",1]]],
PRIMEM["Greenwich",0,
ANGLEUNIT["degree",0.0174532925199433]],
CS[ellipsoidal,2],
AXIS["latitude",north,
ORDER[1],
ANGLEUNIT["degree",0.0174532925199433]],
AXIS["longitude",east,
ORDER[2],
ANGLEUNIT["degree",0.0174532925199433]],
ID["EPSG",4326]]
head(deptos)
Simple feature collection with 6 features and 7 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -77.92834 ymin: -0.70584 xmax: -66.84722 ymax: 6.324317
geographic CRS: WGS 84
deptos$DPTO_CCDGO
[1] "18" "19" "86" "76" "94"
[6] "99" "85" "91" "97" "95"
[11] "17" "63" "66" "05" "27"
[16] "52" "23" "13" "20" "44"
[21] "47" "70" "88" "81" "15"
[26] "25" "54" "11" "50" "41"
[31] "68" "73" "08"
deptos$DPTO_CNMBR
[1] "CAQUETÁ"
[2] "CAUCA"
[3] "PUTUMAYO"
[4] "VALLE DEL CAUCA"
[5] "GUAINÍA"
[6] "VICHADA"
[7] "CASANARE"
[8] "AMAZONAS"
[9] "VAUPÉS"
[10] "GUAVIARE"
[11] "CALDAS"
[12] "QUINDIO"
[13] "RISARALDA"
[14] "ANTIOQUIA"
[15] "CHOCÓ"
[16] "NARIÑO"
[17] "CÓRDOBA"
[18] "BOLÍVAR"
[19] "CESAR"
[20] "LA GUAJIRA"
[21] "MAGDALENA"
[22] "SUCRE"
[23] "ARCHIPIÉLAGO DE SAN ANDRÉS, PROVIDENCIA Y SANTA CATALINA"
[24] "ARAUCA"
[25] "BOYACÁ"
[26] "CUNDINAMARCA"
[27] "NORTE DE SANTANDER"
[28] "BOGOTÁ, D.C."
[29] "META"
[30] "HUILA"
[31] "SANTANDER"
[32] "TOLIMA"
[33] "ATLÁNTICO"
# Se necesita convertir el objeto deptos en un mapa de coordenadas
deptos2 <- deptos %>% st_transform(3116)
# Intentar con preserveTopology = FALSE
deptos3 <- sf::st_simplify(deptos2, preserveTopology = TRUE, dTolerance = 1000)
object.size(deptos)
16290112 bytes
object.size(deptos3)
179568 bytes
Como leaflet trabaja con RRS WGS84, se necesita transformar el objeto simplificado en coordenadas geográficas:
deptos4 <- deptos3 %>% st_transform(4326)
mapa <- leaflet(deptos4)
mapa <- addTiles(mapa)
labels <- sprintf("<strong>%s</strong><br/>%g unkown units</sup>", deptos4$DPTO_CNMBR, deptos4$Shape_Area) %>% lapply(htmltools::HTML)
mapa <- addPolygons(mapa, color="#444444", weight = 1, smoothFactor = 0.5, opacity = 1.0, fillOpacity = 0.5, fillColor = ~colorQuantile("PuRd", Shape_Area) (Shape_Area), highlightOptions = highlightOptions(color="pink", weight = 2, bringToFront = TRUE), label=labels, labelOptions = labelOptions(style=list("font-weight"= "normal", padding="3px 8px"), textsize = "5px", direction = "auto"))
mapa <- addMarkers(mapa, lng=long, lat=lat, popup=m$name)
mapa
sessionInfo()
R version 4.0.4 (2021-02-15)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)
Matrix products: default
locale:
[1] LC_COLLATE=Spanish_Colombia.1252
[2] LC_CTYPE=Spanish_Colombia.1252
[3] LC_MONETARY=Spanish_Colombia.1252
[4] LC_NUMERIC=C
[5] LC_TIME=Spanish_Colombia.1252
attached base packages:
[1] stats graphics
[3] grDevices utils
[5] datasets methods
[7] base
other attached packages:
[1] leaflet_2.0.4.1
[2] sf_0.9-7
[3] rgeos_0.5-5
[4] sp_1.4-5
loaded via a namespace (and not attached):
[1] Rcpp_1.0.6
[2] RColorBrewer_1.1-2
[3] compiler_4.0.4
[4] pillar_1.5.0
[5] class_7.3-18
[6] tools_4.0.4
[7] digest_0.6.27
[8] jsonlite_1.7.2
[9] evaluate_0.14
[10] lifecycle_1.0.0
[11] tibble_3.0.6
[12] lattice_0.20-41
[13] pkgconfig_2.0.3
[14] rlang_0.4.10
[15] DBI_1.1.1
[16] cli_2.3.1
[17] crosstalk_1.1.1
[18] yaml_2.2.1
[19] xfun_0.21
[20] e1071_1.7-4
[21] stringr_1.4.0
[22] knitr_1.31
[23] leaflet.providers_1.9.0
[24] htmlwidgets_1.5.3
[25] vctrs_0.3.6
[26] classInt_0.4-3
[27] grid_4.0.4
[28] glue_1.4.2
[29] R6_2.5.0
[30] fansi_0.4.2
[31] rmarkdown_2.7
[32] farver_2.0.3
[33] magrittr_2.0.1
[34] scales_1.1.1
[35] ellipsis_0.3.1
[36] htmltools_0.5.1.1
[37] units_0.7-0
[38] rsconnect_0.8.16
[39] assertthat_0.2.1
[40] colorspace_2.0-0
[41] KernSmooth_2.23-18
[42] utf8_1.1.4
[43] stringi_1.5.3
[44] munsell_0.5.0
[45] crayon_1.4.1