1.Introduction

This is an R Markdown Notebook.It illustrates how to, make a map with polygons (representing departments in Colombia). The reference data set is a shapefile provided by DANE.

2.Loading the required libraries

let´s start the loading libraries:

#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)

3. Reading a table with locacion of cities

let´s read the ciudades dataset, but this time as a table :

cities <-  read.table(file="./ciudades.txt", header = FALSE, sep = ";")
head(cities)
names(cities)<- c("id", "country", "city", "latitude", "longitude", "altitude")

Now, we convert the dataframe into a spatial object:

##el orden usual de las coordenadas (i) longitud:(ii) latitud (iii) altitud
m<- st_as_sf(cities, coords = c(5,4,6))
m
(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
lat = coords[, 2]
long = coords[, 1]
alt = coords[, 3]

4. Reading a shapefile of departments

deptos <- sf::read_sf("./Municipios/MGN_DPTO_POLITICO.shp")
head(deptos)
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"
deptos2 <- deptos %>% st_transform(3116)
deptos3 <- sf::st_simplify(deptos2, preserveTopology = TRUE, dTolerance = 1000)
object.size(deptos)
## 16290112 bytes
object.size(deptos3)
## 179568 bytes
deptos4 <- deptos3 %>% st_transform(4326)

5. Mapping time

mapa <- leaflet(deptos4)
mapa  <- addTiles(mapa)
labels<-sprintf(
  "<strong>%s</strong><br/>%g unknow 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("yLOrRd", SHAPE_AREA)(SHAPE_AREA), highlightOptions = highlightOptions(color = "red", 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$city)
mapa

.