Lo primero que utilizaremos es abrir un R Markdown e instalar el siguiente paquete de herramientas(libreria):
#install.packages(c("tidyverse", "sf"))
Luego necesitaremos las siguientes dos librerias:
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.5.3
## -- 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
## Warning: package 'ggplot2' was built under R version 3.5.3
## Warning: package 'tibble' was built under R version 3.5.3
## Warning: package 'tidyr' was built under R version 3.5.3
## Warning: package 'readr' was built under R version 3.5.3
## Warning: package 'purrr' was built under R version 3.5.3
## Warning: package 'dplyr' was built under R version 3.5.3
## Warning: package 'forcats' was built under R version 3.5.3
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
##- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## ggplot2 3.3.0 v purrr 0.3.3
## tibble 2.1.3 v dplyr 0.8.5
## tidyr 1.0.2 v stringr 1.4.0
## 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)
## Warning: package 'sf' was built under R version 3.5.3
## Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
## Linking to GEOS 3.5.1, GDAL 2.2.2, PROJ 4.9.2
Ahora, podemos leer un archivo de formas que representa a los departamentos de Colombia, estos se pueden descargar via internet desde DIVA-GIS, utilizando el siguiente codigo con la ruta donde se encuentra el archivo, para saber que ruta esta utilizando R puede escribir en la consola “getwd()” y allí le aparecera la ruta en la cual R lee los archivos. Esto podra ser util para ubicar los archivos que vaya a utilizar.
deptos <- read_sf("C:/Users/USUARIO/Documents/Geomatica/Colombia/COL_adm1.shp")
Utilizando la siguiente indicación podremos saber que contiene el archivo 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 [°]>
Para empezar podemos ver las funcionalidades de ggplot, empezando por el siguiente código:
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 en “https://epsg.io/”, un ejemplo de lo importante que es el sistema de coordenadas es el siguiente:
# El CRS 3978 es usado en Canada
ggplot() + geom_sf(data = deptos) + coord_sf(crs=st_crs(3978))
ya que el sistema de coordenadas CRS 3978 es usado en Canadá, el mapa de Colombia se observaria de la siguiente manera porque corresponde al UTM 18 N, para cambiar el sistema de coordenadas al que corresponde a la zona del pais utilizaremos el siguiente código (Teniendo en cuenta el sistema que corresponde a la ubicación deseada)
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)
Para filtrar datos podremos utilizar el siguiente código, en este caso tomaremos de filtro el departamento de Tolima en Colombia, para ello filtraremos el NAME_1 que corresponde a los departamentos en la base de datos y entre comillas pondremos “Tolima” que es el departamento de interes.
tolima <- deptos %>% filter(NAME_1 == "Tolima")
Con el siguiente codigo podremos observar el departamento que hemos filtrado anteriormente:
ggplot() + geom_sf(data = tolima)
Repitiendo los pasos anteriores podremos agregarle al departamento los municipios correspondientes con el siguiente código:
munic <- read_sf("C:/Users/USUARIO/Documents/Geomatica/Colombia/COL_adm2.shp")
mun_tolima <- munic %>% filter(NAME_1 == "Tolima")
ggplot() + geom_sf(data = mun_tolima)
Y podremos observar que municipios hacen parte del departamento de Tolima:
mun_tolima
## Simple feature collection with 46 features and 11 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: -76.1007 ymin: 2.9669 xmax: -74.5033 ymax: 5.2915
## epsg (SRID): 4326
## proj4string: +proj=longlat +datum=WGS84 +no_defs
## # A tibble: 46 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~ 29 Tolima 966 Alpuj~ Munic~ Municipa~ <NA>
## 2 53 COL Colom~ 29 Tolima 967 Alvar~ Munic~ Municipa~ <NA>
## 3 53 COL Colom~ 29 Tolima 968 Ambal~ Munic~ Municipa~ <NA>
## 4 53 COL Colom~ 29 Tolima 969 Anzoá~ Munic~ Municipa~ <NA>
## 5 53 COL Colom~ 29 Tolima 970 Armero Munic~ Municipa~ <NA>
## 6 53 COL Colom~ 29 Tolima 971 Ataco Munic~ Municipa~ <NA>
## 7 53 COL Colom~ 29 Tolima 972 Cajam~ Munic~ Municipa~ <NA>
## 8 53 COL Colom~ 29 Tolima 973 Carme~ Munic~ Municipa~ <NA>
## 9 53 COL Colom~ 29 Tolima 974 Casab~ Munic~ Municipa~ <NA>
## 10 53 COL Colom~ 29 Tolima 975 Chapa~ Munic~ Municipa~ <NA>
## # ... with 36 more rows, and 2 more variables: VARNAME_2 <chr>,
## # geometry <MULTIPOLYGON [°]>
tolima_points <- st_centroid(mun_tolima)
## Warning in st_centroid.sf(mun_tolima): 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
tolima_points <- cbind(mun_tolima, st_coordinates(st_centroid(mun_tolima$geometry)))
## Warning in st_centroid.sfc(mun_tolima$geometry): st_centroid does not give
## correct centroids for longitude/latitude data
En este chunck podremos utilizarlo para realizar un mejor plot de Tolima con sus municipios, este se puede cambiar las coordenadas si se desea, el color entre otras caracteristicas.
ggplot(tolima) +
geom_sf() +
geom_sf(data = tolima_points, fill = "antiquewhite") +
geom_text(data = tolima_points, aes(x=X, y=Y,label = ID_2), size = 2) +
coord_sf(xlim = c(-76.5, -74.4), ylim = c(2.8, 5.4), 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(tolima) +
geom_sf(data=tolima_points, aes(x=X, y=Y, fill =
ID_2), color = "black", size = 0.25) +
geom_text(data = tolima_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="Mapa de Tolima")
## Warning: Ignoring unknown aesthetics: x, y
Hay que tener en cuenta que esta visualización no es un mapa real, ya que este deberia estar guardado en PDF o JPG, para ello utilizaremos el siguiente chunck:
Para guardar en PDF (con el nombre que desee entre comillas):
ggsave("Tolima_municipios.pdf")
## Saving 7 x 5 in image
Para guardar en JPG:
ggsave("Mapa_Tolima.png", width = 6, height = 6, dpi = "screen")
Primero necesitamos instalar la libreria de leflet
#install.packages("leaflet")
Luego cargamos la libreria instalada anteriormente
library(leaflet)
## Warning: package 'leaflet' was built under R version 3.5.3
Para usar la biblioteca, necesitamos convertir de características simples a puntos espaciales, para ello realizaremos el siguiente chunk
ant_points <- as(tolima_points , 'Spatial')
Con el siguiente chunck podremos observar que objetos pertenecen a “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 29 Tolima 966 Alpujarra Municipio Municipality
## 2 53 COL Colombia 29 Tolima 967 Alvarado Municipio Municipality
## 3 53 COL Colombia 29 Tolima 968 Ambalema Municipio Municipality
## 4 53 COL Colombia 29 Tolima 969 Anzoátegui Municipio Municipality
## 5 53 COL Colombia 29 Tolima 970 Armero Municipio Municipality
## 6 53 COL Colombia 29 Tolima 971 Ataco Municipio Municipality
## NL_NAME_2 VARNAME_2 X Y
## 1 <NA> <NA> -74.95085 3.379883
## 2 <NA> <NA> -75.01417 4.571738
## 3 <NA> <NA> -74.83492 4.806446
## 4 <NA> <NA> -75.22091 4.623653
## 5 <NA> <NA> -74.88130 4.988726
## 6 <NA> <NA> -75.50915 3.419627
##
## Slot "polygons":
## [[1]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] -74.950852 3.379883
##
## Slot "area":
## [1] 0.04358233
##
## Slot "hole":
## [1] FALSE
##
## Slot "ringDir":
## [1] 1
##
## Slot "coords":
## [,1] [,2]
## [1,] -75.06009 3.413973
## [2,] -75.05570 3.417201
## [3,] -75.04530 3.425200
## [4,] -75.04130 3.434401
## [5,] -75.03500 3.455201
## [6,] -75.03210 3.471401
## [7,] -75.02870 3.485201
## [8,] -75.02520 3.498500
## [9,] -75.01370 3.490901
## [10,] -74.99520 3.482202
## [11,] -74.98370 3.476900
## [12,] -74.97220 3.469400
## [13,] -74.96640 3.469400
## [14,] -74.94910 3.472800
## [15,] -74.92260 3.472600
## [16,] -74.88170 3.463200
## [17,] -74.85280 3.468900
## [18,] -74.84590 3.474101
## [19,] -74.82860 3.482600
## [20,] -74.81770 3.489499
## [21,] -74.81080 3.489499
## [22,] -74.80900 3.493000
## [23,] -74.80730 3.485399
## [24,] -74.80670 3.479701
## [25,] -74.81420 3.474500
## [26,] -74.79860 3.460000
## [27,] -74.79690 3.449601
## [28,] -74.79290 3.440300
## [29,] -74.79000 3.432200
## [30,] -74.79290 3.423001
## [31,] -74.80440 3.410901
## [32,] -74.81130 3.402300
## [33,] -74.82220 3.389600
## [34,] -74.82970 3.381001
## [35,] -74.84120 3.369501
## [36,] -74.85050 3.359700
## [37,] -74.86200 3.351700
## [38,] -74.87010 3.340100
## [39,] -74.87870 3.329201
## [40,] -74.89430 3.318300
## [41,] -74.90350 3.309101
## [42,] -74.91210 3.302799
## [43,] -74.92770 3.291301
## [44,] -74.93860 3.282700
## [45,] -74.94730 3.271701
## [46,] -74.95530 3.266600
## [47,] -74.96170 3.266600
## [48,] -74.96510 3.266600
## [49,] -74.97090 3.266600
## [50,] -74.97950 3.266101
## [51,] -74.98240 3.266700
## [52,] -74.99280 3.266700
## [53,] -75.00780 3.264501
## [54,] -75.02100 3.259901
## [55,] -75.03028 3.259984
## [56,] -75.03550 3.262301
## [57,] -75.03830 3.264601
## [58,] -75.04640 3.268701
## [59,] -75.05510 3.272200
## [60,] -75.06770 3.274000
## [61,] -75.08390 3.274600
## [62,] -75.08850 3.275201
## [63,] -75.09660 3.280999
## [64,] -75.09940 3.282800
## [65,] -75.10180 3.291399
## [66,] -75.10350 3.301300
## [67,] -75.09600 3.308700
## [68,] -75.08910 3.313901
## [69,] -75.07930 3.320201
## [70,] -75.06950 3.327099
## [71,] -75.06200 3.334001
## [72,] -75.05970 3.340900
## [73,] -75.05220 3.346101
## [74,] -75.04470 3.354700
## [75,] -75.04130 3.364500
## [76,] -75.04590 3.377901
## [77,] -75.04940 3.384800
## [78,] -75.05050 3.394001
## [79,] -75.05630 3.407899
## [80,] -75.06009 3.413973
##
##
##
## Slot "plotOrder":
## [1] 1
##
## Slot "labpt":
## [1] -74.950852 3.379883
##
## Slot "ID":
## [1] "1"
##
## Slot "area":
## [1] 0.04358233
##
##
## [[2]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] -75.014173 4.571738
##
## Slot "area":
## [1] 0.02812298
##
## Slot "hole":
## [1] FALSE
##
## Slot "ringDir":
## [1] 1
##
## Slot "coords":
## [,1] [,2]
## [1,] -74.8990 4.618000
## [2,] -74.8973 4.614501
## [3,] -74.9002 4.607001
## [4,] -74.9025 4.597200
## [5,] -74.9117 4.585100
## [6,] -74.9186 4.576501
## [7,] -74.9238 4.567200
## [8,] -74.9393 4.549400
## [9,] -74.9503 4.538501
## [10,] -74.9630 4.528701
## [11,] -74.9762 4.520701
## [12,] -74.9877 4.508601
## [13,] -74.9958 4.493000
## [14,] -75.0073 4.476901
## [15,] -75.0154 4.467100
## [16,] -75.0246 4.459700
## [17,] -75.0373 4.451001
## [18,] -75.0592 4.442499
## [19,] -75.0678 4.437301
## [20,] -75.0655 4.447101
## [21,] -75.0765 4.458699
## [22,] -75.0846 4.466800
## [23,] -75.0857 4.477201
## [24,] -75.0898 4.494600
## [25,] -75.0938 4.508501
## [26,] -75.0973 4.518301
## [27,] -75.1042 4.530501
## [28,] -75.1054 4.542000
## [29,] -75.0961 4.555301
## [30,] -75.0938 4.556399
## [31,] -75.0875 4.562699
## [32,] -75.0846 4.570801
## [33,] -75.0806 4.578299
## [34,] -75.0771 4.583500
## [35,] -75.0662 4.603700
## [36,] -75.0633 4.611201
## [37,] -75.0616 4.622102
## [38,] -75.0616 4.631401
## [39,] -75.0582 4.648100
## [40,] -75.0541 4.660800
## [41,] -75.0495 4.674101
## [42,] -75.0495 4.679801
## [43,] -75.0507 4.684999
## [44,] -75.0478 4.691401
## [45,] -75.0409 4.691898
## [46,] -75.0357 4.692500
## [47,] -75.0236 4.693601
## [48,] -75.0098 4.694100
## [49,] -75.0017 4.694100
## [50,] -74.9919 4.683600
## [51,] -74.9867 4.673800
## [52,] -74.9786 4.665700
## [53,] -74.9734 4.655801
## [54,] -74.9705 4.651799
## [55,] -74.9619 4.644800
## [56,] -74.9538 4.637801
## [57,] -74.9457 4.628600
## [58,] -74.9342 4.625099
## [59,] -74.9186 4.623301
## [60,] -74.8990 4.618000
##
##
##
## Slot "plotOrder":
## [1] 1
##
## Slot "labpt":
## [1] -75.014173 4.571738
##
## Slot "ID":
## [1] "2"
##
## Slot "area":
## [1] 0.02812298
##
##
## [[3]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] -74.834915 4.806446
##
## Slot "area":
## [1] 0.01861079
##
## Slot "hole":
## [1] FALSE
##
## Slot "ringDir":
## [1] 1
##
## Slot "coords":
## [,1] [,2]
## [1,] -74.8501 4.716000
## [2,] -74.8743 4.698200
## [3,] -74.8806 4.701101
## [4,] -74.8876 4.706300
## [5,] -74.8968 4.712101
## [6,] -74.9020 4.718500
## [7,] -74.9043 4.728900
## [8,] -74.9135 4.728900
## [9,] -74.9222 4.730701
## [10,] -74.9233 4.737600
## [11,] -74.9297 4.749200
## [12,] -74.9112 4.749700
## [13,] -74.9014 4.761799
## [14,] -74.8991 4.773899
## [15,] -74.8974 4.788399
## [16,] -74.8963 4.798200
## [17,] -74.8905 4.814302
## [18,] -74.8819 4.823001
## [19,] -74.8680 4.825200
## [20,] -74.8640 4.837900
## [21,] -74.8479 4.846501
## [22,] -74.8381 4.863200
## [23,] -74.8404 4.870099
## [24,] -74.8433 4.877700
## [25,] -74.8433 4.881700
## [26,] -74.8392 4.891500
## [27,] -74.8364 4.898399
## [28,] -74.8300 4.907101
## [29,] -74.8208 4.914000
## [30,] -74.8087 4.916801
## [31,] -74.7920 4.920801
## [32,] -74.7793 4.923600
## [33,] -74.7684 4.927000
## [34,] -74.7626 4.928701
## [35,] -74.7603 4.927000
## [36,] -74.7591 4.924101
## [37,] -74.7603 4.918900
## [38,] -74.7672 4.913200
## [39,] -74.7701 4.902201
## [40,] -74.7655 4.894101
## [41,] -74.7626 4.881399
## [42,] -74.7637 4.872700
## [43,] -74.7770 4.869299
## [44,] -74.7822 4.866400
## [45,] -74.7798 4.852001
## [46,] -74.7816 4.846800
## [47,] -74.7862 4.834098
## [48,] -74.7885 4.824300
## [49,] -74.7815 4.813301
## [50,] -74.7810 4.794200
## [51,] -74.7833 4.768202
## [52,] -74.7890 4.763700
## [53,] -74.7988 4.756200
## [54,] -74.8063 4.751001
## [55,] -74.8092 4.741201
## [56,] -74.8259 4.728602
## [57,] -74.8414 4.720502
## [58,] -74.8501 4.716000
##
##
##
## Slot "plotOrder":
## [1] 1
##
## Slot "labpt":
## [1] -74.834915 4.806446
##
## Slot "ID":
## [1] "3"
##
## Slot "area":
## [1] 0.01861079
##
##
## [[4]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] -75.220907 4.623653
##
## Slot "area":
## [1] 0.04397916
##
## Slot "hole":
## [1] FALSE
##
## Slot "ringDir":
## [1] 1
##
## Slot "coords":
## [,1] [,2]
## [1,] -75.09610 4.555301
## [2,] -75.11230 4.557600
## [3,] -75.12670 4.555401
## [4,] -75.13590 4.553701
## [5,] -75.14460 4.549699
## [6,] -75.15720 4.541701
## [7,] -75.16240 4.534200
## [8,] -75.17110 4.531901
## [9,] -75.17970 4.528999
## [10,] -75.19070 4.525000
## [11,] -75.20450 4.515300
## [12,] -75.22060 4.502101
## [13,] -75.22870 4.499799
## [14,] -75.24020 4.496401
## [15,] -75.25060 4.495300
## [16,] -75.25810 4.502199
## [17,] -75.25640 4.508601
## [18,] -75.25350 4.519001
## [19,] -75.25750 4.533400
## [20,] -75.27250 4.539201
## [21,] -75.28290 4.542200
## [22,] -75.28290 4.548500
## [23,] -75.28700 4.562402
## [24,] -75.29730 4.571100
## [25,] -75.30890 4.575200
## [26,] -75.31520 4.584501
## [27,] -75.32040 4.604701
## [28,] -75.32500 4.622102
## [29,] -75.33600 4.633101
## [30,] -75.35500 4.638899
## [31,] -75.36420 4.646500
## [32,] -75.37350 4.645399
## [33,] -75.37460 4.647701
## [34,] -75.37400 4.652900
## [35,] -75.38040 4.664501
## [36,] -75.38790 4.670901
## [37,] -75.39600 4.671500
## [38,] -75.40400 4.677300
## [39,] -75.40870 4.679001
## [40,] -75.41040 4.686501
## [41,] -75.40810 4.695201
## [42,] -75.41961 4.702724
## [43,] -75.41566 4.703027
## [44,] -75.41210 4.703301
## [45,] -75.40060 4.702699
## [46,] -75.38450 4.704899
## [47,] -75.36200 4.710002
## [48,] -75.33660 4.714501
## [49,] -75.31820 4.716201
## [50,] -75.30260 4.715601
## [51,] -75.28360 4.710799
## [52,] -75.27150 4.714300
## [53,] -75.25820 4.714200
## [54,] -75.24210 4.714102
## [55,] -75.21380 4.699002
## [56,] -75.18560 4.684999
## [57,] -75.17350 4.675701
## [58,] -75.15850 4.668200
## [59,] -75.14000 4.663999
## [60,] -75.12500 4.662801
## [61,] -75.11350 4.664501
## [62,] -75.10660 4.667899
## [63,] -75.09100 4.679400
## [64,] -75.08240 4.683399
## [65,] -75.07140 4.684500
## [66,] -75.06050 4.686800
## [67,] -75.05300 4.687901
## [68,] -75.04780 4.691401
## [69,] -75.05070 4.684999
## [70,] -75.04950 4.679801
## [71,] -75.04950 4.674101
## [72,] -75.05410 4.660800
## [73,] -75.05820 4.648100
## [74,] -75.06160 4.631401
## [75,] -75.06160 4.622102
## [76,] -75.06330 4.611201
## [77,] -75.06620 4.603700
## [78,] -75.07710 4.583500
## [79,] -75.08060 4.578299
## [80,] -75.08460 4.570801
## [81,] -75.08750 4.562699
## [82,] -75.09380 4.556399
## [83,] -75.09610 4.555301
##
##
##
## Slot "plotOrder":
## [1] 1
##
## Slot "labpt":
## [1] -75.220907 4.623653
##
## Slot "ID":
## [1] "4"
##
## Slot "area":
## [1] 0.04397916
##
##
## [[5]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] -74.881298 4.988726
##
## Slot "area":
## [1] 0.03723761
##
## Slot "hole":
## [1] FALSE
##
## Slot "ringDir":
## [1] 1
##
## Slot "coords":
## [,1] [,2]
## [1,] -74.76260 4.928701
## [2,] -74.76840 4.927000
## [3,] -74.77930 4.923600
## [4,] -74.79200 4.920801
## [5,] -74.80870 4.916801
## [6,] -74.82080 4.914000
## [7,] -74.83000 4.907101
## [8,] -74.83640 4.898399
## [9,] -74.83920 4.891500
## [10,] -74.84330 4.881700
## [11,] -74.84330 4.877700
## [12,] -74.85770 4.874300
## [13,] -74.86860 4.872000
## [14,] -74.88360 4.868000
## [15,] -74.88650 4.870300
## [16,] -74.89000 4.873798
## [17,] -74.89170 4.876700
## [18,] -74.89230 4.884200
## [19,] -74.89230 4.892900
## [20,] -74.89460 4.902101
## [21,] -74.90790 4.918399
## [22,] -74.92230 4.930000
## [23,] -74.92340 4.932901
## [24,] -74.93900 4.934702
## [25,] -74.96840 4.939401
## [26,] -74.98800 4.943000
## [27,] -74.99550 4.947601
## [28,] -75.00530 4.951699
## [29,] -75.01910 4.948901
## [30,] -75.03180 4.945999
## [31,] -75.03870 4.944901
## [32,] -75.05080 4.954200
## [33,] -75.05490 4.963501
## [34,] -75.05370 4.974399
## [35,] -75.04510 4.986499
## [36,] -75.04570 4.989401
## [37,] -75.03760 4.991700
## [38,] -75.02840 4.994000
## [39,] -75.02090 4.996300
## [40,] -75.01450 4.998501
## [41,] -75.00190 5.003700
## [42,] -74.99260 5.008299
## [43,] -74.98230 5.014599
## [44,] -74.95920 5.018501
## [45,] -74.94710 5.023100
## [46,] -74.92920 5.030500
## [47,] -74.92920 5.039200
## [48,] -74.93270 5.051902
## [49,] -74.92580 5.060002
## [50,] -74.92700 5.076701
## [51,] -74.92700 5.088299
## [52,] -74.92930 5.102701
## [53,] -74.93160 5.111401
## [54,] -74.93450 5.118901
## [55,] -74.88430 5.109500
## [56,] -74.87510 5.096101
## [57,] -74.85780 5.080500
## [58,] -74.84340 5.067700
## [59,] -74.83010 5.071101
## [60,] -74.80760 5.073300
## [61,] -74.80010 5.064601
## [62,] -74.80590 5.060002
## [63,] -74.80530 5.041600
## [64,] -74.79490 5.042701
## [65,] -74.78280 5.034001
## [66,] -74.77360 5.036201
## [67,] -74.76440 5.037299
## [68,] -74.75980 5.035601
## [69,] -74.75860 5.031501
## [70,] -74.76210 5.014799
## [71,] -74.76380 5.007301
## [72,] -74.76576 4.997966
## [73,] -74.76440 4.994000
## [74,] -74.75280 4.983600
## [75,] -74.74590 4.978299
## [76,] -74.74130 4.977800
## [77,] -74.74010 4.970801
## [78,] -74.74010 4.967300
## [79,] -74.74470 4.965098
## [80,] -74.74880 4.962199
## [81,] -74.75340 4.959900
## [82,] -74.76260 4.957600
## [83,] -74.77130 4.954801
## [84,] -74.77700 4.948401
## [85,] -74.77470 4.943200
## [86,] -74.76890 4.937999
## [87,] -74.76490 4.932201
## [88,] -74.76260 4.928701
##
##
##
## Slot "plotOrder":
## [1] 1
##
## Slot "labpt":
## [1] -74.881298 4.988726
##
## Slot "ID":
## [1] "5"
##
## Slot "area":
## [1] 0.03723761
##
##
## [[6]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] -75.509150 3.419627
##
## Slot "area":
## [1] 0.08672013
##
## Slot "hole":
## [1] FALSE
##
## Slot "ringDir":
## [1] 1
##
## Slot "coords":
## [,1] [,2]
## [1,] -75.59690 3.169901
## [2,] -75.59400 3.165301
## [3,] -75.59170 3.158899
## [4,] -75.59170 3.152000
## [5,] -75.59570 3.145101
## [6,] -75.60490 3.138200
## [7,] -75.61166 3.137485
## [8,] -75.61300 3.146300
## [9,] -75.61420 3.167701
## [10,] -75.61650 3.201200
## [11,] -75.62170 3.235300
## [12,] -75.62570 3.264200
## [13,] -75.62810 3.297099
## [14,] -75.63670 3.328900
## [15,] -75.65630 3.341100
## [16,] -75.66090 3.341201
## [17,] -75.66610 3.329600
## [18,] -75.67360 3.324501
## [19,] -75.68000 3.317500
## [20,] -75.69090 3.315300
## [21,] -75.69610 3.330299
## [22,] -75.69960 3.348200
## [23,] -75.70300 3.352899
## [24,] -75.73360 3.364001
## [25,] -75.75490 3.366401
## [26,] -75.79010 3.371700
## [27,] -75.79640 3.371201
## [28,] -75.79010 3.374000
## [29,] -75.76880 3.400501
## [30,] -75.75780 3.414901
## [31,] -75.75090 3.423600
## [32,] -75.73940 3.436199
## [33,] -75.72960 3.443101
## [34,] -75.71750 3.450601
## [35,] -75.70190 3.460901
## [36,] -75.69040 3.468900
## [37,] -75.67660 3.476401
## [38,] -75.66790 3.477500
## [39,] -75.65120 3.483200
## [40,] -75.63620 3.481399
## [41,] -75.62530 3.477901
## [42,] -75.61550 3.471501
## [43,] -75.61030 3.480700
## [44,] -75.60970 3.486500
## [45,] -75.60050 3.497401
## [46,] -75.59650 3.498000
## [47,] -75.59650 3.495099
## [48,] -75.58900 3.488100
## [49,] -75.58550 3.472000
## [50,] -75.58200 3.462100
## [51,] -75.57920 3.460399
## [52,] -75.57050 3.457500
## [53,] -75.55440 3.459100
## [54,] -75.54340 3.462501
## [55,] -75.52670 3.467699
## [56,] -75.51290 3.470500
## [57,] -75.50310 3.472201
## [58,] -75.48690 3.475601
## [59,] -75.47020 3.489401
## [60,] -75.45640 3.508400
## [61,] -75.44430 3.523399
## [62,] -75.43860 3.544101
## [63,] -75.43340 3.555701
## [64,] -75.42650 3.570101
## [65,] -75.42070 3.582201
## [66,] -75.40980 3.594800
## [67,] -75.39940 3.606300
## [68,] -75.39310 3.619000
## [69,] -75.38380 3.631100
## [70,] -75.37640 3.643801
## [71,] -75.36940 3.649000
## [72,] -75.36480 3.649501
## [73,] -75.36190 3.650700
## [74,] -75.36020 3.644900
## [75,] -75.35730 3.629801
## [76,] -75.35330 3.617101
## [77,] -75.34000 3.615301
## [78,] -75.32730 3.610100
## [79,] -75.31180 3.604201
## [80,] -75.30140 3.593799
## [81,] -75.29970 3.589200
## [82,] -75.29730 3.588601
## [83,] -75.30480 3.580501
## [84,] -75.31640 3.568401
## [85,] -75.31920 3.556900
## [86,] -75.31520 3.540101
## [87,] -75.30880 3.526800
## [88,] -75.29610 3.506000
## [89,] -75.28750 3.483400
## [90,] -75.28400 3.474701
## [91,] -75.28460 3.467800
## [92,] -75.28860 3.458601
## [93,] -75.28750 3.450501
## [94,] -75.29780 3.440701
## [95,] -75.30940 3.433800
## [96,] -75.31860 3.419999
## [97,] -75.32490 3.406201
## [98,] -75.33300 3.395200
## [99,] -75.33530 3.387699
## [100,] -75.33930 3.391800
## [101,] -75.35260 3.392401
## [102,] -75.36120 3.388401
## [103,] -75.36700 3.385600
## [104,] -75.38430 3.389101
## [105,] -75.39580 3.386799
## [106,] -75.40620 3.378799
## [107,] -75.40960 3.363800
## [108,] -75.41080 3.354499
## [109,] -75.42810 3.346000
## [110,] -75.45000 3.335102
## [111,] -75.46210 3.331099
## [112,] -75.46900 3.322999
## [113,] -75.48110 3.300001
## [114,] -75.49260 3.293701
## [115,] -75.51740 3.286301
## [116,] -75.53520 3.277100
## [117,] -75.54680 3.268501
## [118,] -75.55140 3.262700
## [119,] -75.55830 3.254101
## [120,] -75.57210 3.244301
## [121,] -75.59000 3.232800
## [122,] -75.59860 3.224201
## [123,] -75.60550 3.215000
## [124,] -75.60610 3.205799
## [125,] -75.60610 3.200001
## [126,] -75.60610 3.187901
## [127,] -75.59980 3.179701
## [128,] -75.59690 3.169901
##
##
##
## Slot "plotOrder":
## [1] 1
##
## Slot "labpt":
## [1] -75.509150 3.419627
##
## Slot "ID":
## [1] "6"
##
## Slot "area":
## [1] 0.08672013
##
##
##
## Slot "plotOrder":
## [1] 6 4 1 5 2 3
##
## Slot "bbox":
## min max
## x -75.796402 -74.740097
## y 3.137485 5.118901
##
## Slot "proj4string":
## CRS arguments: +proj=longlat +datum=WGS84 +no_defs
Luego obtenemos las areas municipales
#install.packages("lwgeom")
Cargamos la libreria
library(lwgeom)
## Warning: package 'lwgeom' was built under R version 3.5.3
## Linking to liblwgeom 3.0.0beta1 r16016, GEOS 3.6.1, PROJ 4.9.3
Calculamos el area de cada municipio (metros cuadrados)
mun_tolima$area <- st_area(mun_tolima) #tener cuidado con las unidades
Para pasar de metros cuadrados a kilometros cuadrados (más comodo) utilizaremos el siguiente chunk
mun_tolima$km2 <- mun_tolima$area/(1000000)
Verificamos que todo este correcto
mun_tolima$km2
## Units: [m^2]
## [1] 535.5497 345.0955 228.2973 539.6278 456.6703 1065.5931 480.5588
## [8] 174.6815 194.0824 2197.0696 443.2263 609.7756 525.4040 657.5084
## [15] 225.5523 257.1507 91.9227 229.0360 528.5403 308.7631 300.5958
## [22] 1172.5566 229.4786 308.4611 350.1924 166.3298 282.1561 951.8257
## [29] 910.4775 274.3805 1599.4145 348.2134 433.9931 1526.2579 671.3811
## [36] 903.0370 121.3080 404.7788 428.6841 293.4622 276.3641 145.4759
## [43] 190.5804 266.4972 266.2207 492.8366
Ahora, necesitamos una conversión de características simples a polígonos espaciales, para ello usaremos el siguiente chunk:
ant_mun <- as(mun_tolima, 'Spatial')
Con el siguiente chunck podremos observar que objetos pertenecen a “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 29 Tolima 966 Alpujarra Municipio Municipality
## 2 53 COL Colombia 29 Tolima 967 Alvarado Municipio Municipality
## 3 53 COL Colombia 29 Tolima 968 Ambalema Municipio Municipality
## 4 53 COL Colombia 29 Tolima 969 Anzoátegui Municipio Municipality
## 5 53 COL Colombia 29 Tolima 970 Armero Municipio Municipality
## 6 53 COL Colombia 29 Tolima 971 Ataco Municipio Municipality
## NL_NAME_2 VARNAME_2 area km2
## 1 <NA> <NA> 535549662 [m^2] 535.5497 [m^2]
## 2 <NA> <NA> 345095514 [m^2] 345.0955 [m^2]
## 3 <NA> <NA> 228297251 [m^2] 228.2973 [m^2]
## 4 <NA> <NA> 539627840 [m^2] 539.6278 [m^2]
## 5 <NA> <NA> 456670309 [m^2] 456.6703 [m^2]
## 6 <NA> <NA> 1065593116 [m^2] 1065.5931 [m^2]
##
## Slot "polygons":
## [[1]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] -74.950852 3.379883
##
## Slot "area":
## [1] 0.04358233
##
## Slot "hole":
## [1] FALSE
##
## Slot "ringDir":
## [1] 1
##
## Slot "coords":
## [,1] [,2]
## [1,] -75.06009 3.413973
## [2,] -75.05570 3.417201
## [3,] -75.04530 3.425200
## [4,] -75.04130 3.434401
## [5,] -75.03500 3.455201
## [6,] -75.03210 3.471401
## [7,] -75.02870 3.485201
## [8,] -75.02520 3.498500
## [9,] -75.01370 3.490901
## [10,] -74.99520 3.482202
## [11,] -74.98370 3.476900
## [12,] -74.97220 3.469400
## [13,] -74.96640 3.469400
## [14,] -74.94910 3.472800
## [15,] -74.92260 3.472600
## [16,] -74.88170 3.463200
## [17,] -74.85280 3.468900
## [18,] -74.84590 3.474101
## [19,] -74.82860 3.482600
## [20,] -74.81770 3.489499
## [21,] -74.81080 3.489499
## [22,] -74.80900 3.493000
## [23,] -74.80730 3.485399
## [24,] -74.80670 3.479701
## [25,] -74.81420 3.474500
## [26,] -74.79860 3.460000
## [27,] -74.79690 3.449601
## [28,] -74.79290 3.440300
## [29,] -74.79000 3.432200
## [30,] -74.79290 3.423001
## [31,] -74.80440 3.410901
## [32,] -74.81130 3.402300
## [33,] -74.82220 3.389600
## [34,] -74.82970 3.381001
## [35,] -74.84120 3.369501
## [36,] -74.85050 3.359700
## [37,] -74.86200 3.351700
## [38,] -74.87010 3.340100
## [39,] -74.87870 3.329201
## [40,] -74.89430 3.318300
## [41,] -74.90350 3.309101
## [42,] -74.91210 3.302799
## [43,] -74.92770 3.291301
## [44,] -74.93860 3.282700
## [45,] -74.94730 3.271701
## [46,] -74.95530 3.266600
## [47,] -74.96170 3.266600
## [48,] -74.96510 3.266600
## [49,] -74.97090 3.266600
## [50,] -74.97950 3.266101
## [51,] -74.98240 3.266700
## [52,] -74.99280 3.266700
## [53,] -75.00780 3.264501
## [54,] -75.02100 3.259901
## [55,] -75.03028 3.259984
## [56,] -75.03550 3.262301
## [57,] -75.03830 3.264601
## [58,] -75.04640 3.268701
## [59,] -75.05510 3.272200
## [60,] -75.06770 3.274000
## [61,] -75.08390 3.274600
## [62,] -75.08850 3.275201
## [63,] -75.09660 3.280999
## [64,] -75.09940 3.282800
## [65,] -75.10180 3.291399
## [66,] -75.10350 3.301300
## [67,] -75.09600 3.308700
## [68,] -75.08910 3.313901
## [69,] -75.07930 3.320201
## [70,] -75.06950 3.327099
## [71,] -75.06200 3.334001
## [72,] -75.05970 3.340900
## [73,] -75.05220 3.346101
## [74,] -75.04470 3.354700
## [75,] -75.04130 3.364500
## [76,] -75.04590 3.377901
## [77,] -75.04940 3.384800
## [78,] -75.05050 3.394001
## [79,] -75.05630 3.407899
## [80,] -75.06009 3.413973
##
##
##
## Slot "plotOrder":
## [1] 1
##
## Slot "labpt":
## [1] -74.950852 3.379883
##
## Slot "ID":
## [1] "1"
##
## Slot "area":
## [1] 0.04358233
##
##
## [[2]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] -75.014173 4.571738
##
## Slot "area":
## [1] 0.02812298
##
## Slot "hole":
## [1] FALSE
##
## Slot "ringDir":
## [1] 1
##
## Slot "coords":
## [,1] [,2]
## [1,] -74.8990 4.618000
## [2,] -74.8973 4.614501
## [3,] -74.9002 4.607001
## [4,] -74.9025 4.597200
## [5,] -74.9117 4.585100
## [6,] -74.9186 4.576501
## [7,] -74.9238 4.567200
## [8,] -74.9393 4.549400
## [9,] -74.9503 4.538501
## [10,] -74.9630 4.528701
## [11,] -74.9762 4.520701
## [12,] -74.9877 4.508601
## [13,] -74.9958 4.493000
## [14,] -75.0073 4.476901
## [15,] -75.0154 4.467100
## [16,] -75.0246 4.459700
## [17,] -75.0373 4.451001
## [18,] -75.0592 4.442499
## [19,] -75.0678 4.437301
## [20,] -75.0655 4.447101
## [21,] -75.0765 4.458699
## [22,] -75.0846 4.466800
## [23,] -75.0857 4.477201
## [24,] -75.0898 4.494600
## [25,] -75.0938 4.508501
## [26,] -75.0973 4.518301
## [27,] -75.1042 4.530501
## [28,] -75.1054 4.542000
## [29,] -75.0961 4.555301
## [30,] -75.0938 4.556399
## [31,] -75.0875 4.562699
## [32,] -75.0846 4.570801
## [33,] -75.0806 4.578299
## [34,] -75.0771 4.583500
## [35,] -75.0662 4.603700
## [36,] -75.0633 4.611201
## [37,] -75.0616 4.622102
## [38,] -75.0616 4.631401
## [39,] -75.0582 4.648100
## [40,] -75.0541 4.660800
## [41,] -75.0495 4.674101
## [42,] -75.0495 4.679801
## [43,] -75.0507 4.684999
## [44,] -75.0478 4.691401
## [45,] -75.0409 4.691898
## [46,] -75.0357 4.692500
## [47,] -75.0236 4.693601
## [48,] -75.0098 4.694100
## [49,] -75.0017 4.694100
## [50,] -74.9919 4.683600
## [51,] -74.9867 4.673800
## [52,] -74.9786 4.665700
## [53,] -74.9734 4.655801
## [54,] -74.9705 4.651799
## [55,] -74.9619 4.644800
## [56,] -74.9538 4.637801
## [57,] -74.9457 4.628600
## [58,] -74.9342 4.625099
## [59,] -74.9186 4.623301
## [60,] -74.8990 4.618000
##
##
##
## Slot "plotOrder":
## [1] 1
##
## Slot "labpt":
## [1] -75.014173 4.571738
##
## Slot "ID":
## [1] "2"
##
## Slot "area":
## [1] 0.02812298
##
##
## [[3]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] -74.834915 4.806446
##
## Slot "area":
## [1] 0.01861079
##
## Slot "hole":
## [1] FALSE
##
## Slot "ringDir":
## [1] 1
##
## Slot "coords":
## [,1] [,2]
## [1,] -74.8501 4.716000
## [2,] -74.8743 4.698200
## [3,] -74.8806 4.701101
## [4,] -74.8876 4.706300
## [5,] -74.8968 4.712101
## [6,] -74.9020 4.718500
## [7,] -74.9043 4.728900
## [8,] -74.9135 4.728900
## [9,] -74.9222 4.730701
## [10,] -74.9233 4.737600
## [11,] -74.9297 4.749200
## [12,] -74.9112 4.749700
## [13,] -74.9014 4.761799
## [14,] -74.8991 4.773899
## [15,] -74.8974 4.788399
## [16,] -74.8963 4.798200
## [17,] -74.8905 4.814302
## [18,] -74.8819 4.823001
## [19,] -74.8680 4.825200
## [20,] -74.8640 4.837900
## [21,] -74.8479 4.846501
## [22,] -74.8381 4.863200
## [23,] -74.8404 4.870099
## [24,] -74.8433 4.877700
## [25,] -74.8433 4.881700
## [26,] -74.8392 4.891500
## [27,] -74.8364 4.898399
## [28,] -74.8300 4.907101
## [29,] -74.8208 4.914000
## [30,] -74.8087 4.916801
## [31,] -74.7920 4.920801
## [32,] -74.7793 4.923600
## [33,] -74.7684 4.927000
## [34,] -74.7626 4.928701
## [35,] -74.7603 4.927000
## [36,] -74.7591 4.924101
## [37,] -74.7603 4.918900
## [38,] -74.7672 4.913200
## [39,] -74.7701 4.902201
## [40,] -74.7655 4.894101
## [41,] -74.7626 4.881399
## [42,] -74.7637 4.872700
## [43,] -74.7770 4.869299
## [44,] -74.7822 4.866400
## [45,] -74.7798 4.852001
## [46,] -74.7816 4.846800
## [47,] -74.7862 4.834098
## [48,] -74.7885 4.824300
## [49,] -74.7815 4.813301
## [50,] -74.7810 4.794200
## [51,] -74.7833 4.768202
## [52,] -74.7890 4.763700
## [53,] -74.7988 4.756200
## [54,] -74.8063 4.751001
## [55,] -74.8092 4.741201
## [56,] -74.8259 4.728602
## [57,] -74.8414 4.720502
## [58,] -74.8501 4.716000
##
##
##
## Slot "plotOrder":
## [1] 1
##
## Slot "labpt":
## [1] -74.834915 4.806446
##
## Slot "ID":
## [1] "3"
##
## Slot "area":
## [1] 0.01861079
##
##
## [[4]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] -75.220907 4.623653
##
## Slot "area":
## [1] 0.04397916
##
## Slot "hole":
## [1] FALSE
##
## Slot "ringDir":
## [1] 1
##
## Slot "coords":
## [,1] [,2]
## [1,] -75.09610 4.555301
## [2,] -75.11230 4.557600
## [3,] -75.12670 4.555401
## [4,] -75.13590 4.553701
## [5,] -75.14460 4.549699
## [6,] -75.15720 4.541701
## [7,] -75.16240 4.534200
## [8,] -75.17110 4.531901
## [9,] -75.17970 4.528999
## [10,] -75.19070 4.525000
## [11,] -75.20450 4.515300
## [12,] -75.22060 4.502101
## [13,] -75.22870 4.499799
## [14,] -75.24020 4.496401
## [15,] -75.25060 4.495300
## [16,] -75.25810 4.502199
## [17,] -75.25640 4.508601
## [18,] -75.25350 4.519001
## [19,] -75.25750 4.533400
## [20,] -75.27250 4.539201
## [21,] -75.28290 4.542200
## [22,] -75.28290 4.548500
## [23,] -75.28700 4.562402
## [24,] -75.29730 4.571100
## [25,] -75.30890 4.575200
## [26,] -75.31520 4.584501
## [27,] -75.32040 4.604701
## [28,] -75.32500 4.622102
## [29,] -75.33600 4.633101
## [30,] -75.35500 4.638899
## [31,] -75.36420 4.646500
## [32,] -75.37350 4.645399
## [33,] -75.37460 4.647701
## [34,] -75.37400 4.652900
## [35,] -75.38040 4.664501
## [36,] -75.38790 4.670901
## [37,] -75.39600 4.671500
## [38,] -75.40400 4.677300
## [39,] -75.40870 4.679001
## [40,] -75.41040 4.686501
## [41,] -75.40810 4.695201
## [42,] -75.41961 4.702724
## [43,] -75.41566 4.703027
## [44,] -75.41210 4.703301
## [45,] -75.40060 4.702699
## [46,] -75.38450 4.704899
## [47,] -75.36200 4.710002
## [48,] -75.33660 4.714501
## [49,] -75.31820 4.716201
## [50,] -75.30260 4.715601
## [51,] -75.28360 4.710799
## [52,] -75.27150 4.714300
## [53,] -75.25820 4.714200
## [54,] -75.24210 4.714102
## [55,] -75.21380 4.699002
## [56,] -75.18560 4.684999
## [57,] -75.17350 4.675701
## [58,] -75.15850 4.668200
## [59,] -75.14000 4.663999
## [60,] -75.12500 4.662801
## [61,] -75.11350 4.664501
## [62,] -75.10660 4.667899
## [63,] -75.09100 4.679400
## [64,] -75.08240 4.683399
## [65,] -75.07140 4.684500
## [66,] -75.06050 4.686800
## [67,] -75.05300 4.687901
## [68,] -75.04780 4.691401
## [69,] -75.05070 4.684999
## [70,] -75.04950 4.679801
## [71,] -75.04950 4.674101
## [72,] -75.05410 4.660800
## [73,] -75.05820 4.648100
## [74,] -75.06160 4.631401
## [75,] -75.06160 4.622102
## [76,] -75.06330 4.611201
## [77,] -75.06620 4.603700
## [78,] -75.07710 4.583500
## [79,] -75.08060 4.578299
## [80,] -75.08460 4.570801
## [81,] -75.08750 4.562699
## [82,] -75.09380 4.556399
## [83,] -75.09610 4.555301
##
##
##
## Slot "plotOrder":
## [1] 1
##
## Slot "labpt":
## [1] -75.220907 4.623653
##
## Slot "ID":
## [1] "4"
##
## Slot "area":
## [1] 0.04397916
##
##
## [[5]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] -74.881298 4.988726
##
## Slot "area":
## [1] 0.03723761
##
## Slot "hole":
## [1] FALSE
##
## Slot "ringDir":
## [1] 1
##
## Slot "coords":
## [,1] [,2]
## [1,] -74.76260 4.928701
## [2,] -74.76840 4.927000
## [3,] -74.77930 4.923600
## [4,] -74.79200 4.920801
## [5,] -74.80870 4.916801
## [6,] -74.82080 4.914000
## [7,] -74.83000 4.907101
## [8,] -74.83640 4.898399
## [9,] -74.83920 4.891500
## [10,] -74.84330 4.881700
## [11,] -74.84330 4.877700
## [12,] -74.85770 4.874300
## [13,] -74.86860 4.872000
## [14,] -74.88360 4.868000
## [15,] -74.88650 4.870300
## [16,] -74.89000 4.873798
## [17,] -74.89170 4.876700
## [18,] -74.89230 4.884200
## [19,] -74.89230 4.892900
## [20,] -74.89460 4.902101
## [21,] -74.90790 4.918399
## [22,] -74.92230 4.930000
## [23,] -74.92340 4.932901
## [24,] -74.93900 4.934702
## [25,] -74.96840 4.939401
## [26,] -74.98800 4.943000
## [27,] -74.99550 4.947601
## [28,] -75.00530 4.951699
## [29,] -75.01910 4.948901
## [30,] -75.03180 4.945999
## [31,] -75.03870 4.944901
## [32,] -75.05080 4.954200
## [33,] -75.05490 4.963501
## [34,] -75.05370 4.974399
## [35,] -75.04510 4.986499
## [36,] -75.04570 4.989401
## [37,] -75.03760 4.991700
## [38,] -75.02840 4.994000
## [39,] -75.02090 4.996300
## [40,] -75.01450 4.998501
## [41,] -75.00190 5.003700
## [42,] -74.99260 5.008299
## [43,] -74.98230 5.014599
## [44,] -74.95920 5.018501
## [45,] -74.94710 5.023100
## [46,] -74.92920 5.030500
## [47,] -74.92920 5.039200
## [48,] -74.93270 5.051902
## [49,] -74.92580 5.060002
## [50,] -74.92700 5.076701
## [51,] -74.92700 5.088299
## [52,] -74.92930 5.102701
## [53,] -74.93160 5.111401
## [54,] -74.93450 5.118901
## [55,] -74.88430 5.109500
## [56,] -74.87510 5.096101
## [57,] -74.85780 5.080500
## [58,] -74.84340 5.067700
## [59,] -74.83010 5.071101
## [60,] -74.80760 5.073300
## [61,] -74.80010 5.064601
## [62,] -74.80590 5.060002
## [63,] -74.80530 5.041600
## [64,] -74.79490 5.042701
## [65,] -74.78280 5.034001
## [66,] -74.77360 5.036201
## [67,] -74.76440 5.037299
## [68,] -74.75980 5.035601
## [69,] -74.75860 5.031501
## [70,] -74.76210 5.014799
## [71,] -74.76380 5.007301
## [72,] -74.76576 4.997966
## [73,] -74.76440 4.994000
## [74,] -74.75280 4.983600
## [75,] -74.74590 4.978299
## [76,] -74.74130 4.977800
## [77,] -74.74010 4.970801
## [78,] -74.74010 4.967300
## [79,] -74.74470 4.965098
## [80,] -74.74880 4.962199
## [81,] -74.75340 4.959900
## [82,] -74.76260 4.957600
## [83,] -74.77130 4.954801
## [84,] -74.77700 4.948401
## [85,] -74.77470 4.943200
## [86,] -74.76890 4.937999
## [87,] -74.76490 4.932201
## [88,] -74.76260 4.928701
##
##
##
## Slot "plotOrder":
## [1] 1
##
## Slot "labpt":
## [1] -74.881298 4.988726
##
## Slot "ID":
## [1] "5"
##
## Slot "area":
## [1] 0.03723761
##
##
## [[6]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] -75.509150 3.419627
##
## Slot "area":
## [1] 0.08672013
##
## Slot "hole":
## [1] FALSE
##
## Slot "ringDir":
## [1] 1
##
## Slot "coords":
## [,1] [,2]
## [1,] -75.59690 3.169901
## [2,] -75.59400 3.165301
## [3,] -75.59170 3.158899
## [4,] -75.59170 3.152000
## [5,] -75.59570 3.145101
## [6,] -75.60490 3.138200
## [7,] -75.61166 3.137485
## [8,] -75.61300 3.146300
## [9,] -75.61420 3.167701
## [10,] -75.61650 3.201200
## [11,] -75.62170 3.235300
## [12,] -75.62570 3.264200
## [13,] -75.62810 3.297099
## [14,] -75.63670 3.328900
## [15,] -75.65630 3.341100
## [16,] -75.66090 3.341201
## [17,] -75.66610 3.329600
## [18,] -75.67360 3.324501
## [19,] -75.68000 3.317500
## [20,] -75.69090 3.315300
## [21,] -75.69610 3.330299
## [22,] -75.69960 3.348200
## [23,] -75.70300 3.352899
## [24,] -75.73360 3.364001
## [25,] -75.75490 3.366401
## [26,] -75.79010 3.371700
## [27,] -75.79640 3.371201
## [28,] -75.79010 3.374000
## [29,] -75.76880 3.400501
## [30,] -75.75780 3.414901
## [31,] -75.75090 3.423600
## [32,] -75.73940 3.436199
## [33,] -75.72960 3.443101
## [34,] -75.71750 3.450601
## [35,] -75.70190 3.460901
## [36,] -75.69040 3.468900
## [37,] -75.67660 3.476401
## [38,] -75.66790 3.477500
## [39,] -75.65120 3.483200
## [40,] -75.63620 3.481399
## [41,] -75.62530 3.477901
## [42,] -75.61550 3.471501
## [43,] -75.61030 3.480700
## [44,] -75.60970 3.486500
## [45,] -75.60050 3.497401
## [46,] -75.59650 3.498000
## [47,] -75.59650 3.495099
## [48,] -75.58900 3.488100
## [49,] -75.58550 3.472000
## [50,] -75.58200 3.462100
## [51,] -75.57920 3.460399
## [52,] -75.57050 3.457500
## [53,] -75.55440 3.459100
## [54,] -75.54340 3.462501
## [55,] -75.52670 3.467699
## [56,] -75.51290 3.470500
## [57,] -75.50310 3.472201
## [58,] -75.48690 3.475601
## [59,] -75.47020 3.489401
## [60,] -75.45640 3.508400
## [61,] -75.44430 3.523399
## [62,] -75.43860 3.544101
## [63,] -75.43340 3.555701
## [64,] -75.42650 3.570101
## [65,] -75.42070 3.582201
## [66,] -75.40980 3.594800
## [67,] -75.39940 3.606300
## [68,] -75.39310 3.619000
## [69,] -75.38380 3.631100
## [70,] -75.37640 3.643801
## [71,] -75.36940 3.649000
## [72,] -75.36480 3.649501
## [73,] -75.36190 3.650700
## [74,] -75.36020 3.644900
## [75,] -75.35730 3.629801
## [76,] -75.35330 3.617101
## [77,] -75.34000 3.615301
## [78,] -75.32730 3.610100
## [79,] -75.31180 3.604201
## [80,] -75.30140 3.593799
## [81,] -75.29970 3.589200
## [82,] -75.29730 3.588601
## [83,] -75.30480 3.580501
## [84,] -75.31640 3.568401
## [85,] -75.31920 3.556900
## [86,] -75.31520 3.540101
## [87,] -75.30880 3.526800
## [88,] -75.29610 3.506000
## [89,] -75.28750 3.483400
## [90,] -75.28400 3.474701
## [91,] -75.28460 3.467800
## [92,] -75.28860 3.458601
## [93,] -75.28750 3.450501
## [94,] -75.29780 3.440701
## [95,] -75.30940 3.433800
## [96,] -75.31860 3.419999
## [97,] -75.32490 3.406201
## [98,] -75.33300 3.395200
## [99,] -75.33530 3.387699
## [100,] -75.33930 3.391800
## [101,] -75.35260 3.392401
## [102,] -75.36120 3.388401
## [103,] -75.36700 3.385600
## [104,] -75.38430 3.389101
## [105,] -75.39580 3.386799
## [106,] -75.40620 3.378799
## [107,] -75.40960 3.363800
## [108,] -75.41080 3.354499
## [109,] -75.42810 3.346000
## [110,] -75.45000 3.335102
## [111,] -75.46210 3.331099
## [112,] -75.46900 3.322999
## [113,] -75.48110 3.300001
## [114,] -75.49260 3.293701
## [115,] -75.51740 3.286301
## [116,] -75.53520 3.277100
## [117,] -75.54680 3.268501
## [118,] -75.55140 3.262700
## [119,] -75.55830 3.254101
## [120,] -75.57210 3.244301
## [121,] -75.59000 3.232800
## [122,] -75.59860 3.224201
## [123,] -75.60550 3.215000
## [124,] -75.60610 3.205799
## [125,] -75.60610 3.200001
## [126,] -75.60610 3.187901
## [127,] -75.59980 3.179701
## [128,] -75.59690 3.169901
##
##
##
## Slot "plotOrder":
## [1] 1
##
## Slot "labpt":
## [1] -75.509150 3.419627
##
## Slot "ID":
## [1] "6"
##
## Slot "area":
## [1] 0.08672013
##
##
##
## Slot "plotOrder":
## [1] 6 4 1 5 2 3
##
## Slot "bbox":
## min max
## x -75.796402 -74.740097
## y 3.137485 5.118901
##
## Slot "proj4string":
## CRS arguments: +proj=longlat +datum=WGS84 +no_defs
A continuación prepararemos el plot:
bins <- c(0, 50, 100, 200, 300, 500, 1000, 2000, Inf)
pal <- colorBin("YlOrRd", domain = ant_mun$km2, bins = bins)
labels <- mun_tolima$NAME_2
labels
## [1] "Alpujarra" "Alvarado"
## [3] "Ambalema" "Anzoátegui"
## [5] "Armero" "Ataco"
## [7] "Cajamarca" "Carmen de Apicalá"
## [9] "Casabianca" "Chaparral"
## [11] "Coello" "Coyaima"
## [13] "Cunday" "Dolores"
## [15] "Espinal" "Falán"
## [17] "Flandes" "Fresno"
## [19] "Guamo" "Herveo"
## [21] "Honda" "Ibagué"
## [23] "Icononzo" "Lérida"
## [25] "Líbano" "Melgar"
## [27] "Murillo" "Natagaima"
## [29] "Ortega" "Piedras"
## [31] "Planadas" "Prado"
## [33] "Purificación" "Rioblanco"
## [35] "Roncesvalles" "Rovira"
## [37] "Saldaña" "San Antonio"
## [39] "San Luís" "San Sebastian de Mariquita"
## [41] "Santa Isabel" "Suárez"
## [43] "Valle de San Juan" "Venadillo"
## [45] "Villahermosa" "Villarrica"
Despues de prepararlo vamos a crearlo
m <- leaflet(ant_mun) %>%
setView(-75, 4, 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 = "bottomright")
y ejecutamos para observar si esta bien
m
otra forma de plotear algo más sencilla es la siguiente:
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)
Podemos usar otras maneras para representar un mapa, un ejemplo se encuentra en el siguiente chunck el cual esta enfocado en Ibagué, Capital de Tolima.
capital <- leaflet() %>%
addTiles() %>%
addMarkers(lng= -75.2430012, lat= 4.4446595,
popup="Ibagué")
capital
Gracias por la atención prestada.