En este Notebook abordaremos las funcionalidades geoespacioales R mediante el uso de funciones simples (sf) y otras bibliotecas, con el fin de brindar herramientas aplicables en el area de la geomática básica.

Para empezar debemos instalar las librerias necesarias para desarrollar nuestro trabajo:

#install.packages(c("tidyverse", "sf", "leaflet", "lwgeom"))

Luego, cargaremos las librerías:

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.2     ✓ purrr   0.3.4
## ✓ tibble  3.0.3     ✓ dplyr   1.0.2
## ✓ tidyr   1.1.2     ✓ stringr 1.4.0
## ✓ readr   1.3.1     ✓ forcats 0.5.0
## ── Conflicts ────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(sf)
## Linking to GEOS 3.5.1, GDAL 2.2.2, PROJ 4.9.2
library(leaflet)
library(lwgeom)
## Linking to liblwgeom 3.0.0beta1 r16016, GEOS 3.5.1, PROJ 4.9.2

1. Lectura de datos vactoriales geoespaciales

A partir de una tabla que contiene coordenadas y nombres de algunas ciudades elaboraremos un mapa con dichas ciudades.

1.1. Leer datos en formato txt

cities<-read.table("Colombia_cities.txt", header = F, sep = ";")
head(cities)
##     V1        V2             V3        V4        V5   V6
## 1 2338  Colombi         Bogota   4.600000 -74.08334 2620
## 2 2339  Colombi           Cali   3.437222 -76.52250  758
## 3 2340  Colombi       Medellin   6.291389 -75.53611 2076
## 4 2341  Colombi   Barranquilla  10.963889 -74.79639   33
## 5 2342  Colombi      Cartagena  10.399722 -75.51444   36
## 6 2343  Colombi         Cucuta   7.883333 -72.50528  314

Puede explorar el nombre de las columnas y cambiarlas si así lo desea

colnames(cities)
## [1] "V1" "V2" "V3" "V4" "V5" "V6"
names(cities)=c("cod", "Country", "Name", "Lat", "Lon", "Alt")
head(cities)
##    cod   Country           Name       Lat       Lon  Alt
## 1 2338  Colombi         Bogota   4.600000 -74.08334 2620
## 2 2339  Colombi           Cali   3.437222 -76.52250  758
## 3 2340  Colombi       Medellin   6.291389 -75.53611 2076
## 4 2341  Colombi   Barranquilla  10.963889 -74.79639   33
## 5 2342  Colombi      Cartagena  10.399722 -75.51444   36
## 6 2343  Colombi         Cucuta   7.883333 -72.50528  314

1.2. Convertir el dataframe en un objeto espacial

Verificar que el vector de las coordenadas este en el orden correspondiente

##m <- st_as_sf(cities, coords = c(Longitud,Latitud,Altura))
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:
##     cod   Country           Name                       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...

¿Qué clase de objeto es m?

class(m)
## [1] "sf"         "data.frame"

Para saber como R interpreta los datos utiliaremos el siguiente fragmento de codigo

plot(st_geometry(m))

1.3. Creación del mapa leaflet de cities

coords=st_coordinates(m)
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

Creación de los vectores de coordenadas

#Longitud [Fila, columna]
long=coords[,1]
long
##         1         2         3         4         5         6         7         8 
## -74.08334 -76.52250 -75.53611 -74.79639 -75.51444 -72.50528 -73.12583 -75.69611 
##         9        10        11        12        13        14        15        16 
## -74.20167 -75.23222 -75.56223 -77.28111 -75.52055 -75.33028 -74.76667 -73.63500 
##        17        18        19        20        21        22        23        24 
## -75.68111 -74.22139 -73.25056 -75.61139 -75.89000 -75.39778 -73.08972 -76.30361 
##        25        26        27        28        29        30        31        32 
## -77.06973 -73.85472 -75.67250 -76.20000 -75.56389 -75.91167 -72.24416 -75.61750 
##        33        34        35        36        37        38        39        40 
## -74.80083 -72.92973 -76.30278 -73.36777 -73.17306 -74.77306 -74.75333 -74.36667 
##        41        42        43        44        45        46        47        48 
## -72.90722 -73.02028 -74.00584 -74.36777 -74.25417 -78.81556 -76.63472 -73.05361 
##        49        50        51        52        53        54        55        56 
## -73.35778 -74.66917 -77.64445 -76.66111 -73.62695 -76.49583 -70.76167 -74.92278 
##        57        58        59        60        61        62        63        64 
## -75.60361 -75.63750 -72.47417 -74.06667 -75.38889 -75.64361 -72.39417 -75.44778 
##        65        66        67        68        69        70        71        72 
## -74.18667 -72.51334 -81.70055 -75.19722 -75.62139 -74.88861 -75.40833 -75.79667 
##        73        74        75        76        77        78        79        80 
## -73.97806 -74.21667 -76.05639 -76.41944 -72.65250 -73.23889 -76.73167 -74.26833 
##        81        82        83        84        85        86        87        88 
## -75.34389 -75.64694 -75.51305 -74.78722 -76.68639 -75.13306 -76.23861 -73.82000 
##        89        90        91        92        93        94        95        96 
## -76.54444 -76.24472 -74.91833 -75.93611 -76.48666 -74.81194 -73.75667 -75.81973 
##        97        98        99       100 
## -74.70361 -75.29583 -76.31667 -75.58833
#Latitud
lat=coords[,2]
lat
##          1          2          3          4          5          6          7 
##  4.6000000  3.4372222  6.2913889 10.9638889 10.3997222  7.8833333  7.1297222 
##          8          9         10         11         12         13         14 
##  4.8133333 11.2472222  4.4388889  6.3388889  1.2136111  5.0700000  2.9305556 
##         15         16         17         18         19         20         21 
## 10.9172222  4.1533333  4.5338889  4.5872222 10.4769444  6.1719444  8.7575000 
##         22         23         24         25         26         27         28 
##  9.3047222  7.0647222  3.5394444  3.8933333  7.0652778  4.8347222  4.0866667 
##         29         30         31         32         33         34         35 
##  6.1730556  4.7463889 11.3841667  1.6175000  4.3030556  5.7205556  3.9022222 
##         36         37         38         39         40         41         42 
##  5.5352778  7.0708333 10.8588889  9.2413889  4.8166667 11.5444444  5.8269444 
##         43         44         45         46         47         48         49 
##  5.0283333  4.3438889 11.0094444  1.7986111  7.8855556  6.9894444  8.2363889 
##         50         51         52         53         54         55         56 
##  5.4522222  0.8302778  5.6947222  8.3125000  3.5850000  7.0902778 10.6377778 
##         57         58         59         60         61         62         63 
##  4.9825000  6.0900000  7.8338889  4.8666667  6.1552778  4.5325000  5.3394444 
##         64         65         66         67         68         69         70 
##  8.9494444 10.5213889  7.8383333 12.5847222  7.9869444  4.8680556  4.1527778 
##         71         72         73         74         75         76         77 
## 10.3294444  8.8855556  9.0047222  4.7166667  1.8675000  3.2336111  7.3780556 
##         78         79         80         81         82         83         84 
## 10.0358333  8.0980556  4.7344444 10.2544444  6.1605556  6.3488889  9.7919444 
##         85         86         87         88         89         90         91 
##  7.6769444  9.7222222  3.3275000  5.6188889  3.2638889  3.4211111 10.7997222 
##         92         93         94         95         96         97         98 
##  4.2688889  3.0130556  7.5941667  3.9875000  9.2316667  7.0833333  9.3177778 
##         99        100 
##  3.6880556  8.4147222
#Altitud
alt=coords[,3]
alt
##     1     2     3     4     5     6     7     8     9    10    11    12    13 
##  2620   758  2076    33    36   314   967  1445     9  1082  1489  2569  1935 
##    14    15    16    17    18    19    20    21    22    23    24    25    26 
##   507    10   529  1349  2413   163  1637    25   219  1026   863     1    45 
##    27    28    29    30    31    32    33    34    35    36    37    38    39 
##  1500   766  1784   722    44   579   328  2419   722  2717   712     5    18 
##    40    41    42    43    44    45    46    47    48    49    50    51    52 
##  2816     5  2501  2656  1745    12     1    57  1080  1272   152  2954    67 
##    53    54    55    56    57    58    59    60    61    62    63    64    65 
##   151   991   119    88  1488  1929   381  2325  2058  1459   426    72    55 
##    66    67    68    69    70    71    72    73    74    75    76    77    78 
##   385 -9999    65  1827   329   129    15    21  2276  1319   847  2516   183 
##    79    80    81    82    83    84    85    86    87    88    89    90    91 
##     7  2400    58  1887  1498    18   109   154   894  2713   890   912   124 
##    92    93    94    95    96    97    98    99   100 
##  1394  1061    51   579     7   638   154   851   113

- Crar el mapa leaflet

library(leaflet)
mapa=leaflet()%>%
addTiles()%>%  
addMarkers(lng=long, lat=lat, popup=cities$Name) 
mapa

2. Creación de un mapa Departamental

Usaremos la biblioteca sf para leer un shapefile de departamentos descargados del Dane o DIVA-GIS. Revise el siguiente fragmento de código y cámbielo de acuerdo con su departamento.

Antes de empezar instale la libreria necesaria

2.1. Shapefile de departamentos

Primero, podemos leer un shapefile que representa a los departamentos colombianos que se descargó previamente de DIVA-GIS.

deptos <-  read_sf("COL_adm1.shp")
deptos
## Simple feature collection with 32 features and 9 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -81.84153 ymin: -4.228429 xmax: -66.87033 ymax: 15.91247
## CRS:            4326
## # A tibble: 32 x 10
##     ID_0 ISO   NAME_0  ID_1 NAME_1 TYPE_1 ENGTYPE_1 NL_NAME_1 VARNAME_1
##    <dbl> <chr> <chr>  <dbl> <chr>  <chr>  <chr>     <chr>     <chr>    
##  1    53 COL   Colom…     1 Amazo… Comis… Commissi… <NA>      <NA>     
##  2    53 COL   Colom…     2 Antio… Depar… Departme… <NA>      <NA>     
##  3    53 COL   Colom…     3 Arauca Inten… Intendan… <NA>      <NA>     
##  4    53 COL   Colom…     4 Atlán… Depar… Departme… <NA>      <NA>     
##  5    53 COL   Colom…     5 Bolív… Depar… Departme… <NA>      <NA>     
##  6    53 COL   Colom…     6 Boyacá Depar… Departme… <NA>      <NA>     
##  7    53 COL   Colom…     7 Córdo… Depar… Departme… <NA>      <NA>     
##  8    53 COL   Colom…     8 Caldas Depar… Departme… <NA>      <NA>     
##  9    53 COL   Colom…     9 Caque… Inten… Intendan… <NA>      <NA>     
## 10    53 COL   Colom…    10 Casan… Inten… Intendan… <NA>      <NA>     
## # … with 22 more rows, and 1 more variable: geometry <MULTIPOLYGON [°]>

Para saber el sistema de referencia de coordenadas de los datos vectoriales almacenados en el objeto deptos, aplicamos el siguiente codigo

st_crs(deptos)
## Coordinate Reference System:
##   User input: 4326 
##   wkt:
## GEOGCS["GCS_WGS_1984",
##     DATUM["WGS_1984",
##         SPHEROID["WGS_84",6378137.0,298.257223563]],
##     PRIMEM["Greenwich",0.0],
##     UNIT["Degree",0.0174532925199433],
##     AUTHORITY["EPSG","4326"]]

Un ggplot nos permite visualizar los datos geoespaciales

ggplot() + geom_sf(data = deptos, fill="gold2")

2.2. Mapa de Antioquía por filtrado de datos

Como solo trabajaremos con un departamento, podemos filtrar los datos de DIVA-GIS o subir los datos obtenidos del DANE.

Para visualizar el mapa del Departamento aplicar el siguiente codigo

antioquia <-  deptos %>%   filter(NAME_1 == "Antioquia")
ggplot() + geom_sf(data = antioquia, fill="gold4") 

También podemos filtrar por municipio según el departamento

munic <-  read_sf("COL_adm2.shp")
mun_antioquia <- munic %>% filter(NAME_1 == "Antioquia")
ggplot() + geom_sf(data = mun_antioquia, fill="khaki1") 

Veamos que hay en mun_antioquia

mun_antioquia
## Simple feature collection with 124 features and 11 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -77.149 ymin: 5.456801 xmax: -73.8962 ymax: 9.000139
## CRS:            4326
## # A tibble: 124 x 12
##     ID_0 ISO   NAME_0  ID_1 NAME_1  ID_2 NAME_2 TYPE_2 ENGTYPE_2 NL_NAME_2
##  * <dbl> <chr> <chr>  <dbl> <chr>  <dbl> <chr>  <chr>  <chr>     <chr>    
##  1    53 COL   Colom…     2 Antio…     9 Abejo… Munic… Municipa… <NA>     
##  2    53 COL   Colom…     2 Antio…    10 Abria… Munic… Municipa… <NA>     
##  3    53 COL   Colom…     2 Antio…    11 Aleja… Munic… Municipa… <NA>     
##  4    53 COL   Colom…     2 Antio…    12 Amagá  Munic… Municipa… <NA>     
##  5    53 COL   Colom…     2 Antio…    13 Amalfi Munic… Municipa… <NA>     
##  6    53 COL   Colom…     2 Antio…    14 Andes  Munic… Municipa… <NA>     
##  7    53 COL   Colom…     2 Antio…    15 Angel… Munic… Municipa… <NA>     
##  8    53 COL   Colom…     2 Antio…    16 Angos… Munic… Municipa… <NA>     
##  9    53 COL   Colom…     2 Antio…    17 Anorí  Munic… Municipa… <NA>     
## 10    53 COL   Colom…     2 Antio…    18 Anzá   Munic… Municipa… <NA>     
## # … with 114 more rows, and 2 more variables: VARNAME_2 <chr>,
## #   geometry <MULTIPOLYGON [°]>

Para construir un mejor terminado del mapa en formato png o pdf, descomentar el siguiente fragmento de código

#antioquia_points<- st_centroid(mun_antioquia)
#antioquia_points <- cbind(mun_antioquia, st_coordinates(st_centroid(mun_antioquia$geometry)))
#ggplot(antioquia) + 
#geom_sf(data=antioquia_points, aes(x=X, y=Y, fill =ID_2), color = "black", size = 0.25) +
#geom_text(data = antioquia_points, aes(x=X, y=Y,label = ID_2), size = 2) +
#theme(aspect.ratio=1)+
#scale_fill_distiller(name="ID_2", palette = "YlGn")+
#labs(title="Another  Map of Antioquia")

2.3. Visualización de datos mediante leaflet

Para obtener el área de los municipios cargamos primero la siguiente librería (lwgeom) previamente intalada

library(lwgeom)

Calculemos el área de cada municipio (m2)

mun_antioquia$area <- st_area(mun_antioquia)
mun_antioquia$area
## Units: [m^2]
##   [1]  507604080  281746587  131995406  102161347 1181139427  540102630
##   [7]   87932214  364579608 1412775529  222173985  573793070  482786408
##  [13]  233750426  130729354  221367480  150389375  309021433  225723968
##  [19]  255264509  409749329  358611915  308970970 2034382489  465211833
##  [25]  222977132  152670264  212665113  309890464   78874429  246308794
##  [31]  170913544 1320498637  673713930   39000787  262909428  155605622
##  [37]  201448673   66265470 1831296444  183027832  220889086 1927631274
##  [43]  423670642   64551669  192706031   46776861  225715166 1315083049
##  [49]  387271775  101544194   82686209  220662295   82197491  159732255
##  [55]   81437619  113263619   45629700   23479693 2129961667  237635109
##  [61]  177933656  134626147   37046194  200996395  241062299  423522380
##  [67]  113759123  361372217   80117178 1574108207 1200382035  303970842
##  [73]  491462592 1160824515  118439371  148436329  392588911  116915089
##  [79] 1140186485  543644719  354299204 1923549049  267507032  215622357
##  [85]  252586428   21554042  448135674  169303920  764538247  356872303
##  [91]  151178175  177611752  310801185  443691309  215791030  715293115
##  [97]  369051467  432297142  204964758  245728904  444902343  871822946
## [103]  262356515 1216274998 1302794296  188183801  253151556 1748808006
## [109]  139974164  145784102  140095786 3566771011  242289889 2642135198
## [115]  534073867  121026645  531755944  144635975 1495856879  487295551
## [121]  734335803  896467192 1825954162 1021941883

Luego obtenemos el área en km2 mediante la creación de un nuevo objeto

mun_antioquia$km2 <- mun_antioquia$area/(1000000)

Veamos que hay en mun_antioquia$km2

mun_antioquia$km2
## Units: [m^2]
##   [1]  507.60408  281.74659  131.99541  102.16135 1181.13943  540.10263
##   [7]   87.93221  364.57961 1412.77553  222.17398  573.79307  482.78641
##  [13]  233.75043  130.72935  221.36748  150.38938  309.02143  225.72397
##  [19]  255.26451  409.74933  358.61191  308.97097 2034.38249  465.21183
##  [25]  222.97713  152.67026  212.66511  309.89046   78.87443  246.30879
##  [31]  170.91354 1320.49864  673.71393   39.00079  262.90943  155.60562
##  [37]  201.44867   66.26547 1831.29644  183.02783  220.88909 1927.63127
##  [43]  423.67064   64.55167  192.70603   46.77686  225.71517 1315.08305
##  [49]  387.27178  101.54419   82.68621  220.66230   82.19749  159.73225
##  [55]   81.43762  113.26362   45.62970   23.47969 2129.96167  237.63511
##  [61]  177.93366  134.62615   37.04619  200.99640  241.06230  423.52238
##  [67]  113.75912  361.37222   80.11718 1574.10821 1200.38203  303.97084
##  [73]  491.46259 1160.82451  118.43937  148.43633  392.58891  116.91509
##  [79] 1140.18649  543.64472  354.29920 1923.54905  267.50703  215.62236
##  [85]  252.58643   21.55404  448.13567  169.30392  764.53825  356.87230
##  [91]  151.17818  177.61175  310.80119  443.69131  215.79103  715.29312
##  [97]  369.05147  432.29714  204.96476  245.72890  444.90234  871.82295
## [103]  262.35652 1216.27500 1302.79430  188.18380  253.15156 1748.80801
## [109]  139.97416  145.78410  140.09579 3566.77101  242.28989 2642.13520
## [115]  534.07387  121.02665  531.75594  144.63597 1495.85688  487.29555
## [121]  734.33580  896.46719 1825.95416 1021.94188

2.3. Creación del plot

Necesitamos una conversión de características simples a polígonos espaciales

ant_mun <- as(mun_antioquia, 'Spatial')

Exploramos el objeto espacial (desomentar el diguiente codigo)

#head(ant_mun)

Ahora, organicemos el plot

bins <- c(0, 50, 100, 200, 300, 500, 1000, 2000, Inf)
pal <- colorBin("RdPu", domain = ant_mun$km2, bins = bins)
labels <- mun_antioquia$NAME_2
labels
##   [1] "Abejorral"                 "Abriaquí"                 
##   [3] "Alejandría"                "Amagá"                    
##   [5] "Amalfi"                    "Andes"                    
##   [7] "Angelópolis"               "Angostura"                
##   [9] "Anorí"                     "Anzá"                     
##  [11] "Apartadó"                  "Arboletes"                
##  [13] "Argelia"                   "Armenia"                  
##  [15] "Barbosa"                   "Bello"                    
##  [17] "Belmira"                   "Betania"                  
##  [19] "Betulia"                   "Bolívar"                  
##  [21] "Briceño"                   "Buriticá"                 
##  [23] "Cáceres"                   "Cañasgordas"              
##  [25] "Caicedo"                   "Caldas"                   
##  [27] "Campamento"                "Caracolí"                 
##  [29] "Caramanta"                 "Carepa"                   
##  [31] "Carolina del Principe"     "Caucasia"                 
##  [33] "Chigorodó"                 "Cisneros"                 
##  [35] "Cocorná"                   "Concepción"               
##  [37] "Concordia"                 "Copacabana"               
##  [39] "Dabeiba"                   "Don Matías"               
##  [41] "Ebéjico"                   "El Bagre"                 
##  [43] "El Carmen de Viboral"      "El Santuario"             
##  [45] "Entrerríos"                "Envigado"                 
##  [47] "Fredonia"                  "Frontino"                 
##  [49] "Gómez Plata"               "Giraldo"                  
##  [51] "Girardota"                 "Granada"                  
##  [53] "Guadalupe"                 "Guarne"                   
##  [55] "Guatapé"                   "Heliconia"                
##  [57] "Hispania"                  "Itagüí"                   
##  [59] "Ituango"                   "Jardín"                   
##  [61] "Jericó"                    "La Ceja"                  
##  [63] "La Estrella"               "La Unión de Sucre"        
##  [65] "Liborina"                  "Maceo"                    
##  [67] "Marinilla"                 "Medellín"                 
##  [69] "Montebello"                "Murindó"                  
##  [71] "Mutatá"                    "Nariño"                   
##  [73] "Nechí"                     "Necoclí"                  
##  [75] "Olaya"                     "Peñol"                    
##  [77] "Pequé"                     "Pueblorrico"              
##  [79] "Puerto Berrío"             "Puerto Nare"              
##  [81] "Puerto Triunfo"            "Remedios"                 
##  [83] "Retiro"                    "Rionegro"                 
##  [85] "Sabanalarga"               "Sabaneta"                 
##  [87] "Salgar"                    "San Andrés de Cuerquia"   
##  [89] "San Carlos"                "San Francisco"            
##  [91] "San Jerónimo"              "San José de la Montaña"   
##  [93] "San Juan de Urabá"         "San Luis"                 
##  [95] "San Pedro de los Milagros" "San Pedro de Urabá"       
##  [97] "San Rafael"                "San Roque"                
##  [99] "San Vicente"               "Santa Bárbara"            
## [101] "Santa Fe de Antioquia"     "Santa Rosa de Osos"       
## [103] "Santo Domingo"             "Segovia"                  
## [105] "Sonsón"                    "Sopetrán"                 
## [107] "Támesis"                   "Tarazá"                   
## [109] "Tarso"                     "Titiribí"                 
## [111] "Toledo"                    "Turbo"                    
## [113] "Uramita"                   "Urrao"                    
## [115] "Valdivia"                  "Valparaíso"               
## [117] "Vegachí"                   "Venecia"                  
## [119] "Vigía del Fuerte"          "Yalí"                     
## [121] "Yarumal"                   "Yolombó"                  
## [123] "Yondó"                     "Zaragoza"

Luego, crea el plot

mapa1 <- leaflet(ant_mun) %>%
  setView(-75.5, 7, 8)  %>% addPolygons(
  fillColor = ~pal(km2),
  weight = 2,
  opacity = 1,
  color = "white",
  dashArray = "3",
  fillOpacity = 0.7,
  highlight = highlightOptions(
    weight = 5,
    color = "RdPu",
    dashArray = "",
    fillOpacity = 0.7,
    bringToFront = TRUE),
  label = labels) %>%
  addLegend(pal = pal, values = ~km2, opacity = 0.7, title = NULL,
    position = "bottomright")

Visualicemos como quedó

mapa1

Exploremos otras formas de diagramar el mapa

leaflet() %>%
  addProviderTiles(providers$Esri.WorldImagery, options= providerTileOptions(opacity = 0.99)) %>%
  addPolygons(data = ant_mun, popup= ant_mun$NAME_2,
    stroke = TRUE, fillOpacity = 0.25, smoothFactor = 0.25)
leaflet() %>%
  addProviderTiles(providers$Esri.WorldStreetMap, options= providerTileOptions(opacity = 0.9)) %>%
  addPolygons(data = ant_mun, popup= ant_mun$NAME_2,
    stroke = TRUE, fillOpacity = 0.05, smoothFactor = 0.05)