1. Introducción

Este cuaderno de R Markdown, ilustra las funciones básicas proporcionadas por las simple features (sf) y las tidyverse libraries. El objetivo es difundir las funcionalidades geoespaciales de R para los estudiantes de Geomática Básica en la Universidad Nacional de Colombia. Para esto se seguirán los siguientes pasos:

- Instalar liberías necesarias:

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

- Cargar la librería

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.5
## 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 vectoriales

Se podrá leer el shapefile descargado previamente de DIVA-GIS, que representa a los departamentos que conforman el territorio Colombiano:

deptos = read_sf("C:/Users/Brian/Desktop/Daniela/COL_adm/COL_adm1.shp")

Para saber qué contiene el objeto deptos:

head(deptos)
## Simple feature collection with 6 features and 9 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -77.149 ymin: -4.228429 xmax: -69.36835 ymax: 11.10792
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
## # A tibble: 6 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>     
## # ... with 1 more variable: geometry <MULTIPOLYGON [°]>

Es de utilidad revisar las funciones básicas proporcionadas por la sf library. Ejemplo: Para saber 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. Usando ggplot para visualizar datos geoespaciales

Un buen punto de partida para comprender los datos es trazar los datos. Se puede usar las funcionalidades de ggplot:

ggplot() + geom_sf(data = deptos)

Es posible 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. Puede encontrar más información sobre los códigos EPSG aquí:

# El CRS es usado en Canada
  ggplot() + geom_sf(data = deptos) + coord_sf(crs=st_crs(3978))

Busque las propiedades del CRS con el código EPSG 32618. Corresponde a UTM 18 N. En caso de que necesitemos usar dicho CRS, es necesario convertir el objeto espacial de EPSG4326 a EPSG: 32618.

deptos_utm = st_transform(deptos, crs = st_crs(32618))
deptos_utm
## Simple feature collection with 32 features and 9 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -245935.3 ymin: -469204.3 xmax: 1407491 ymax: 1763314
## epsg (SRID):    32618
## proj4string:    +proj=utm +zone=18 +datum=WGS84 +units=m +no_defs
## # 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 [m]>
ggplot() + geom_sf(data = deptos_utm)

4. Filtrar datos geoespaciales basados en atributos

Como solo estoy interesada en el departamento de Boyacá, puedo filtrar los datos:

boyaca  = deptos %>% filter(NAME_1 == "Boyacá")

Tracemos el nuevo objeto:

ggplot() + geom_sf(data = boyaca)

Podemos repetir los pasos anteriores para cargar los municipios colombianos y filtrar los Boyacenses:

munic =  read_sf("C:/Users/Brian/Desktop/Daniela/COL_adm/COL_adm2.shp")
mun_boyaca = munic %>% filter(NAME_1 == "Boyacá")
ggplot() + geom_sf(data = mun_boyaca) 

mun_boyaca
## Simple feature collection with 123 features and 11 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -74.6743 ymin: 4.642001 xmax: -71.98309 ymax: 7.0275
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
## # A tibble: 123 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~     6 Boyacá   199 Almei~ Munic~ Municipa~ <NA>     
##  2    53 COL   Colom~     6 Boyacá   200 Aquit~ Munic~ Municipa~ <NA>     
##  3    53 COL   Colom~     6 Boyacá   201 Arcab~ Munic~ Municipa~ <NA>     
##  4    53 COL   Colom~     6 Boyacá   202 Belén  <NA>   <NA>      <NA>     
##  5    53 COL   Colom~     6 Boyacá   203 Berbeo Munic~ Municipa~ <NA>     
##  6    53 COL   Colom~     6 Boyacá   204 Betei~ Munic~ Municipa~ <NA>     
##  7    53 COL   Colom~     6 Boyacá   205 Boavi~ Munic~ Municipa~ <NA>     
##  8    53 COL   Colom~     6 Boyacá   206 Boyacá Munic~ Municipa~ <NA>     
##  9    53 COL   Colom~     6 Boyacá   207 Brice~ Munic~ Municipa~ <NA>     
## 10    53 COL   Colom~     6 Boyacá   208 Buena~ Munic~ Municipa~ <NA>     
## # ... with 113 more rows, and 2 more variables: VARNAME_2 <chr>,
## #   geometry <MULTIPOLYGON [°]>
boyaca_points = st_centroid(mun_boyaca)
## Warning in st_centroid.sf(mun_boyaca): 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
boyaca_points = cbind(mun_boyaca, st_coordinates(st_centroid(mun_boyaca$geometry)))
## Warning in st_centroid.sfc(mun_boyaca$geometry): st_centroid does not give
## correct centroids for longitude/latitude data

Se puede producir un mejor gráfico utilizando el siguiente fragmento:

ggplot(boyaca) +
    geom_sf() +
    geom_sf(data = boyaca_points, fill = "antiquewhite") + 
    geom_text(data = boyaca_points, aes(x=X, y=Y,label = ID_2), size = 2) +
    coord_sf(xlim = c(-75, -71.7), ylim = c(4.5, 7.1), 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(boyaca) + 
  geom_sf(data=boyaca_points, aes(x=X, y=Y, fill =
                                       ID_2), color = "black", size = 0.25) +
  geom_text(data = boyaca_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="Another  Map of Boyacá")
## Warning: Ignoring unknown aesthetics: x, y

Sin embargo, esta visualización no es un mapa real. De todos modos, la salida se puede guardar como un pdf o como un png.

ggsave("boyaca_municipios.pdf")
## Saving 7 x 5 in image
ggsave("map_boyaca.png", width = 6, height = 6, dpi = "screen")

5. Usando leaflet para visualizar datos

- Primero, instalemos la biblioteca requerida:

#install.packages("leaflet")

- Ahora. cargar la librería

library(leaflet)

- Para usar la biblioteca, necesitamos convertir de características simples a puntos espaciales.

ant_points = as(boyaca_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 NL_NAME_2
## 1   53 COL Colombia    6 Boyacá  199   Almeida Municipio Municipality      <NA>
## 2   53 COL Colombia    6 Boyacá  200 Aquitania Municipio Municipality      <NA>
## 3   53 COL Colombia    6 Boyacá  201  Arcabuco Municipio Municipality      <NA>
## 4   53 COL Colombia    6 Boyacá  202     Belén      <NA>         <NA>      <NA>
## 5   53 COL Colombia    6 Boyacá  203    Berbeo Municipio Municipality      <NA>
## 6   53 COL Colombia    6 Boyacá  204 Beteitiva Municipio Municipality      <NA>
##   VARNAME_2         X        Y
## 1      <NA> -73.41131 4.922605
## 2      <NA> -72.90192 5.403410
## 3      <NA> -73.46782 5.723896
## 4      <NA> -72.92481 5.976306
## 5      <NA> -73.12606 5.185135
## 6      <NA> -72.87078 5.894272
## 
## Slot "polygons":
## [[1]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] -73.411308   4.922605
## 
## Slot "area":
## [1] 0.00391144
## 
## Slot "hole":
## [1] FALSE
## 
## Slot "ringDir":
## [1] 1
## 
## Slot "coords":
##           [,1]     [,2]
##  [1,] -73.3883 4.900501
##  [2,] -73.4033 4.888401
##  [3,] -73.4206 4.876900
##  [4,] -73.4315 4.878101
##  [5,] -73.4436 4.874701
##  [6,] -73.4598 4.871301
##  [7,] -73.4488 4.875900
##  [8,] -73.4465 4.882199
##  [9,] -73.4436 4.893800
## [10,] -73.4385 4.910502
## [11,] -73.4339 4.922000
## [12,] -73.4241 4.931800
## [13,] -73.4252 4.941001
## [14,] -73.4120 4.961800
## [15,] -73.4114 4.971001
## [16,] -73.4097 4.981401
## [17,] -73.3953 4.980799
## [18,] -73.3935 4.969201
## [19,] -73.3860 4.957600
## [20,] -73.3780 4.933300
## [21,] -73.3883 4.920701
## [22,] -73.3970 4.905102
## [23,] -73.3883 4.900501
## 
## 
## 
## Slot "plotOrder":
## [1] 1
## 
## Slot "labpt":
## [1] -73.411308   4.922605
## 
## Slot "ID":
## [1] "1"
## 
## Slot "area":
## [1] 0.00391144
## 
## 
## [[2]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] -72.90192   5.40341
## 
## Slot "area":
## [1] 0.07373845
## 
## Slot "hole":
## [1] FALSE
## 
## Slot "ringDir":
## [1] 1
## 
## Slot "coords":
##            [,1]     [,2]
##   [1,] -73.0553 5.276800
##   [2,] -73.0571 5.282600
##   [3,] -73.0582 5.289000
##   [4,] -73.0582 5.297599
##   [5,] -73.0513 5.308001
##   [6,] -73.0484 5.315499
##   [7,] -73.0536 5.327100
##   [8,] -73.0629 5.333999
##   [9,] -73.0600 5.339802
##  [10,] -73.0490 5.349001
##  [11,] -73.0392 5.351300
##  [12,] -73.0387 5.360501
##  [13,] -73.0387 5.366901
##  [14,] -73.0323 5.371500
##  [15,] -73.0254 5.381800
##  [16,] -73.0237 5.393400
##  [17,] -73.0226 5.403201
##  [18,] -73.0128 5.415900
##  [19,] -73.0076 5.429101
##  [20,] -73.0030 5.442399
##  [21,] -72.9868 5.460200
##  [22,] -72.9782 5.474100
##  [23,] -72.9701 5.486701
##  [24,] -72.9580 5.498799
##  [25,] -72.9511 5.508599
##  [26,] -72.9448 5.517800
##  [27,] -72.9425 5.517800
##  [28,] -72.9356 5.530499
##  [29,] -72.9287 5.536801
##  [30,] -72.9252 5.543101
##  [31,] -72.9166 5.561601
##  [32,] -72.9125 5.572499
##  [33,] -72.9097 5.575401
##  [34,] -72.8872 5.571900
##  [35,] -72.8768 5.571200
##  [36,] -72.8716 5.570601
##  [37,] -72.8630 5.566002
##  [38,] -72.8543 5.560201
##  [39,] -72.8428 5.557200
##  [40,] -72.8365 5.563000
##  [41,] -72.8278 5.586099
##  [42,] -72.8151 5.605602
##  [43,] -72.8082 5.624101
##  [44,] -72.8013 5.613699
##  [45,] -72.7909 5.606700
##  [46,] -72.7811 5.596800
##  [47,] -72.7725 5.583501
##  [48,] -72.7656 5.577099
##  [49,] -72.7598 5.574200
##  [50,] -72.7604 5.564399
##  [51,] -72.7609 5.552901
##  [52,] -72.7603 5.539001
##  [53,] -72.7540 5.523901
##  [54,] -72.7494 5.504301
##  [55,] -72.7471 5.491601
##  [56,] -72.7378 5.480001
##  [57,] -72.7321 5.472400
##  [58,] -72.7222 5.447600
##  [59,] -72.7159 5.434299
##  [60,] -72.7061 5.425001
##  [61,] -72.7101 5.422701
##  [62,] -72.7107 5.415699
##  [63,] -72.7176 5.404801
##  [64,] -72.7291 5.404801
##  [65,] -72.7418 5.415900
##  [66,] -72.7516 5.426300
##  [67,] -72.7591 5.429201
##  [68,] -72.7695 5.427499
##  [69,] -72.7804 5.413099
##  [70,] -72.7977 5.405701
##  [71,] -72.8098 5.397701
##  [72,] -72.8202 5.384999
##  [73,] -72.8208 5.373501
##  [74,] -72.8196 5.363102
##  [75,] -72.8208 5.358402
##  [76,] -72.8317 5.341201
##  [77,] -72.8507 5.328001
##  [78,] -72.8605 5.321601
##  [79,] -72.8703 5.311299
##  [80,] -72.8824 5.289401
##  [81,] -72.8974 5.279601
##  [82,] -72.9129 5.259501
##  [83,] -72.9320 5.242200
##  [84,] -72.9515 5.226700
##  [85,] -72.9613 5.221000
##  [86,] -72.9717 5.210101
##  [87,] -72.9648 5.205401
##  [88,] -72.9625 5.199601
##  [89,] -72.9590 5.195601
##  [90,] -72.9590 5.188600
##  [91,] -72.9584 5.181101
##  [92,] -72.9671 5.180000
##  [93,] -72.9734 5.179401
##  [94,] -72.9832 5.176000
##  [95,] -72.9890 5.176600
##  [96,] -73.0051 5.176700
##  [97,] -73.0109 5.180201
##  [98,] -73.0270 5.183100
##  [99,] -73.0328 5.184301
## [100,] -73.0317 5.202201
## [101,] -73.0271 5.224099
## [102,] -73.0305 5.235700
## [103,] -73.0438 5.244400
## [104,] -73.0490 5.252500
## [105,] -73.0542 5.265801
## [106,] -73.0553 5.276800
## 
## 
## 
## Slot "plotOrder":
## [1] 1
## 
## Slot "labpt":
## [1] -72.90192   5.40341
## 
## Slot "ID":
## [1] "2"
## 
## Slot "area":
## [1] 0.07373845
## 
## 
## [[3]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] -73.467818   5.723896
## 
## Slot "area":
## [1] 0.01332427
## 
## Slot "hole":
## [1] FALSE
## 
## Slot "ringDir":
## [1] 1
## 
## Slot "coords":
##            [,1]     [,2]
##  [1,] -73.46480 5.675400
##  [2,] -73.47290 5.682901
##  [3,] -73.49020 5.688200
##  [4,] -73.50750 5.691101
##  [5,] -73.51790 5.692300
##  [6,] -73.52420 5.694100
##  [7,] -73.52420 5.697601
##  [8,] -73.52890 5.704500
##  [9,] -73.52310 5.726400
## [10,] -73.53060 5.731100
## [11,] -73.53920 5.732299
## [12,] -73.54160 5.745000
## [13,] -73.54040 5.750801
## [14,] -73.53810 5.759400
## [15,] -73.53120 5.761100
## [16,] -73.52250 5.766301
## [17,] -73.52020 5.775500
## [18,] -73.52200 5.784801
## [19,] -73.52720 5.793500
## [20,] -73.52890 5.802100
## [21,] -73.53060 5.818899
## [22,] -73.52600 5.821800
## [23,] -73.51160 5.814199
## [24,] -73.50350 5.799700
## [25,] -73.49490 5.790401
## [26,] -73.48620 5.789300
## [27,] -73.47640 5.797302
## [28,] -73.47010 5.792101
## [29,] -73.46320 5.790300
## [30,] -73.46150 5.789099
## [31,] -73.45630 5.778200
## [32,] -73.45340 5.770600
## [33,] -73.45220 5.768300
## [34,] -73.44990 5.762000
## [35,] -73.44760 5.755001
## [36,] -73.44700 5.751500
## [37,] -73.43840 5.745201
## [38,] -73.42970 5.740501
## [39,] -73.42110 5.724898
## [40,] -73.41300 5.713300
## [41,] -73.40380 5.704600
## [42,] -73.39630 5.692400
## [43,] -73.39450 5.684900
## [44,] -73.39110 5.678500
## [45,] -73.38820 5.674500
## [46,] -73.38470 5.669301
## [47,] -73.38470 5.660000
## [48,] -73.38990 5.649600
## [49,] -73.39050 5.643900
## [50,] -73.39293 5.630946
## [51,] -73.39560 5.631201
## [52,] -73.40550 5.635301
## [53,] -73.41350 5.646899
## [54,] -73.42390 5.652100
## [55,] -73.43310 5.658500
## [56,] -73.44180 5.662600
## [57,] -73.44470 5.671200
## [58,] -73.45620 5.675300
## [59,] -73.46480 5.675400
## 
## 
## 
## Slot "plotOrder":
## [1] 1
## 
## Slot "labpt":
## [1] -73.467818   5.723896
## 
## Slot "ID":
## [1] "3"
## 
## Slot "area":
## [1] 0.01332427
## 
## 
## [[4]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] -72.924811   5.976306
## 
## Slot "area":
## [1] 0.01263091
## 
## Slot "hole":
## [1] FALSE
## 
## Slot "ringDir":
## [1] 1
## 
## Slot "coords":
##           [,1]     [,2]
##  [1,] -72.8194 5.951100
##  [2,] -72.8205 5.947600
##  [3,] -72.8355 5.947100
##  [4,] -72.8459 5.941899
##  [5,] -72.8609 5.934501
##  [6,] -72.8718 5.930501
##  [7,] -72.8868 5.928300
##  [8,] -72.9030 5.926600
##  [9,] -72.9127 5.924300
## [10,] -72.9202 5.923801
## [11,] -72.9295 5.919799
## [12,] -72.9387 5.920400
## [13,] -72.9427 5.922101
## [14,] -72.9508 5.925100
## [15,] -72.9508 5.932601
## [16,] -72.9554 5.938399
## [17,] -72.9693 5.945899
## [18,] -72.9773 5.946000
## [19,] -72.9796 5.949400
## [20,] -72.9854 5.956399
## [21,] -72.9894 5.961600
## [22,] -72.9917 5.963900
## [23,] -73.0027 5.987701
## [24,] -73.0016 5.992300
## [25,] -73.0004 5.996300
## [26,] -72.9975 6.000899
## [27,] -72.9935 6.010100
## [28,] -72.9877 6.016500
## [29,] -72.9843 6.024001
## [30,] -72.9797 6.032600
## [31,] -72.9751 6.037801
## [32,] -72.9722 6.040101
## [33,] -72.9664 6.045302
## [34,] -72.9601 6.045801
## [35,] -72.9543 6.052700
## [36,] -72.9474 6.058500
## [37,] -72.9365 6.044600
## [38,] -72.9318 6.032399
## [39,] -72.9272 6.024901
## [40,] -72.9186 6.013300
## [41,] -72.9076 6.003400
## [42,] -72.8995 5.994201
## [43,] -72.8920 5.984900
## [44,] -72.8828 5.977901
## [45,] -72.8626 5.977301
## [46,] -72.8534 5.976599
## [47,] -72.8465 5.976599
## [48,] -72.8407 5.976599
## [49,] -72.8332 5.978299
## [50,] -72.8321 5.975400
## [51,] -72.8246 5.971900
## [52,] -72.8165 5.965502
## [53,] -72.8125 5.956800
## [54,] -72.8165 5.953999
## [55,] -72.8194 5.951100
## 
## 
## 
## Slot "plotOrder":
## [1] 1
## 
## Slot "labpt":
## [1] -72.924811   5.976306
## 
## Slot "ID":
## [1] "4"
## 
## Slot "area":
## [1] 0.01263091
## 
## 
## [[5]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] -73.126058   5.185135
## 
## Slot "area":
## [1] 0.0051261
## 
## Slot "hole":
## [1] FALSE
## 
## Slot "ringDir":
## [1] 1
## 
## Slot "coords":
##           [,1]     [,2]
##  [1,] -73.0639 5.140501
##  [2,] -73.0795 5.133700
##  [3,] -73.1221 5.124000
##  [4,] -73.1239 5.130399
##  [5,] -73.1285 5.133900
##  [6,] -73.1406 5.153002
##  [7,] -73.1440 5.157100
##  [8,] -73.1527 5.173300
##  [9,] -73.1596 5.180800
## [10,] -73.1648 5.186001
## [11,] -73.1735 5.197000
## [12,] -73.1740 5.203400
## [13,] -73.1637 5.208000
## [14,] -73.1521 5.216601
## [15,] -73.1441 5.221699
## [16,] -73.1331 5.232101
## [17,] -73.1222 5.242401
## [18,] -73.1026 5.258501
## [19,] -73.1003 5.260800
## [20,] -73.0980 5.256800
## [21,] -73.0980 5.251000
## [22,] -73.0997 5.245801
## [23,] -73.1014 5.236600
## [24,] -73.0985 5.231399
## [25,] -73.0985 5.225000
## [26,] -73.1089 5.219299
## [27,] -73.1187 5.213599
## [28,] -73.1227 5.208398
## [29,] -73.1204 5.200301
## [30,] -73.1164 5.195601
## [31,] -73.1164 5.189301
## [32,] -73.1164 5.177701
## [33,] -73.1095 5.165001
## [34,] -73.1043 5.152801
## [35,] -73.0945 5.147001
## [36,] -73.0858 5.144099
## [37,] -73.0737 5.144099
## [38,] -73.0639 5.140501
## 
## 
## 
## Slot "plotOrder":
## [1] 1
## 
## Slot "labpt":
## [1] -73.126058   5.185135
## 
## Slot "ID":
## [1] "5"
## 
## Slot "area":
## [1] 0.0051261
## 
## 
## [[6]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] -72.870782   5.894272
## 
## Slot "area":
## [1] 0.01047862
## 
## Slot "hole":
## [1] FALSE
## 
## Slot "ringDir":
## [1] 1
## 
## Slot "coords":
##           [,1]     [,2]
##  [1,] -72.8499 5.849000
##  [2,] -72.8585 5.847798
##  [3,] -72.8660 5.846101
##  [4,] -72.8758 5.845000
##  [5,] -72.8793 5.840400
##  [6,] -72.8833 5.833501
##  [7,] -72.8856 5.832401
##  [8,] -72.8902 5.832401
##  [9,] -72.8971 5.832401
## [10,] -72.9023 5.830101
## [11,] -72.9046 5.827200
## [12,] -72.9098 5.825499
## [13,] -72.9133 5.853300
## [14,] -72.9202 5.863700
## [15,] -72.9254 5.874100
## [16,] -72.9335 5.882200
## [17,] -72.9410 5.889801
## [18,] -72.9462 5.894400
## [19,] -72.9554 5.900200
## [20,] -72.9439 5.905399
## [21,] -72.9404 5.908800
## [22,] -72.9364 5.908800
## [23,] -72.9295 5.919799
## [24,] -72.9202 5.923801
## [25,] -72.9127 5.924300
## [26,] -72.9030 5.926600
## [27,] -72.8868 5.928300
## [28,] -72.8718 5.930501
## [29,] -72.8609 5.934501
## [30,] -72.8459 5.941899
## [31,] -72.8355 5.947100
## [32,] -72.8205 5.947600
## [33,] -72.8194 5.951100
## [34,] -72.8125 5.949899
## [35,] -72.8078 5.948101
## [36,] -72.7998 5.947000
## [37,] -72.7969 5.944600
## [38,] -72.7957 5.938900
## [39,] -72.7963 5.934800
## [40,] -72.8009 5.923299
## [41,] -72.8032 5.917501
## [42,] -72.8101 5.903701
## [43,] -72.8130 5.894999
## [44,] -72.8176 5.888100
## [45,] -72.8274 5.874300
## [46,] -72.8384 5.862200
## [47,] -72.8453 5.855301
## [48,] -72.8499 5.849000
## 
## 
## 
## Slot "plotOrder":
## [1] 1
## 
## Slot "labpt":
## [1] -72.870782   5.894272
## 
## Slot "ID":
## [1] "6"
## 
## Slot "area":
## [1] 0.01047862
## 
## 
## 
## Slot "plotOrder":
## [1] 2 3 4 6 5 1
## 
## Slot "bbox":
##          min      max
## x -73.541603 -72.7061
## y   4.871301   6.0585
## 
## Slot "proj4string":
## CRS arguments: +proj=longlat +datum=WGS84 +no_defs

- Obtener área de municipios:

#install.packages("lwgeom")

- Abriendo la librería

library(lwgeom)
## Linking to liblwgeom 3.0.0beta1 r16016, GEOS 3.6.1, PROJ 4.9.3

- Calculemos el área de cada municipio (en metros cuadrados):

mun_boyaca$area = st_area(mun_boyaca)  #Cuidar unidades

- Ahora, creemos un nuevo campo para almacenar el área en kilómetros cuadrados:

mun_boyaca$km2 = mun_boyaca$area/(1000000)

- Verifique la salida:

mun_boyaca$km2
## Units: [m^2]
##   [1]   47.97334  903.72455  163.21342  154.65223   62.84607  128.31822
##   [7]  178.59268   58.05340   54.03198   87.85452   15.85462  187.07899
##  [13]   88.56730  310.79600   54.64079   38.83464  151.41470  129.89509
##  [19]  731.32186  562.80458  135.06323   60.90598  108.15644   52.28278
##  [25]  174.95417   63.59134   97.31592   46.33178 1313.56649   33.22368
##  [31]  281.12784  180.85505   79.60334   91.00209   98.67122  128.02692
##  [37]   89.49661  201.72954   68.40413   37.89154   72.28849  879.95611
##  [43]   44.76551   45.96802  132.97290   62.16379  198.61630   46.02968
##  [49]  633.58878  187.40220  162.25746  245.93149  360.93416   56.85137
##  [55]  212.81716   65.18185  170.87576   51.26003   39.85799   54.30010
##  [61]  510.96286  354.22549   62.55822  416.26337  272.21976   39.32367
##  [67]  279.20117  570.62090  111.16939  292.34229  333.01586 1439.18169
##  [73]   95.52294  207.74829  139.87297  186.89415   65.52678  283.85552
##  [79]  165.19258  104.00139  100.25682  262.71646  117.81661  104.96359
##  [85]  183.04830  492.44555   74.69779   74.58023   72.34770  141.61040
##  [91]   56.11386  148.56929  140.99821  180.27338  636.50190  207.20233
##  [97]   78.82340   47.15936   30.35042  281.75359  213.50974  105.61560
## [103]   33.13001  207.51419   37.78645  112.62569  115.79385   72.96197
## [109]   71.00005  156.43187   80.61687   34.04449  182.49107  221.20266
## [115]   27.95687   91.71484  149.88357  152.57119  153.24558  183.91159
## [121]  112.90525   59.88663  260.54623

- Nuevamente, necesitamos una conversión de características simples a polígonos espaciales:

ant_mun = as(mun_boyaca, 'Spatial')

- Descomente 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 NL_NAME_2
## 1   53 COL Colombia    6 Boyacá  199   Almeida Municipio Municipality      <NA>
## 2   53 COL Colombia    6 Boyacá  200 Aquitania Municipio Municipality      <NA>
## 3   53 COL Colombia    6 Boyacá  201  Arcabuco Municipio Municipality      <NA>
## 4   53 COL Colombia    6 Boyacá  202     Belén      <NA>         <NA>      <NA>
## 5   53 COL Colombia    6 Boyacá  203    Berbeo Municipio Municipality      <NA>
## 6   53 COL Colombia    6 Boyacá  204 Beteitiva Municipio Municipality      <NA>
##   VARNAME_2            area             km2
## 1      <NA>  47973340 [m^2]  47.97334 [m^2]
## 2      <NA> 903724548 [m^2] 903.72455 [m^2]
## 3      <NA> 163213422 [m^2] 163.21342 [m^2]
## 4      <NA> 154652234 [m^2] 154.65223 [m^2]
## 5      <NA>  62846073 [m^2]  62.84607 [m^2]
## 6      <NA> 128318216 [m^2] 128.31822 [m^2]
## 
## Slot "polygons":
## [[1]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] -73.411308   4.922605
## 
## Slot "area":
## [1] 0.00391144
## 
## Slot "hole":
## [1] FALSE
## 
## Slot "ringDir":
## [1] 1
## 
## Slot "coords":
##           [,1]     [,2]
##  [1,] -73.3883 4.900501
##  [2,] -73.4033 4.888401
##  [3,] -73.4206 4.876900
##  [4,] -73.4315 4.878101
##  [5,] -73.4436 4.874701
##  [6,] -73.4598 4.871301
##  [7,] -73.4488 4.875900
##  [8,] -73.4465 4.882199
##  [9,] -73.4436 4.893800
## [10,] -73.4385 4.910502
## [11,] -73.4339 4.922000
## [12,] -73.4241 4.931800
## [13,] -73.4252 4.941001
## [14,] -73.4120 4.961800
## [15,] -73.4114 4.971001
## [16,] -73.4097 4.981401
## [17,] -73.3953 4.980799
## [18,] -73.3935 4.969201
## [19,] -73.3860 4.957600
## [20,] -73.3780 4.933300
## [21,] -73.3883 4.920701
## [22,] -73.3970 4.905102
## [23,] -73.3883 4.900501
## 
## 
## 
## Slot "plotOrder":
## [1] 1
## 
## Slot "labpt":
## [1] -73.411308   4.922605
## 
## Slot "ID":
## [1] "1"
## 
## Slot "area":
## [1] 0.00391144
## 
## 
## [[2]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] -72.90192   5.40341
## 
## Slot "area":
## [1] 0.07373845
## 
## Slot "hole":
## [1] FALSE
## 
## Slot "ringDir":
## [1] 1
## 
## Slot "coords":
##            [,1]     [,2]
##   [1,] -73.0553 5.276800
##   [2,] -73.0571 5.282600
##   [3,] -73.0582 5.289000
##   [4,] -73.0582 5.297599
##   [5,] -73.0513 5.308001
##   [6,] -73.0484 5.315499
##   [7,] -73.0536 5.327100
##   [8,] -73.0629 5.333999
##   [9,] -73.0600 5.339802
##  [10,] -73.0490 5.349001
##  [11,] -73.0392 5.351300
##  [12,] -73.0387 5.360501
##  [13,] -73.0387 5.366901
##  [14,] -73.0323 5.371500
##  [15,] -73.0254 5.381800
##  [16,] -73.0237 5.393400
##  [17,] -73.0226 5.403201
##  [18,] -73.0128 5.415900
##  [19,] -73.0076 5.429101
##  [20,] -73.0030 5.442399
##  [21,] -72.9868 5.460200
##  [22,] -72.9782 5.474100
##  [23,] -72.9701 5.486701
##  [24,] -72.9580 5.498799
##  [25,] -72.9511 5.508599
##  [26,] -72.9448 5.517800
##  [27,] -72.9425 5.517800
##  [28,] -72.9356 5.530499
##  [29,] -72.9287 5.536801
##  [30,] -72.9252 5.543101
##  [31,] -72.9166 5.561601
##  [32,] -72.9125 5.572499
##  [33,] -72.9097 5.575401
##  [34,] -72.8872 5.571900
##  [35,] -72.8768 5.571200
##  [36,] -72.8716 5.570601
##  [37,] -72.8630 5.566002
##  [38,] -72.8543 5.560201
##  [39,] -72.8428 5.557200
##  [40,] -72.8365 5.563000
##  [41,] -72.8278 5.586099
##  [42,] -72.8151 5.605602
##  [43,] -72.8082 5.624101
##  [44,] -72.8013 5.613699
##  [45,] -72.7909 5.606700
##  [46,] -72.7811 5.596800
##  [47,] -72.7725 5.583501
##  [48,] -72.7656 5.577099
##  [49,] -72.7598 5.574200
##  [50,] -72.7604 5.564399
##  [51,] -72.7609 5.552901
##  [52,] -72.7603 5.539001
##  [53,] -72.7540 5.523901
##  [54,] -72.7494 5.504301
##  [55,] -72.7471 5.491601
##  [56,] -72.7378 5.480001
##  [57,] -72.7321 5.472400
##  [58,] -72.7222 5.447600
##  [59,] -72.7159 5.434299
##  [60,] -72.7061 5.425001
##  [61,] -72.7101 5.422701
##  [62,] -72.7107 5.415699
##  [63,] -72.7176 5.404801
##  [64,] -72.7291 5.404801
##  [65,] -72.7418 5.415900
##  [66,] -72.7516 5.426300
##  [67,] -72.7591 5.429201
##  [68,] -72.7695 5.427499
##  [69,] -72.7804 5.413099
##  [70,] -72.7977 5.405701
##  [71,] -72.8098 5.397701
##  [72,] -72.8202 5.384999
##  [73,] -72.8208 5.373501
##  [74,] -72.8196 5.363102
##  [75,] -72.8208 5.358402
##  [76,] -72.8317 5.341201
##  [77,] -72.8507 5.328001
##  [78,] -72.8605 5.321601
##  [79,] -72.8703 5.311299
##  [80,] -72.8824 5.289401
##  [81,] -72.8974 5.279601
##  [82,] -72.9129 5.259501
##  [83,] -72.9320 5.242200
##  [84,] -72.9515 5.226700
##  [85,] -72.9613 5.221000
##  [86,] -72.9717 5.210101
##  [87,] -72.9648 5.205401
##  [88,] -72.9625 5.199601
##  [89,] -72.9590 5.195601
##  [90,] -72.9590 5.188600
##  [91,] -72.9584 5.181101
##  [92,] -72.9671 5.180000
##  [93,] -72.9734 5.179401
##  [94,] -72.9832 5.176000
##  [95,] -72.9890 5.176600
##  [96,] -73.0051 5.176700
##  [97,] -73.0109 5.180201
##  [98,] -73.0270 5.183100
##  [99,] -73.0328 5.184301
## [100,] -73.0317 5.202201
## [101,] -73.0271 5.224099
## [102,] -73.0305 5.235700
## [103,] -73.0438 5.244400
## [104,] -73.0490 5.252500
## [105,] -73.0542 5.265801
## [106,] -73.0553 5.276800
## 
## 
## 
## Slot "plotOrder":
## [1] 1
## 
## Slot "labpt":
## [1] -72.90192   5.40341
## 
## Slot "ID":
## [1] "2"
## 
## Slot "area":
## [1] 0.07373845
## 
## 
## [[3]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] -73.467818   5.723896
## 
## Slot "area":
## [1] 0.01332427
## 
## Slot "hole":
## [1] FALSE
## 
## Slot "ringDir":
## [1] 1
## 
## Slot "coords":
##            [,1]     [,2]
##  [1,] -73.46480 5.675400
##  [2,] -73.47290 5.682901
##  [3,] -73.49020 5.688200
##  [4,] -73.50750 5.691101
##  [5,] -73.51790 5.692300
##  [6,] -73.52420 5.694100
##  [7,] -73.52420 5.697601
##  [8,] -73.52890 5.704500
##  [9,] -73.52310 5.726400
## [10,] -73.53060 5.731100
## [11,] -73.53920 5.732299
## [12,] -73.54160 5.745000
## [13,] -73.54040 5.750801
## [14,] -73.53810 5.759400
## [15,] -73.53120 5.761100
## [16,] -73.52250 5.766301
## [17,] -73.52020 5.775500
## [18,] -73.52200 5.784801
## [19,] -73.52720 5.793500
## [20,] -73.52890 5.802100
## [21,] -73.53060 5.818899
## [22,] -73.52600 5.821800
## [23,] -73.51160 5.814199
## [24,] -73.50350 5.799700
## [25,] -73.49490 5.790401
## [26,] -73.48620 5.789300
## [27,] -73.47640 5.797302
## [28,] -73.47010 5.792101
## [29,] -73.46320 5.790300
## [30,] -73.46150 5.789099
## [31,] -73.45630 5.778200
## [32,] -73.45340 5.770600
## [33,] -73.45220 5.768300
## [34,] -73.44990 5.762000
## [35,] -73.44760 5.755001
## [36,] -73.44700 5.751500
## [37,] -73.43840 5.745201
## [38,] -73.42970 5.740501
## [39,] -73.42110 5.724898
## [40,] -73.41300 5.713300
## [41,] -73.40380 5.704600
## [42,] -73.39630 5.692400
## [43,] -73.39450 5.684900
## [44,] -73.39110 5.678500
## [45,] -73.38820 5.674500
## [46,] -73.38470 5.669301
## [47,] -73.38470 5.660000
## [48,] -73.38990 5.649600
## [49,] -73.39050 5.643900
## [50,] -73.39293 5.630946
## [51,] -73.39560 5.631201
## [52,] -73.40550 5.635301
## [53,] -73.41350 5.646899
## [54,] -73.42390 5.652100
## [55,] -73.43310 5.658500
## [56,] -73.44180 5.662600
## [57,] -73.44470 5.671200
## [58,] -73.45620 5.675300
## [59,] -73.46480 5.675400
## 
## 
## 
## Slot "plotOrder":
## [1] 1
## 
## Slot "labpt":
## [1] -73.467818   5.723896
## 
## Slot "ID":
## [1] "3"
## 
## Slot "area":
## [1] 0.01332427
## 
## 
## [[4]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] -72.924811   5.976306
## 
## Slot "area":
## [1] 0.01263091
## 
## Slot "hole":
## [1] FALSE
## 
## Slot "ringDir":
## [1] 1
## 
## Slot "coords":
##           [,1]     [,2]
##  [1,] -72.8194 5.951100
##  [2,] -72.8205 5.947600
##  [3,] -72.8355 5.947100
##  [4,] -72.8459 5.941899
##  [5,] -72.8609 5.934501
##  [6,] -72.8718 5.930501
##  [7,] -72.8868 5.928300
##  [8,] -72.9030 5.926600
##  [9,] -72.9127 5.924300
## [10,] -72.9202 5.923801
## [11,] -72.9295 5.919799
## [12,] -72.9387 5.920400
## [13,] -72.9427 5.922101
## [14,] -72.9508 5.925100
## [15,] -72.9508 5.932601
## [16,] -72.9554 5.938399
## [17,] -72.9693 5.945899
## [18,] -72.9773 5.946000
## [19,] -72.9796 5.949400
## [20,] -72.9854 5.956399
## [21,] -72.9894 5.961600
## [22,] -72.9917 5.963900
## [23,] -73.0027 5.987701
## [24,] -73.0016 5.992300
## [25,] -73.0004 5.996300
## [26,] -72.9975 6.000899
## [27,] -72.9935 6.010100
## [28,] -72.9877 6.016500
## [29,] -72.9843 6.024001
## [30,] -72.9797 6.032600
## [31,] -72.9751 6.037801
## [32,] -72.9722 6.040101
## [33,] -72.9664 6.045302
## [34,] -72.9601 6.045801
## [35,] -72.9543 6.052700
## [36,] -72.9474 6.058500
## [37,] -72.9365 6.044600
## [38,] -72.9318 6.032399
## [39,] -72.9272 6.024901
## [40,] -72.9186 6.013300
## [41,] -72.9076 6.003400
## [42,] -72.8995 5.994201
## [43,] -72.8920 5.984900
## [44,] -72.8828 5.977901
## [45,] -72.8626 5.977301
## [46,] -72.8534 5.976599
## [47,] -72.8465 5.976599
## [48,] -72.8407 5.976599
## [49,] -72.8332 5.978299
## [50,] -72.8321 5.975400
## [51,] -72.8246 5.971900
## [52,] -72.8165 5.965502
## [53,] -72.8125 5.956800
## [54,] -72.8165 5.953999
## [55,] -72.8194 5.951100
## 
## 
## 
## Slot "plotOrder":
## [1] 1
## 
## Slot "labpt":
## [1] -72.924811   5.976306
## 
## Slot "ID":
## [1] "4"
## 
## Slot "area":
## [1] 0.01263091
## 
## 
## [[5]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] -73.126058   5.185135
## 
## Slot "area":
## [1] 0.0051261
## 
## Slot "hole":
## [1] FALSE
## 
## Slot "ringDir":
## [1] 1
## 
## Slot "coords":
##           [,1]     [,2]
##  [1,] -73.0639 5.140501
##  [2,] -73.0795 5.133700
##  [3,] -73.1221 5.124000
##  [4,] -73.1239 5.130399
##  [5,] -73.1285 5.133900
##  [6,] -73.1406 5.153002
##  [7,] -73.1440 5.157100
##  [8,] -73.1527 5.173300
##  [9,] -73.1596 5.180800
## [10,] -73.1648 5.186001
## [11,] -73.1735 5.197000
## [12,] -73.1740 5.203400
## [13,] -73.1637 5.208000
## [14,] -73.1521 5.216601
## [15,] -73.1441 5.221699
## [16,] -73.1331 5.232101
## [17,] -73.1222 5.242401
## [18,] -73.1026 5.258501
## [19,] -73.1003 5.260800
## [20,] -73.0980 5.256800
## [21,] -73.0980 5.251000
## [22,] -73.0997 5.245801
## [23,] -73.1014 5.236600
## [24,] -73.0985 5.231399
## [25,] -73.0985 5.225000
## [26,] -73.1089 5.219299
## [27,] -73.1187 5.213599
## [28,] -73.1227 5.208398
## [29,] -73.1204 5.200301
## [30,] -73.1164 5.195601
## [31,] -73.1164 5.189301
## [32,] -73.1164 5.177701
## [33,] -73.1095 5.165001
## [34,] -73.1043 5.152801
## [35,] -73.0945 5.147001
## [36,] -73.0858 5.144099
## [37,] -73.0737 5.144099
## [38,] -73.0639 5.140501
## 
## 
## 
## Slot "plotOrder":
## [1] 1
## 
## Slot "labpt":
## [1] -73.126058   5.185135
## 
## Slot "ID":
## [1] "5"
## 
## Slot "area":
## [1] 0.0051261
## 
## 
## [[6]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] -72.870782   5.894272
## 
## Slot "area":
## [1] 0.01047862
## 
## Slot "hole":
## [1] FALSE
## 
## Slot "ringDir":
## [1] 1
## 
## Slot "coords":
##           [,1]     [,2]
##  [1,] -72.8499 5.849000
##  [2,] -72.8585 5.847798
##  [3,] -72.8660 5.846101
##  [4,] -72.8758 5.845000
##  [5,] -72.8793 5.840400
##  [6,] -72.8833 5.833501
##  [7,] -72.8856 5.832401
##  [8,] -72.8902 5.832401
##  [9,] -72.8971 5.832401
## [10,] -72.9023 5.830101
## [11,] -72.9046 5.827200
## [12,] -72.9098 5.825499
## [13,] -72.9133 5.853300
## [14,] -72.9202 5.863700
## [15,] -72.9254 5.874100
## [16,] -72.9335 5.882200
## [17,] -72.9410 5.889801
## [18,] -72.9462 5.894400
## [19,] -72.9554 5.900200
## [20,] -72.9439 5.905399
## [21,] -72.9404 5.908800
## [22,] -72.9364 5.908800
## [23,] -72.9295 5.919799
## [24,] -72.9202 5.923801
## [25,] -72.9127 5.924300
## [26,] -72.9030 5.926600
## [27,] -72.8868 5.928300
## [28,] -72.8718 5.930501
## [29,] -72.8609 5.934501
## [30,] -72.8459 5.941899
## [31,] -72.8355 5.947100
## [32,] -72.8205 5.947600
## [33,] -72.8194 5.951100
## [34,] -72.8125 5.949899
## [35,] -72.8078 5.948101
## [36,] -72.7998 5.947000
## [37,] -72.7969 5.944600
## [38,] -72.7957 5.938900
## [39,] -72.7963 5.934800
## [40,] -72.8009 5.923299
## [41,] -72.8032 5.917501
## [42,] -72.8101 5.903701
## [43,] -72.8130 5.894999
## [44,] -72.8176 5.888100
## [45,] -72.8274 5.874300
## [46,] -72.8384 5.862200
## [47,] -72.8453 5.855301
## [48,] -72.8499 5.849000
## 
## 
## 
## Slot "plotOrder":
## [1] 1
## 
## Slot "labpt":
## [1] -72.870782   5.894272
## 
## Slot "ID":
## [1] "6"
## 
## Slot "area":
## [1] 0.01047862
## 
## 
## 
## Slot "plotOrder":
## [1] 2 3 4 6 5 1
## 
## Slot "bbox":
##          min      max
## x -73.541603 -72.7061
## y   4.871301   6.0585
## 
## Slot "proj4string":
## CRS arguments: +proj=longlat +datum=WGS84 +no_defs

- Ahora, prepare el gráfico:

bins <- c(0, 50, 100, 200, 300, 500, 1000, 2000, Inf)
pal <- colorBin("YlOrRd", domain = ant_mun$km2, bins = bins)


labels <- mun_boyaca$NAME_2

labels
##   [1] "Almeida"               "Aquitania"             "Arcabuco"             
##   [4] "Belén"                 "Berbeo"                "Beteitiva"            
##   [7] "Boavita"               "Boyacá"                "Briceño"              
##  [10] "Buenavista"            "Busbanza"              "Cómbita"              
##  [13] "Caldas"                "Campohermoso"          "Cerinza"              
##  [16] "Chíquiza"              "Chinavita"             "Chiquinquirá"         
##  [19] "Chiscas"               "Chita"                 "Chitaraque"           
##  [22] "Chivatá"               "Chivor"                "Ciénaga"              
##  [25] "Coper"                 "Corrales"              "Covarachía"           
##  [28] "Cuítiva"               "Cubará"                "Cucaita"              
##  [31] "Duitama"               "El Cocuy"              "El Espino"            
##  [34] "Firavitoba"            "Floresta"              "Gámeza"               
##  [37] "Gachantivá"            "Garagoa"               "Guacamayas"           
##  [40] "Guateque"              "Guayatá"               "Guicán"               
##  [43] "Izá"                   "Jenesano"              "Jericó"               
##  [46] "La Capilla"            "La Uvita"              "La Victoria"          
##  [49] "Labranzagrande"        "Macanal"               "Maripí"               
##  [52] "Miraflores"            "Mongua"                "Monguí"               
##  [55] "Moniquirá"             "Motavita"              "Muzo"                 
##  [58] "Nobsa"                 "Nuevo Colón"           "Oicatá"               
##  [61] "Otanche"               "Páez"                  "Pachavita"            
##  [64] "Paipa"                 "Pajarito"              "Panqueba"             
##  [67] "Pauna"                 "Paya"                  "Paz de Río"           
##  [70] "Pesca"                 "Pisba"                 "Puerto Boyacá"        
##  [73] "Quípama"               "Ráquira"               "Ramiriquí"            
##  [76] "Rondón"                "Sáchica"               "Saboyá"               
##  [79] "Samacá"                "San Eduardo"           "San José de Pare"     
##  [82] "San Luis de Gaceno"    "San Mateo"             "San Miguel de Sema"   
##  [85] "San Pablo de Borbur"   "Santa María"           "Santa Rosa de Viterbo"
##  [88] "Santa Sofía"           "Santana"               "Sativanorte"          
##  [91] "Sativasur"             "Siachoque"             "Soatá"                
##  [94] "Socha"                 "Socotá"                "Sogamoso"             
##  [97] "Somondoco"             "Soracá"                "Sora"                 
## [100] "Sotaquirá"             "Susacón"               "Sutamarchán"          
## [103] "Sutatenza"             "Tasco"                 "Tenza"                
## [106] "Tibaná"                "Tibasosa"              "Tinjacá"              
## [109] "Tipacoque"             "Toca"                  "Toguí"                
## [112] "Topagá"                "Tota"                  "Tunja"                
## [115] "Tunungua"              "Turmequé"              "Tuta"                 
## [118] "Tutazá"                "Umbita"                "Ventaquemada"         
## [121] "Villa de Leyva"        "Viracachá"             "Zetaquirá"

- Luego, cree el gráfico:

m <- leaflet(ant_mun) %>%
  setView(-73.5, 5.9, 8)  %>% 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 = "bottomleft")

- ver el gráfico

m

Otra forma de trazar. Puede ser más simple:

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)

Puede probar diferentes proveedores para mejorar su mapa. ¡Aproveche la función de completar pestañas para seleccionar el mapa base preferido con solo desplazarse por la lista de 110 proveedores!

Descomente el siguiente fragmento, complete el código y cree una visualización hermosa para la capital de su departamento. En caso de cualquier problema, use la ayuda de R.

#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.362975, lat = 5.526751, zoom = 14.5) %>%
addMarkers(lng = -73.362975, lat = 5.526751, popup = "Tunja")