Nombre: Aparicio Aponte Erika
Fecha: 25 de marzo del 2020

Aprende a leer, filtrar y visualizar datos vectoriales geoespaciales en R

1. Introduccción

Este es un R Markdown Notebook que ilustra las funciones básicas proporcionadas por las funciones simples (sf) y las bibliotecas tidyverse, por tanto, el primer pasa sera instalar las bibliotecas necesarias
#install.packages(c("tidyverse", "sf"))
Ahora es necesario cargar las dos librerias:
library(tidyverse)
## -- Attaching packages --------------------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.0     v purrr   0.3.3
## v tibble  2.1.3     v dplyr   0.8.4
## v tidyr   1.0.2     v stringr 1.4.0
## v readr   1.3.1     v 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.6.1, GDAL 2.2.3, PROJ 4.9.3

2. Lectura de datos de vectores

ya se puede leer un archivo de formas que representa a los departamentos colombiano, el cual se ha descargado previamente de DIVA-GIS.
deptos <- read_sf("C:/Users/Familia/Desktop/Curso/Datos/COL_adm/COL_adm1.shp")
Para saber el contenido del objeto deptos
head(deptos)
Por otro lado, si queremos saber cuál es el sistema de referencia de coordenadas de los datos vectoriales almacenados en el objeto deptos, se puede usar la función st_crs:
st_crs(deptos)
## Coordinate Reference System:
##   EPSG: 4326 
##   proj4string: "+proj=longlat +datum=WGS84 +no_defs"

3. Usar ggplot para visualizar datos geoespaciales:

Primero es necesario comprender los datos, para ello, es trazar los datos. Sepuede usar las funcionalidades de ggplot:
ggplot() + geom_sf(data = deptos) 

##### Se puede utilizar cualquier sistema de referencia de coordenadas para trazar los datos. Sin embargo, no es correcto utilizar un sistema de referencia de coordenadas que se haya definido explícitamente para otro país o región. #### Por ejemplo, el CRS 3978 se usa en Canadá

ggplot() + geom_sf(data = deptos) + coord_sf(crs=st_crs(3978))

No obstante, las propiedades del CRS con el código EPSG 32618. Corresponde a UTM 18 N. Por lo cual, en caso de requerir usar dicho CRS, es necesario convertir el objeto espacial de EPSG4326 a EPSG: 32618.Para ello realizamos:
deptos_utm <- st_transform(deptos, crs = st_crs(32618))
deptos_utm
ggplot() + geom_sf(data = deptos_utm)

### 4. Filtrar datos geoespaciales basados en atributos ##### En este caso, se va a filtrar los datos del departamentode Meta, el cual es el de interes

meta <-  deptos %>%   filter(NAME_1 == "Meta")
Procedemos a trazar el nuevo objeto
ggplot() + geom_sf(data = meta) 

Ahora se repetira los pasos anteriores para cargar los municipios colombianos y filtrar los del meta
munic <-  read_sf("C:/Users/Familia/Desktop/Curso/Datos/COL_adm/COL_adm2.shp")
mun_meta <- munic %>% filter(NAME_1 == "Meta")
ggplot() + geom_sf(data = mun_meta) 

mun_meta
meta_points<- st_centroid(mun_meta)
## Warning in st_centroid.sf(mun_meta): st_centroid assumes attributes are constant
## over geometries of x
## Warning in st_centroid.sfc(st_geometry(x), of_largest_polygon =
## of_largest_polygon): st_centroid does not give correct centroids for longitude/
## latitude data
meta_points <- cbind(mun_meta, st_coordinates(st_centroid(mun_meta$geometry)))
## Warning in st_centroid.sfc(mun_meta$geometry): st_centroid does not give correct
## centroids for longitude/latitude data
Se puede producir una mejor trama utilizando el siguiente fragmento:
ggplot(meta) +
    geom_sf() +
    geom_sf(data = meta_points, fill = "antiquewhite") + 
    geom_text(data = meta_points, aes(x=X, y=Y,label = ID_2), size = 2) +
    coord_sf(xlim = c(-76, -70.5), ylim = c(1.5, 5), expand = FALSE)

library(scales)
## 
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
## 
##     discard
## The following object is masked from 'package:readr':
## 
##     col_factor
ggplot(meta) + 
  geom_sf(data=meta_points, aes(x=X, y=Y, fill =
                                       ID_2), color = "black", size = 0.25) +
  geom_text(data = meta_points, aes(x=X, y=Y,label = ID_2), size = 2) +
  theme(aspect.ratio=1)+
  scale_fill_distiller(name="ID_2", palette = "YlGn", breaks = pretty_breaks(n = 5))+
  labs(title="Otro mapa de Meta")
## Warning: Ignoring unknown aesthetics: x, y

Cabe recalcar que esta visualización no es un mapa real, Pero la salida se puede guardar como un pdf o como un png.
ggsave("meta_municipios.pdf")
## Saving 7 x 5 in image
ggsave("map_meta.png", width = 6, height = 6, dpi = "screen")

5. Usar el folleto para visualizar datos

Primero, se instala “leaflet” que es la biblioteca requerida:
#install.packages("leaflet")
Ahora se llama a la libreria
library(leaflet)
## This version of Shiny is designed to work with 'htmlwidgets' >= 1.5.
##     Please upgrade via install.packages('htmlwidgets').
Sin embargo, para usar la biblioteca se necesita convertir de características simples a puntos espaciales.
ant_points <- as(meta_points, 'Spatial')
Descomente el siguiente comando para ver qué hay dentro del objeto:
#head(ant_points)
head(ant_points)
## An object of class "SpatialPolygonsDataFrame"
## Slot "data":
##   ID_0 ISO   NAME_0 ID_1 NAME_1 ID_2            NAME_2    TYPE_2    ENGTYPE_2
## 1   53 COL Colombia   20   Meta  683           Acacías Municipio Municipality
## 2   53 COL Colombia   20   Meta  684  Barranca de Upía Municipio Municipality
## 3   53 COL Colombia   20   Meta  685          Cabuyaro Municipio Municipality
## 4   53 COL Colombia   20   Meta  686 Castilla la Nueva Municipio Municipality
## 5   53 COL Colombia   20   Meta  687           Cumaral Municipio Municipality
## 6   53 COL Colombia   20   Meta  688       El Calvario Municipio Municipality
##   NL_NAME_2 VARNAME_2         X        Y
## 1      <NA>      <NA> -73.78020 4.009281
## 2      <NA>      <NA> -72.99367 4.474491
## 3      <NA>      <NA> -72.98028 4.285326
## 4      <NA>      <NA> -73.53548 3.793866
## 5      <NA>      <NA> -73.32252 4.220168
## 6      <NA>      <NA> -73.74434 4.354279
## 
## Slot "polygons":
## [[1]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] -73.780200   4.009281
## 
## Slot "area":
## [1] 0.07943183
## 
## Slot "hole":
## [1] FALSE
## 
## Slot "ringDir":
## [1] 1
## 
## Slot "coords":
##            [,1]     [,2]
##  [1,] -73.69500 3.882801
##  [2,] -73.70880 3.883400
##  [3,] -73.71570 3.891600
##  [4,] -73.73760 3.903201
##  [5,] -73.74860 3.908999
##  [6,] -73.77400 3.921801
##  [7,] -73.80110 3.933500
##  [8,] -73.83390 3.946400
##  [9,] -73.85530 3.950500
## [10,] -73.88350 3.962201
## [11,] -73.91290 3.972099
## [12,] -73.96190 3.984999
## [13,] -73.99020 3.992600
## [14,] -74.01790 4.003201
## [15,] -74.04610 4.014302
## [16,] -74.06340 4.026500
## [17,] -74.07730 4.036300
## [18,] -74.09050 4.037000
## [19,] -74.08530 4.039801
## [20,] -74.07090 4.053601
## [21,] -74.06000 4.066301
## [22,] -74.05190 4.079000
## [23,] -74.03920 4.088700
## [24,] -74.00810 4.095501
## [25,] -73.99140 4.094899
## [26,] -73.97760 4.096000
## [27,] -73.97240 4.095401
## [28,] -73.95510 4.096502
## [29,] -73.92910 4.100401
## [30,] -73.91240 4.109600
## [31,] -73.88880 4.125602
## [32,] -73.86340 4.137100
## [33,] -73.83690 4.150301
## [34,] -73.80990 4.166899
## [35,] -73.77590 4.188102
## [36,] -73.74820 4.181700
## [37,] -73.73610 4.168901
## [38,] -73.71590 4.155000
## [39,] -73.71530 4.138198
## [40,] -73.72620 4.121501
## [41,] -73.74240 4.099601
## [42,] -73.76140 4.084098
## [43,] -73.76540 4.066800
## [44,] -73.76830 4.050101
## [45,] -73.76080 4.032100
## [46,] -73.73940 4.020499
## [47,] -73.70600 4.008800
## [48,] -73.66050 4.000500
## [49,] -73.61950 3.983600
## [50,] -73.56710 3.974700
## [51,] -73.49730 3.970901
## [52,] -73.46270 3.969100
## [53,] -73.48000 3.940800
## [54,] -73.49210 3.927000
## [55,] -73.50250 3.912600
## [56,] -73.50760 3.900500
## [57,] -73.50760 3.882001
## [58,] -73.51823 3.862573
## [59,] -73.52660 3.866500
## [60,] -73.53880 3.870600
## [61,] -73.56350 3.873600
## [62,] -73.59760 3.874299
## [63,] -73.62640 3.874400
## [64,] -73.64940 3.874500
## [65,] -73.68340 3.883300
## [66,] -73.69500 3.882801
## 
## 
## 
## Slot "plotOrder":
## [1] 1
## 
## Slot "labpt":
## [1] -73.780200   4.009281
## 
## Slot "ID":
## [1] "1"
## 
## Slot "area":
## [1] 0.07943183
## 
## 
## [[2]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] -72.993668   4.474491
## 
## Slot "area":
## [1] 0.03459735
## 
## Slot "hole":
## [1] FALSE
## 
## Slot "ringDir":
## [1] 1
## 
## Slot "coords":
##           [,1]     [,2]
##  [1,] -72.8167 4.341800
##  [2,] -72.8542 4.343099
##  [3,] -72.8853 4.357100
##  [4,] -72.9268 4.352699
##  [5,] -72.9505 4.368899
##  [6,] -72.9949 4.402599
##  [7,] -73.0393 4.432900
##  [8,] -73.0508 4.434602
##  [9,] -73.0641 4.434700
## [10,] -73.0704 4.435302
## [11,] -73.0750 4.435302
## [12,] -73.0808 4.434199
## [13,] -73.0866 4.434800
## [14,] -73.0917 4.435400
## [15,] -73.0998 4.441800
## [16,] -73.1096 4.437200
## [17,] -73.0723 4.666399
## [18,] -73.0457 4.634501
## [19,] -73.0371 4.618800
## [20,] -73.0244 4.596801
## [21,] -73.0025 4.562699
## [22,] -72.9806 4.535401
## [23,] -72.9523 4.499498
## [24,] -72.9332 4.475200
## [25,] -72.9177 4.458900
## [26,] -72.8952 4.438600
## [27,] -72.8773 4.418300
## [28,] -72.8715 4.409600
## [29,] -72.8715 4.406200
## [30,] -72.8698 4.397500
## [31,] -72.8588 4.398601
## [32,] -72.8490 4.395700
## [33,] -72.8490 4.384101
## [34,] -72.8496 4.373100
## [35,] -72.8467 4.370800
## [36,] -72.8265 4.371299
## [37,] -72.8133 4.365501
## [38,] -72.8213 4.356799
## [39,] -72.8236 4.347601
## [40,] -72.8167 4.341800
## 
## 
## 
## Slot "plotOrder":
## [1] 1
## 
## Slot "labpt":
## [1] -72.993668   4.474491
## 
## Slot "ID":
## [1] "2"
## 
## Slot "area":
## [1] 0.03459735
## 
## 
## [[3]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] -72.980284   4.285326
## 
## Slot "area":
## [1] 0.06587133
## 
## Slot "hole":
## [1] FALSE
## 
## Slot "ringDir":
## [1] 1
## 
## Slot "coords":
##           [,1]     [,2]
##  [1,] -73.0633 4.163800
##  [2,] -73.0605 4.181101
##  [3,] -73.0709 4.203701
##  [4,] -73.0853 4.216999
##  [5,] -73.0911 4.225099
##  [6,] -73.1083 4.223500
##  [7,] -73.1383 4.221300
##  [8,] -73.1096 4.437200
##  [9,] -73.0998 4.441800
## [10,] -73.0917 4.435400
## [11,] -73.0866 4.434800
## [12,] -73.0808 4.434199
## [13,] -73.0750 4.435302
## [14,] -73.0704 4.435302
## [15,] -73.0641 4.434700
## [16,] -73.0508 4.434602
## [17,] -73.0393 4.432900
## [18,] -72.9949 4.402599
## [19,] -72.9505 4.368899
## [20,] -72.9268 4.352699
## [21,] -72.8853 4.357100
## [22,] -72.8542 4.343099
## [23,] -72.8167 4.341800
## [24,] -72.8081 4.330801
## [25,] -72.8138 4.323301
## [26,] -72.8086 4.316898
## [27,] -72.8017 4.313999
## [28,] -72.7919 4.312801
## [29,] -72.7810 4.305300
## [30,] -72.7729 4.293699
## [31,] -72.7717 4.287901
## [32,] -72.7700 4.279801
## [33,] -72.7677 4.265901
## [34,] -72.7608 4.252600
## [35,] -72.7723 4.241701
## [36,] -72.7861 4.235401
## [37,] -72.8000 4.240700
## [38,] -72.8138 4.242400
## [39,] -72.8282 4.235000
## [40,] -72.8455 4.234501
## [41,] -72.8622 4.240301
## [42,] -72.8760 4.227101
## [43,] -72.8806 4.216202
## [44,] -72.9002 4.205301
## [45,] -72.8968 4.192501
## [46,] -72.9066 4.174102
## [47,] -72.9129 4.177001
## [48,] -72.9279 4.181101
## [49,] -72.9331 4.160901
## [50,] -72.9446 4.141900
## [51,] -72.9740 4.137401
## [52,] -72.9844 4.140899
## [53,] -73.0005 4.140999
## [54,] -73.0172 4.149701
## [55,] -73.0362 4.142900
## [56,] -73.0403 4.147499
## [57,] -73.0403 4.156798
## [58,] -73.0449 4.162501
## [59,] -73.0633 4.163800
## 
## 
## 
## Slot "plotOrder":
## [1] 1
## 
## Slot "labpt":
## [1] -72.980284   4.285326
## 
## Slot "ID":
## [1] "3"
## 
## Slot "area":
## [1] 0.06587133
## 
## 
## [[4]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] -73.535484   3.793866
## 
## Slot "area":
## [1] 0.04571812
## 
## Slot "hole":
## [1] FALSE
## 
## Slot "ringDir":
## [1] 1
## 
## Slot "coords":
##            [,1]     [,2]
##  [1,] -73.51823 3.862573
##  [2,] -73.51450 3.857201
##  [3,] -73.49670 3.865801
##  [4,] -73.48280 3.869201
##  [5,] -73.46840 3.861101
##  [6,] -73.44480 3.866701
##  [7,] -73.42920 3.870101
##  [8,] -73.41600 3.875899
##  [9,] -73.40790 3.843501
## [10,] -73.40040 3.801301
## [11,] -73.38070 3.750401
## [12,] -73.37780 3.724900
## [13,] -73.37670 3.695501
## [14,] -73.37492 3.681109
## [15,] -73.39970 3.686300
## [16,] -73.45330 3.700401
## [17,] -73.49600 3.704600
## [18,] -73.54500 3.719899
## [19,] -73.57610 3.736200
## [20,] -73.62400 3.756600
## [21,] -73.64710 3.757799
## [22,] -73.68630 3.770100
## [23,] -73.70880 3.780600
## [24,] -73.72200 3.789399
## [25,] -73.72370 3.790500
## [26,] -73.71970 3.810101
## [27,] -73.71280 3.827399
## [28,] -73.70760 3.839000
## [29,] -73.69500 3.882801
## [30,] -73.68340 3.883300
## [31,] -73.64940 3.874500
## [32,] -73.62640 3.874400
## [33,] -73.59760 3.874299
## [34,] -73.56350 3.873600
## [35,] -73.53880 3.870600
## [36,] -73.52660 3.866500
## [37,] -73.51823 3.862573
## 
## 
## 
## Slot "plotOrder":
## [1] 1
## 
## Slot "labpt":
## [1] -73.535484   3.793866
## 
## Slot "ID":
## [1] "4"
## 
## Slot "area":
## [1] 0.04571812
## 
## 
## [[5]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] -73.322518   4.220168
## 
## Slot "area":
## [1] 0.03088037
## 
## Slot "hole":
## [1] FALSE
## 
## Slot "ringDir":
## [1] 1
## 
## Slot "coords":
##            [,1]     [,2]
##  [1,] -73.06330 4.163800
##  [2,] -73.07140 4.155201
##  [3,] -73.08580 4.152901
##  [4,] -73.10430 4.151201
##  [5,] -73.11816 4.150319
##  [6,] -73.16480 4.169400
##  [7,] -73.23740 4.178999
##  [8,] -73.31010 4.190801
##  [9,] -73.37520 4.195701
## [10,] -73.42600 4.205699
## [11,] -73.48710 4.206000
## [12,] -73.53430 4.209699
## [13,] -73.55800 4.220698
## [14,] -73.56660 4.237500
## [15,] -73.57820 4.255501
## [16,] -73.57990 4.266500
## [17,] -73.57420 4.284899
## [18,] -73.56090 4.269800
## [19,] -73.55170 4.264000
## [20,] -73.54130 4.264599
## [21,] -73.52110 4.259300
## [22,] -73.47330 4.262500
## [23,] -73.45480 4.265299
## [24,] -73.44040 4.274500
## [25,] -73.41620 4.284801
## [26,] -73.38800 4.278399
## [27,] -73.36260 4.270700
## [28,] -73.32910 4.261399
## [29,] -73.29050 4.255401
## [30,] -73.25190 4.248900
## [31,] -73.20580 4.223301
## [32,] -73.18150 4.204701
## [33,] -73.15910 4.197700
## [34,] -73.14580 4.190700
## [35,] -73.13830 4.221300
## [36,] -73.10830 4.223500
## [37,] -73.09110 4.225099
## [38,] -73.08530 4.216999
## [39,] -73.07090 4.203701
## [40,] -73.06050 4.181101
## [41,] -73.06330 4.163800
## 
## 
## 
## Slot "plotOrder":
## [1] 1
## 
## Slot "labpt":
## [1] -73.322518   4.220168
## 
## Slot "ID":
## [1] "5"
## 
## Slot "area":
## [1] 0.03088037
## 
## 
## [[6]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] -73.744336   4.354279
## 
## Slot "area":
## [1] 0.01611318
## 
## Slot "hole":
## [1] FALSE
## 
## Slot "ringDir":
## [1] 1
## 
## Slot "coords":
##           [,1]     [,2]
##  [1,] -73.6382 4.371801
##  [2,] -73.6393 4.356799
##  [3,] -73.6468 4.336000
##  [4,] -73.6595 4.333201
##  [5,] -73.6664 4.320500
##  [6,] -73.6641 4.304299
##  [7,] -73.6618 4.288200
##  [8,] -73.6618 4.278299
##  [9,] -73.6623 4.269700
## [10,] -73.6802 4.277800
## [11,] -73.7177 4.301100
## [12,] -73.7356 4.305801
## [13,] -73.7534 4.305300
## [14,] -73.7702 4.305899
## [15,] -73.7996 4.305501
## [16,] -73.8024 4.315900
## [17,] -73.8048 4.325199
## [18,] -73.8065 4.341901
## [19,] -73.8105 4.352900
## [20,] -73.8203 4.360501
## [21,] -73.8290 4.365699
## [22,] -73.8353 4.376101
## [23,] -73.8348 4.381300
## [24,] -73.8302 4.394002
## [25,] -73.8261 4.407300
## [26,] -73.8233 4.415900
## [27,] -73.8117 4.429700
## [28,] -73.7962 4.447600
## [29,] -73.7852 4.442301
## [30,] -73.7748 4.431901
## [31,] -73.7645 4.421401
## [32,] -73.7547 4.407000
## [33,] -73.7477 4.393701
## [34,] -73.7414 4.377501
## [35,] -73.7327 4.366399
## [36,] -73.7200 4.357100
## [37,] -73.7027 4.366301
## [38,] -73.6860 4.369701
## [39,] -73.6734 4.373699
## [40,] -73.6601 4.376001
## [41,] -73.6382 4.371801
## 
## 
## 
## Slot "plotOrder":
## [1] 1
## 
## Slot "labpt":
## [1] -73.744336   4.354279
## 
## Slot "ID":
## [1] "6"
## 
## Slot "area":
## [1] 0.01611318
## 
## 
## 
## Slot "plotOrder":
## [1] 1 3 4 2 5 6
## 
## Slot "bbox":
##          min        max
## x -74.090500 -72.760803
## y   3.681109   4.666399
## 
## Slot "proj4string":
## CRS arguments: +proj=longlat +datum=WGS84 +no_defs
Para obtener el area de los municipios
#install.packages("lwgeom")
Cargar la libreria
library(lwgeom)
## Linking to liblwgeom 3.0.0beta1 r16016, GEOS 3.6.1, PROJ 4.9.3
Ahora, para calcular el área de cada municipio (en metros cuadrados):
mun_meta$area <- st_area(mun_meta)
Posteriormente, se crea un nuevo campo para almacenar el área en kilómetros cuadrados:
mun_meta$km2 <- mun_meta$area/(1000000)
Despues se Verifica la salida:
mun_meta$km2
## Units: [m^2]
##  [1]   975.4030   424.6030   808.6122   561.5467   379.1041   197.7810
##  [7]   719.5302   105.7460   754.1116   229.6312   764.3242 11025.7283
## [13]  6298.7609   969.2610 12455.3204  2119.5518   918.9151 16788.4326
## [19]  5576.2151  2084.8685  4061.3990   566.5750  1437.4442   834.0937
## [25]   202.6045  1686.2457  6511.0136  1369.7743  4498.9691
Nuevamente, se necesita una conversión de características simples a polígonos espaciales:
ant_mun <- as(mun_meta, 'Spatial')
Se Descomenta el siguiente código para ver qué hay dentro del objeto espacial:
#head(ant_mun)
head(ant_mun)
## An object of class "SpatialPolygonsDataFrame"
## Slot "data":
##   ID_0 ISO   NAME_0 ID_1 NAME_1 ID_2            NAME_2    TYPE_2    ENGTYPE_2
## 1   53 COL Colombia   20   Meta  683           Acacías Municipio Municipality
## 2   53 COL Colombia   20   Meta  684  Barranca de Upía Municipio Municipality
## 3   53 COL Colombia   20   Meta  685          Cabuyaro Municipio Municipality
## 4   53 COL Colombia   20   Meta  686 Castilla la Nueva Municipio Municipality
## 5   53 COL Colombia   20   Meta  687           Cumaral Municipio Municipality
## 6   53 COL Colombia   20   Meta  688       El Calvario Municipio Municipality
##   NL_NAME_2 VARNAME_2            area            km2
## 1      <NA>      <NA> 975403018 [m^2] 975.4030 [m^2]
## 2      <NA>      <NA> 424602991 [m^2] 424.6030 [m^2]
## 3      <NA>      <NA> 808612218 [m^2] 808.6122 [m^2]
## 4      <NA>      <NA> 561546739 [m^2] 561.5467 [m^2]
## 5      <NA>      <NA> 379104067 [m^2] 379.1041 [m^2]
## 6      <NA>      <NA> 197780984 [m^2] 197.7810 [m^2]
## 
## Slot "polygons":
## [[1]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] -73.780200   4.009281
## 
## Slot "area":
## [1] 0.07943183
## 
## Slot "hole":
## [1] FALSE
## 
## Slot "ringDir":
## [1] 1
## 
## Slot "coords":
##            [,1]     [,2]
##  [1,] -73.69500 3.882801
##  [2,] -73.70880 3.883400
##  [3,] -73.71570 3.891600
##  [4,] -73.73760 3.903201
##  [5,] -73.74860 3.908999
##  [6,] -73.77400 3.921801
##  [7,] -73.80110 3.933500
##  [8,] -73.83390 3.946400
##  [9,] -73.85530 3.950500
## [10,] -73.88350 3.962201
## [11,] -73.91290 3.972099
## [12,] -73.96190 3.984999
## [13,] -73.99020 3.992600
## [14,] -74.01790 4.003201
## [15,] -74.04610 4.014302
## [16,] -74.06340 4.026500
## [17,] -74.07730 4.036300
## [18,] -74.09050 4.037000
## [19,] -74.08530 4.039801
## [20,] -74.07090 4.053601
## [21,] -74.06000 4.066301
## [22,] -74.05190 4.079000
## [23,] -74.03920 4.088700
## [24,] -74.00810 4.095501
## [25,] -73.99140 4.094899
## [26,] -73.97760 4.096000
## [27,] -73.97240 4.095401
## [28,] -73.95510 4.096502
## [29,] -73.92910 4.100401
## [30,] -73.91240 4.109600
## [31,] -73.88880 4.125602
## [32,] -73.86340 4.137100
## [33,] -73.83690 4.150301
## [34,] -73.80990 4.166899
## [35,] -73.77590 4.188102
## [36,] -73.74820 4.181700
## [37,] -73.73610 4.168901
## [38,] -73.71590 4.155000
## [39,] -73.71530 4.138198
## [40,] -73.72620 4.121501
## [41,] -73.74240 4.099601
## [42,] -73.76140 4.084098
## [43,] -73.76540 4.066800
## [44,] -73.76830 4.050101
## [45,] -73.76080 4.032100
## [46,] -73.73940 4.020499
## [47,] -73.70600 4.008800
## [48,] -73.66050 4.000500
## [49,] -73.61950 3.983600
## [50,] -73.56710 3.974700
## [51,] -73.49730 3.970901
## [52,] -73.46270 3.969100
## [53,] -73.48000 3.940800
## [54,] -73.49210 3.927000
## [55,] -73.50250 3.912600
## [56,] -73.50760 3.900500
## [57,] -73.50760 3.882001
## [58,] -73.51823 3.862573
## [59,] -73.52660 3.866500
## [60,] -73.53880 3.870600
## [61,] -73.56350 3.873600
## [62,] -73.59760 3.874299
## [63,] -73.62640 3.874400
## [64,] -73.64940 3.874500
## [65,] -73.68340 3.883300
## [66,] -73.69500 3.882801
## 
## 
## 
## Slot "plotOrder":
## [1] 1
## 
## Slot "labpt":
## [1] -73.780200   4.009281
## 
## Slot "ID":
## [1] "1"
## 
## Slot "area":
## [1] 0.07943183
## 
## 
## [[2]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] -72.993668   4.474491
## 
## Slot "area":
## [1] 0.03459735
## 
## Slot "hole":
## [1] FALSE
## 
## Slot "ringDir":
## [1] 1
## 
## Slot "coords":
##           [,1]     [,2]
##  [1,] -72.8167 4.341800
##  [2,] -72.8542 4.343099
##  [3,] -72.8853 4.357100
##  [4,] -72.9268 4.352699
##  [5,] -72.9505 4.368899
##  [6,] -72.9949 4.402599
##  [7,] -73.0393 4.432900
##  [8,] -73.0508 4.434602
##  [9,] -73.0641 4.434700
## [10,] -73.0704 4.435302
## [11,] -73.0750 4.435302
## [12,] -73.0808 4.434199
## [13,] -73.0866 4.434800
## [14,] -73.0917 4.435400
## [15,] -73.0998 4.441800
## [16,] -73.1096 4.437200
## [17,] -73.0723 4.666399
## [18,] -73.0457 4.634501
## [19,] -73.0371 4.618800
## [20,] -73.0244 4.596801
## [21,] -73.0025 4.562699
## [22,] -72.9806 4.535401
## [23,] -72.9523 4.499498
## [24,] -72.9332 4.475200
## [25,] -72.9177 4.458900
## [26,] -72.8952 4.438600
## [27,] -72.8773 4.418300
## [28,] -72.8715 4.409600
## [29,] -72.8715 4.406200
## [30,] -72.8698 4.397500
## [31,] -72.8588 4.398601
## [32,] -72.8490 4.395700
## [33,] -72.8490 4.384101
## [34,] -72.8496 4.373100
## [35,] -72.8467 4.370800
## [36,] -72.8265 4.371299
## [37,] -72.8133 4.365501
## [38,] -72.8213 4.356799
## [39,] -72.8236 4.347601
## [40,] -72.8167 4.341800
## 
## 
## 
## Slot "plotOrder":
## [1] 1
## 
## Slot "labpt":
## [1] -72.993668   4.474491
## 
## Slot "ID":
## [1] "2"
## 
## Slot "area":
## [1] 0.03459735
## 
## 
## [[3]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] -72.980284   4.285326
## 
## Slot "area":
## [1] 0.06587133
## 
## Slot "hole":
## [1] FALSE
## 
## Slot "ringDir":
## [1] 1
## 
## Slot "coords":
##           [,1]     [,2]
##  [1,] -73.0633 4.163800
##  [2,] -73.0605 4.181101
##  [3,] -73.0709 4.203701
##  [4,] -73.0853 4.216999
##  [5,] -73.0911 4.225099
##  [6,] -73.1083 4.223500
##  [7,] -73.1383 4.221300
##  [8,] -73.1096 4.437200
##  [9,] -73.0998 4.441800
## [10,] -73.0917 4.435400
## [11,] -73.0866 4.434800
## [12,] -73.0808 4.434199
## [13,] -73.0750 4.435302
## [14,] -73.0704 4.435302
## [15,] -73.0641 4.434700
## [16,] -73.0508 4.434602
## [17,] -73.0393 4.432900
## [18,] -72.9949 4.402599
## [19,] -72.9505 4.368899
## [20,] -72.9268 4.352699
## [21,] -72.8853 4.357100
## [22,] -72.8542 4.343099
## [23,] -72.8167 4.341800
## [24,] -72.8081 4.330801
## [25,] -72.8138 4.323301
## [26,] -72.8086 4.316898
## [27,] -72.8017 4.313999
## [28,] -72.7919 4.312801
## [29,] -72.7810 4.305300
## [30,] -72.7729 4.293699
## [31,] -72.7717 4.287901
## [32,] -72.7700 4.279801
## [33,] -72.7677 4.265901
## [34,] -72.7608 4.252600
## [35,] -72.7723 4.241701
## [36,] -72.7861 4.235401
## [37,] -72.8000 4.240700
## [38,] -72.8138 4.242400
## [39,] -72.8282 4.235000
## [40,] -72.8455 4.234501
## [41,] -72.8622 4.240301
## [42,] -72.8760 4.227101
## [43,] -72.8806 4.216202
## [44,] -72.9002 4.205301
## [45,] -72.8968 4.192501
## [46,] -72.9066 4.174102
## [47,] -72.9129 4.177001
## [48,] -72.9279 4.181101
## [49,] -72.9331 4.160901
## [50,] -72.9446 4.141900
## [51,] -72.9740 4.137401
## [52,] -72.9844 4.140899
## [53,] -73.0005 4.140999
## [54,] -73.0172 4.149701
## [55,] -73.0362 4.142900
## [56,] -73.0403 4.147499
## [57,] -73.0403 4.156798
## [58,] -73.0449 4.162501
## [59,] -73.0633 4.163800
## 
## 
## 
## Slot "plotOrder":
## [1] 1
## 
## Slot "labpt":
## [1] -72.980284   4.285326
## 
## Slot "ID":
## [1] "3"
## 
## Slot "area":
## [1] 0.06587133
## 
## 
## [[4]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] -73.535484   3.793866
## 
## Slot "area":
## [1] 0.04571812
## 
## Slot "hole":
## [1] FALSE
## 
## Slot "ringDir":
## [1] 1
## 
## Slot "coords":
##            [,1]     [,2]
##  [1,] -73.51823 3.862573
##  [2,] -73.51450 3.857201
##  [3,] -73.49670 3.865801
##  [4,] -73.48280 3.869201
##  [5,] -73.46840 3.861101
##  [6,] -73.44480 3.866701
##  [7,] -73.42920 3.870101
##  [8,] -73.41600 3.875899
##  [9,] -73.40790 3.843501
## [10,] -73.40040 3.801301
## [11,] -73.38070 3.750401
## [12,] -73.37780 3.724900
## [13,] -73.37670 3.695501
## [14,] -73.37492 3.681109
## [15,] -73.39970 3.686300
## [16,] -73.45330 3.700401
## [17,] -73.49600 3.704600
## [18,] -73.54500 3.719899
## [19,] -73.57610 3.736200
## [20,] -73.62400 3.756600
## [21,] -73.64710 3.757799
## [22,] -73.68630 3.770100
## [23,] -73.70880 3.780600
## [24,] -73.72200 3.789399
## [25,] -73.72370 3.790500
## [26,] -73.71970 3.810101
## [27,] -73.71280 3.827399
## [28,] -73.70760 3.839000
## [29,] -73.69500 3.882801
## [30,] -73.68340 3.883300
## [31,] -73.64940 3.874500
## [32,] -73.62640 3.874400
## [33,] -73.59760 3.874299
## [34,] -73.56350 3.873600
## [35,] -73.53880 3.870600
## [36,] -73.52660 3.866500
## [37,] -73.51823 3.862573
## 
## 
## 
## Slot "plotOrder":
## [1] 1
## 
## Slot "labpt":
## [1] -73.535484   3.793866
## 
## Slot "ID":
## [1] "4"
## 
## Slot "area":
## [1] 0.04571812
## 
## 
## [[5]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] -73.322518   4.220168
## 
## Slot "area":
## [1] 0.03088037
## 
## Slot "hole":
## [1] FALSE
## 
## Slot "ringDir":
## [1] 1
## 
## Slot "coords":
##            [,1]     [,2]
##  [1,] -73.06330 4.163800
##  [2,] -73.07140 4.155201
##  [3,] -73.08580 4.152901
##  [4,] -73.10430 4.151201
##  [5,] -73.11816 4.150319
##  [6,] -73.16480 4.169400
##  [7,] -73.23740 4.178999
##  [8,] -73.31010 4.190801
##  [9,] -73.37520 4.195701
## [10,] -73.42600 4.205699
## [11,] -73.48710 4.206000
## [12,] -73.53430 4.209699
## [13,] -73.55800 4.220698
## [14,] -73.56660 4.237500
## [15,] -73.57820 4.255501
## [16,] -73.57990 4.266500
## [17,] -73.57420 4.284899
## [18,] -73.56090 4.269800
## [19,] -73.55170 4.264000
## [20,] -73.54130 4.264599
## [21,] -73.52110 4.259300
## [22,] -73.47330 4.262500
## [23,] -73.45480 4.265299
## [24,] -73.44040 4.274500
## [25,] -73.41620 4.284801
## [26,] -73.38800 4.278399
## [27,] -73.36260 4.270700
## [28,] -73.32910 4.261399
## [29,] -73.29050 4.255401
## [30,] -73.25190 4.248900
## [31,] -73.20580 4.223301
## [32,] -73.18150 4.204701
## [33,] -73.15910 4.197700
## [34,] -73.14580 4.190700
## [35,] -73.13830 4.221300
## [36,] -73.10830 4.223500
## [37,] -73.09110 4.225099
## [38,] -73.08530 4.216999
## [39,] -73.07090 4.203701
## [40,] -73.06050 4.181101
## [41,] -73.06330 4.163800
## 
## 
## 
## Slot "plotOrder":
## [1] 1
## 
## Slot "labpt":
## [1] -73.322518   4.220168
## 
## Slot "ID":
## [1] "5"
## 
## Slot "area":
## [1] 0.03088037
## 
## 
## [[6]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] -73.744336   4.354279
## 
## Slot "area":
## [1] 0.01611318
## 
## Slot "hole":
## [1] FALSE
## 
## Slot "ringDir":
## [1] 1
## 
## Slot "coords":
##           [,1]     [,2]
##  [1,] -73.6382 4.371801
##  [2,] -73.6393 4.356799
##  [3,] -73.6468 4.336000
##  [4,] -73.6595 4.333201
##  [5,] -73.6664 4.320500
##  [6,] -73.6641 4.304299
##  [7,] -73.6618 4.288200
##  [8,] -73.6618 4.278299
##  [9,] -73.6623 4.269700
## [10,] -73.6802 4.277800
## [11,] -73.7177 4.301100
## [12,] -73.7356 4.305801
## [13,] -73.7534 4.305300
## [14,] -73.7702 4.305899
## [15,] -73.7996 4.305501
## [16,] -73.8024 4.315900
## [17,] -73.8048 4.325199
## [18,] -73.8065 4.341901
## [19,] -73.8105 4.352900
## [20,] -73.8203 4.360501
## [21,] -73.8290 4.365699
## [22,] -73.8353 4.376101
## [23,] -73.8348 4.381300
## [24,] -73.8302 4.394002
## [25,] -73.8261 4.407300
## [26,] -73.8233 4.415900
## [27,] -73.8117 4.429700
## [28,] -73.7962 4.447600
## [29,] -73.7852 4.442301
## [30,] -73.7748 4.431901
## [31,] -73.7645 4.421401
## [32,] -73.7547 4.407000
## [33,] -73.7477 4.393701
## [34,] -73.7414 4.377501
## [35,] -73.7327 4.366399
## [36,] -73.7200 4.357100
## [37,] -73.7027 4.366301
## [38,] -73.6860 4.369701
## [39,] -73.6734 4.373699
## [40,] -73.6601 4.376001
## [41,] -73.6382 4.371801
## 
## 
## 
## Slot "plotOrder":
## [1] 1
## 
## Slot "labpt":
## [1] -73.744336   4.354279
## 
## Slot "ID":
## [1] "6"
## 
## Slot "area":
## [1] 0.01611318
## 
## 
## 
## Slot "plotOrder":
## [1] 1 3 4 2 5 6
## 
## Slot "bbox":
##          min        max
## x -74.090500 -72.760803
## y   3.681109   4.666399
## 
## Slot "proj4string":
## CRS arguments: +proj=longlat +datum=WGS84 +no_defs
Ahora, se prepara la trama:
bins <- c(0, 50, 100, 200, 300, 500, 1000, 2000, Inf)
pal <- colorBin("YlOrRd", domain = ant_mun$km2, bins = bins)


labels <- mun_meta$NAME_2

labels
##  [1] "Acacías"              "Barranca de Upía"     "Cabuyaro"            
##  [4] "Castilla la Nueva"    "Cumaral"              "El Calvario"         
##  [7] "El Castillo"          "El Dorado"            "Fuente de Oro"       
## [10] "Granada"              "Guamal"               "La Macarena"         
## [13] "La Uribe"             "Lejanías"             "Mapiripán"           
## [16] "Mesetas"              "Puerto Concordia"     "Puerto Gaitán"       
## [19] "Puerto López"         "Puerto Lleras"        "Puerto Rico"         
## [22] "Restrepo"             "San Carlos de Guaroa" "San Juan de Arama"   
## [25] "San Juanito"          "San Luis de Cubarral" "San Martín"          
## [28] "Villavicencio"        "Vista Hermosa"
Entonces, se crea el plot:
m <- leaflet(ant_mun) %>%
  setView(-72.7, 3.5, 6.9)  %>% addPolygons(
  fillColor = ~pal(km2),
  weight = 2,
  opacity = 1,
  color = "white",
  dashArray = "3",
  fillOpacity = 0.7,
  highlight = highlightOptions(
    weight = 5,
    color = "#666",
    dashArray = "",
    fillOpacity = 0.7,
    bringToFront = TRUE),
  label = labels) %>%
  addLegend(pal = pal, values = ~km2, opacity = 0.7, title = NULL,
    position = "bottomright")
Visualización del plot:
m
Otra forma que puede utilizar para trazar, la cual Puede ser más simple es:
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
  )
Por último, si se quiere visualizar la capital del departamento puede descomente el siguiente fragmento y completar el código
#leaflet() %>%
#  addProviderTiles(providers$
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)%>%  setView(lng = -73.6226403, lat = 4.1220002, zoom = 14.5) %>%
addMarkers(lng = -73.6226403, lat = 4.1220002, popup = "Villavicencio")