###1. Introduccion
Este cuaderno tiene como objetivo mostrar como hacer mapas con poligonos, representando los departamentos de colombia. La informacion de referencia usada es un archivo proporcionado por el DANE.
Se instala el paquete de librerias leaflet, rgeos y sfpara hacer mapas en R studio por medio de poligonos. el proceso para instalar las librerias.
#install.packages(c("rgeos","sf"))
library(rgeos)
Loading required package: sp
rgeos version: 0.5-5, (SVN revision 640)
GEOS runtime version: 3.8.0-CAPI-1.13.1
Linking to sp version: 1.4-5
Polygon checking: TRUE
library(sf)
Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(leaflet)
a continuacion se lee la informacion de ciudades en forma de tabla
cities<-read.csv(file="C:/Users/HP LAPTOP/Desktop/cities.txt", header=FALSE, sep=";")
class(cities)
[1] "data.frame"
head(cities)
NA
names(cities) <- c("ID","country","city","latitud","longitud","altitud")
cities
Ahora podemos cambiar el dataframe en un objeto espacial.
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 country city geometry
1 2338 Colombi Bogota POINT Z (-74.08334 4.6 2620)
2 2339 Colombi Cali POINT Z (-76.5225 3.437222 ...
3 2340 Colombi Medellin POINT Z (-75.53611 6.291389...
4 2341 Colombi Barranquilla POINT Z (-74.79639 10.96389...
5 2342 Colombi Cartagena POINT Z (-75.51444 10.39972...
6 2343 Colombi Cucuta POINT Z (-72.50528 7.883333...
7 2344 Colombi Bucaramanga POINT Z (-73.12583 7.129722...
8 2345 Colombi Pereira POINT Z (-75.69611 4.813333...
9 2346 Colombi Santa Marta POINT Z (-74.20167 11.24722 9)
10 2347 Colombi Ibague POINT Z (-75.23222 4.438889...
class(m)
[1] "sf" "data.frame"
vamos a conseguir una matrix de coordenadas.
(coords <-st_coordinates(m))
X Y Z
1 -74.08334 4.6000000 2620
2 -76.52250 3.4372222 758
3 -75.53611 6.2913889 2076
4 -74.79639 10.9638889 33
5 -75.51444 10.3997222 36
6 -72.50528 7.8833333 314
7 -73.12583 7.1297222 967
8 -75.69611 4.8133333 1445
9 -74.20167 11.2472222 9
10 -75.23222 4.4388889 1082
11 -75.56223 6.3388889 1489
12 -77.28111 1.2136111 2569
13 -75.52055 5.0700000 1935
14 -75.33028 2.9305556 507
15 -74.76667 10.9172222 10
16 -73.63500 4.1533333 529
17 -75.68111 4.5338889 1349
18 -74.22139 4.5872222 2413
19 -73.25056 10.4769444 163
20 -75.61139 6.1719444 1637
21 -75.89000 8.7575000 25
22 -75.39778 9.3047222 219
23 -73.08972 7.0647222 1026
24 -76.30361 3.5394444 863
25 -77.06973 3.8933333 1
26 -73.85472 7.0652778 45
27 -75.67250 4.8347222 1500
28 -76.20000 4.0866667 766
29 -75.56389 6.1730556 1784
30 -75.91167 4.7463889 722
31 -72.24416 11.3841667 44
32 -75.61750 1.6175000 579
33 -74.80083 4.3030556 328
34 -72.92973 5.7205556 2419
35 -76.30278 3.9022222 722
36 -73.36777 5.5352778 2717
37 -73.17306 7.0708333 712
38 -74.77306 10.8588889 5
39 -74.75333 9.2413889 18
40 -74.36667 4.8166667 2816
41 -72.90722 11.5444444 5
42 -73.02028 5.8269444 2501
43 -74.00584 5.0283333 2656
44 -74.36777 4.3438889 1745
45 -74.25417 11.0094444 12
46 -78.81556 1.7986111 1
47 -76.63472 7.8855556 57
48 -73.05361 6.9894444 1080
49 -73.35778 8.2363889 1272
50 -74.66917 5.4522222 152
51 -77.64445 0.8302778 2954
52 -76.66111 5.6947222 67
53 -73.62695 8.3125000 151
54 -76.49583 3.5850000 991
55 -70.76167 7.0902778 119
56 -74.92278 10.6377778 88
57 -75.60361 4.9825000 1488
58 -75.63750 6.0900000 1929
59 -72.47417 7.8338889 381
60 -74.06667 4.8666667 2325
61 -75.38889 6.1552778 2058
62 -75.64361 4.5325000 1459
63 -72.39417 5.3394444 426
64 -75.44778 8.9494444 72
65 -74.18667 10.5213889 55
66 -72.51334 7.8383333 385
67 -81.70055 12.5847222 -9999
68 -75.19722 7.9869444 65
69 -75.62139 4.8680556 1827
70 -74.88861 4.1527778 329
71 -75.40833 10.3294444 129
72 -75.79667 8.8855556 15
73 -73.97806 9.0047222 21
74 -74.21667 4.7166667 2276
75 -76.05639 1.8675000 1319
76 -76.41944 3.2336111 847
77 -72.65250 7.3780556 2516
78 -73.23889 10.0358333 183
79 -76.73167 8.0980556 7
80 -74.26833 4.7344444 2400
81 -75.34389 10.2544444 58
82 -75.64694 6.1605556 1887
83 -75.51305 6.3488889 1498
84 -74.78722 9.7919444 18
85 -76.68639 7.6769444 109
86 -75.13306 9.7222222 154
87 -76.23861 3.3275000 894
88 -73.82000 5.6188889 2713
89 -76.54444 3.2638889 890
90 -76.24472 3.4211111 912
91 -74.91833 10.7997222 124
92 -75.93611 4.2688889 1394
93 -76.48666 3.0130556 1061
94 -74.81194 7.5941667 51
95 -73.75667 3.9875000 579
96 -75.81973 9.2316667 7
97 -74.70361 7.0833333 638
98 -75.29583 9.3177778 154
99 -76.31667 3.6880556 851
100 -75.58833 8.4147222 113
class(coords)
[1] "matrix" "array"
coords[,]
X Y Z
1 -74.08334 4.6000000 2620
2 -76.52250 3.4372222 758
3 -75.53611 6.2913889 2076
4 -74.79639 10.9638889 33
5 -75.51444 10.3997222 36
6 -72.50528 7.8833333 314
7 -73.12583 7.1297222 967
8 -75.69611 4.8133333 1445
9 -74.20167 11.2472222 9
10 -75.23222 4.4388889 1082
11 -75.56223 6.3388889 1489
12 -77.28111 1.2136111 2569
13 -75.52055 5.0700000 1935
14 -75.33028 2.9305556 507
15 -74.76667 10.9172222 10
16 -73.63500 4.1533333 529
17 -75.68111 4.5338889 1349
18 -74.22139 4.5872222 2413
19 -73.25056 10.4769444 163
20 -75.61139 6.1719444 1637
21 -75.89000 8.7575000 25
22 -75.39778 9.3047222 219
23 -73.08972 7.0647222 1026
24 -76.30361 3.5394444 863
25 -77.06973 3.8933333 1
26 -73.85472 7.0652778 45
27 -75.67250 4.8347222 1500
28 -76.20000 4.0866667 766
29 -75.56389 6.1730556 1784
30 -75.91167 4.7463889 722
31 -72.24416 11.3841667 44
32 -75.61750 1.6175000 579
33 -74.80083 4.3030556 328
34 -72.92973 5.7205556 2419
35 -76.30278 3.9022222 722
36 -73.36777 5.5352778 2717
37 -73.17306 7.0708333 712
38 -74.77306 10.8588889 5
39 -74.75333 9.2413889 18
40 -74.36667 4.8166667 2816
41 -72.90722 11.5444444 5
42 -73.02028 5.8269444 2501
43 -74.00584 5.0283333 2656
44 -74.36777 4.3438889 1745
45 -74.25417 11.0094444 12
46 -78.81556 1.7986111 1
47 -76.63472 7.8855556 57
48 -73.05361 6.9894444 1080
49 -73.35778 8.2363889 1272
50 -74.66917 5.4522222 152
51 -77.64445 0.8302778 2954
52 -76.66111 5.6947222 67
53 -73.62695 8.3125000 151
54 -76.49583 3.5850000 991
55 -70.76167 7.0902778 119
56 -74.92278 10.6377778 88
57 -75.60361 4.9825000 1488
58 -75.63750 6.0900000 1929
59 -72.47417 7.8338889 381
60 -74.06667 4.8666667 2325
61 -75.38889 6.1552778 2058
62 -75.64361 4.5325000 1459
63 -72.39417 5.3394444 426
64 -75.44778 8.9494444 72
65 -74.18667 10.5213889 55
66 -72.51334 7.8383333 385
67 -81.70055 12.5847222 -9999
68 -75.19722 7.9869444 65
69 -75.62139 4.8680556 1827
70 -74.88861 4.1527778 329
71 -75.40833 10.3294444 129
72 -75.79667 8.8855556 15
73 -73.97806 9.0047222 21
74 -74.21667 4.7166667 2276
75 -76.05639 1.8675000 1319
76 -76.41944 3.2336111 847
77 -72.65250 7.3780556 2516
78 -73.23889 10.0358333 183
79 -76.73167 8.0980556 7
80 -74.26833 4.7344444 2400
81 -75.34389 10.2544444 58
82 -75.64694 6.1605556 1887
83 -75.51305 6.3488889 1498
84 -74.78722 9.7919444 18
85 -76.68639 7.6769444 109
86 -75.13306 9.7222222 154
87 -76.23861 3.3275000 894
88 -73.82000 5.6188889 2713
89 -76.54444 3.2638889 890
90 -76.24472 3.4211111 912
91 -74.91833 10.7997222 124
92 -75.93611 4.2688889 1394
93 -76.48666 3.0130556 1061
94 -74.81194 7.5941667 51
95 -73.75667 3.9875000 579
96 -75.81973 9.2316667 7
97 -74.70361 7.0833333 638
98 -75.29583 9.3177778 154
99 -76.31667 3.6880556 851
100 -75.58833 8.4147222 113
lat=coords[,2]
long=coords[,1]
alt=coords[.3]
deptos <-read_sf("C:/Users/HP LAPTOP/Downloads/MGN_DPTO_POLITICO.shp")
class(deptos)
[1] "sf" "tbl_df" "tbl" "data.frame"
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_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"
deptos$DPTO_CACTO
[1] "Ley 78 del 29 de Diciembre de 1981"
[2] "15 de junio de 1857"
[3] "Articulo 309 Constitucion Politica de 1991"
[4] "Decreto No 340 de 16 de Abril de 1910"
[5] "Articulo 309 Constitucion Politica de 1991"
[6] "5 de Julio Constitucion Politica de 1991"
[7] "5 de Julio Constitucion Politica de 1991"
[8] "Dcto. 2274 del 4 de Octubre de la Constitución Política 1991"
[9] "Articulo 309 Constitucion Politica de 1991"
[10] "5 de Julio Constitucion Politica de 1991"
[11] "11 de Abril de 1905"
[12] "Ley 2 TM de 1966"
[13] "Ley 70 del 1 de Diciembre de 1966"
[14] "Constitucion Politica de 1886"
[15] "Ley 13 del 3 de Noviembre de 1947"
[16] "Ley 1 de 1904"
[17] "Ley 9 del 18 de Diciembre de 1951"
[18] "Constitucion Politica de 1886"
[19] "Ley 25 21 de junio de 1967"
[20] "Acto Legislativo No. 1 de Diciembre 28 de 1964"
[21] "1964"
[22] "Ley 47 del 8 de Agosto de 1966"
[23] "Artículo 310 Constitucion Politica de 1991"
[24] "5 de Julio Constitucion Politica de 1991"
[25] "Constitucion Politica de 1886"
[26] "Constitucion Politica de 1886"
[27] "Ley 25 de 1910"
[28] "Constitucion Politica de 1886"
[29] "Ley 118 del 16 de Diciembre de 1959"
[30] "Ley 46 de 1905"
[31] "Ley 25 14 de Julio de 1910"
[32] "Ley 65 de Noviembre de 1909"
[33] "Ley 21 de 1910"
# necesitamos proyectar el objeto deptos (convierte las coordenadas en un mapa)
deptos2 <-deptos%>%st_transform(3116)
# intentar preserveTopology= FALSE
deptos3 <-sf:: st_simplify(deptos2, preserveTopology = TRUE, dTolerance = 1000)
object.size(deptos)
16290112 bytes
object.size(deptos3)
179568 bytes
deptos4 <- deptos3 %>% st_transform(4326)
mapa <-leaflet(deptos4)
mapas <-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("Dark2", SHAPE_AREA)(SHAPE_AREA),highlightOptions = highlightOptions(color = "Lightgreen", weight = 2, bringToFront = TRUE), label = labels, labelOptions = labelOptions(style = list("font-weight"="normal", padding = "3px 8px"), textsize = "15px", direction = "auto" ) )
mapa<- addMarkers(mapa, lng=long, lat=lat, popup = m$name)
mapa
NA
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_Mexico.1252 LC_CTYPE=Spanish_Mexico.1252
[3] LC_MONETARY=Spanish_Mexico.1252 LC_NUMERIC=C
[5] LC_TIME=Spanish_Mexico.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods
[7] base
other attached packages:
[1] leaflet_2.0.4.1 sf_0.9-7 rgeos_0.5-5 sp_1.4-5
loaded via a namespace (and not attached):
[1] Rcpp_1.0.6 RColorBrewer_1.1-2 compiler_4.0.4
[4] pillar_1.5.0 class_7.3-18 tools_4.0.4
[7] digest_0.6.27 jsonlite_1.7.2 evaluate_0.14
[10] tibble_3.0.6 lifecycle_1.0.0 lattice_0.20-41
[13] pkgconfig_2.0.3 rlang_0.4.10 cli_2.3.1
[16] DBI_1.1.1 crosstalk_1.1.1 curl_4.3
[19] yaml_2.2.1 xfun_0.21 e1071_1.7-4
[22] stringr_1.4.0 knitr_1.31 vctrs_0.3.6
[25] htmlwidgets_1.5.3 classInt_0.4-3 grid_4.0.4
[28] glue_1.4.2 R6_2.5.0 fansi_0.4.2
[31] rmarkdown_2.7 farver_2.0.3 magrittr_2.0.1
[34] scales_1.1.1 htmltools_0.5.1.1 ellipsis_0.3.1
[37] units_0.7-0 assertthat_0.2.1 rsconnect_0.8.16
[40] colorspace_2.0-0 KernSmooth_2.23-18 tinytex_0.29
[43] utf8_1.1.4 stringi_1.5.3 munsell_0.5.0
[46] crayon_1.4.1
class(mapa)
[1] "leaflet" "htmlwidget"