Ana María Montaño Hernández
23/03/2020
Este cuaderno de R Markdown ilustra las funciones básicas proporcionadaas por las librerías simple features (sf) y tidyverse. Su objetivo es difundir las funcionalidades geoespaciales de R para los estudiantes de Geomática Básica en la Universidad Nacional de Colombia. A continuación, se mencionan los pasos a seguir:
Primero se deben instalar las librerías
#install.packages(c("tidyverse", "sf"))
Luego, se deben cargar ambas librerías
library(tidyverse)
library(sf)
Ahora, podemos leer el shapefile que representa a los departamentos de Colombia, el cual fue descargado previamente de DIVA-GIS
library(sf)
deptos = read_sf("C:/Users/user/Documents/Norte de Santander/COL_adm/QGIS.N.Santander/COL_adm1.shp")
Se puede saber que contiene el objetos 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
Es de utilidad revisar las funciones básicas que proporciona la librería sf¨. Por ejemplo:para saber cuál es el sistema 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"
Una manera adecuada para comprender los datos inicialmente, es trazarlos. Para esto, 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 para otro país o región. Se puede encontrar más información sobre los códigos EPSG en epsg.io.
# El CRS 3978 es usado en Canadá
ggplot()+ geom_sf(data = deptos) + coord_sf(crs = st_crs(3978))
En seguida, se buscan las propiedades del CRS con el código EPSG 32618. Corresponde a UTM 18 N. En caso de que se necesite usar dicho CRS, es necesario convertir el objeto espacial de EPSG 4326 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
ggplot() + geom_sf(data = deptos_utm)
Se pueden filtrar los datos ya que, solo nos interesa un departamento; en este caso, Norte de Santander.
Norte.de.Santander = deptos %>% filter(NAME_1 == "Norte de Santander")
Ahora se traza el nuevo objeto:
ggplot() + geom_sf(data = Norte.de.Santander)
Se pueden repetir los pasos anteriores para cargar los municipios de Colombia, filtrando los que pertenecen al departamento de Norte de Santander.
munic = read_sf("C:/Users/user/Documents/Norte de Santander/COL_adm/QGIS.N.Santander/COL_adm2.shp")
mun_Norte.de.Santander = munic %>% filter(NAME_1 == "Norte de Santander")
ggplot() + geom_sf(data = mun_Norte.de.Santander)
mun_Norte.de.Santander
Simple feature collection with 40 features and 4 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -73.6528 ymin: 6.857401 xmax: -72.03216 ymax: 9.296101
epsg (SRID): 4326
proj4string: +proj=longlat +datum=WGS84 +no_defs
Norte.de.Santander_points = st_centroid(mun_Norte.de.Santander)
## Warning in st_centroid.sf(mun_Norte.de.Santander): 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
Norte.de.Santader_points = cbind(mun_Norte.de.Santander, st_coordinates(st_centroid(mun_Norte.de.Santander$geometry)))
## Warning in st_centroid.sfc(mun_Norte.de.Santander$geometry): st_centroid does not give
## correct centroids for longitude/latitude data
Un mejor gráfico se puede producir utilizando el siguiente chunk:
ggplot(Norte.de.Santander) + geom_sf() + geom_sf(data = Norte.de.Santader_points, fill = "antiquewhite") + geom_text(data = Norte.de.Santader_points, aes(x=X, y=Y, label = ID_2), size = 2) + coord_sf(xlim = c(-74, -72),ylim = c(6.7, 9.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(Norte.de.Santander) + geom_sf(data = Norte.de.Santader_points, aes(x=X, y=Y, fill = ID_2), color = "black", size = 0.25) + geom_text(data = Norte.de.Santader_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 Norte de Santander")
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("Norte.de.Santander_municipios.pdf")
Saving 7 x 7 in image
ggsave("map_Norte.de.Santander.png", width = 6, height = 6, dpi = "screen")
Primero, se debe instalar la librería requerida:
#install.packages("leaflet")
Ahora, se carga la librería:
library(leaflet)
Para usar la librería, se necesita convertir de características simples a puntos espaciales.
ant_points = as(Norte.de.Santader_points, "Spatial")
Luego, se descomenta el siguiente comando para ver qué hay en el objeto
#head(ant_points)
head(ant_points)
An object of class "SpatialPolygonsDataFrame"
Slot "data":
Slot "polygons":
[[1]]
An object of class "Polygons"
Slot "Polygons":
[[1]]
An object of class "Polygon"
Slot "labpt":
[1] -73.18508 8.00005
Slot "area":
[1] 0.1254519
Slot "hole":
[1] FALSE
Slot "ringDir":
[1] 1
Slot "coords":
[,1] [,2]
[1,] -73.02060 8.088599
[2,] -73.01660 8.070102
[3,] -73.02230 8.051600
[4,] -73.02520 8.034301
[5,] -73.04650 8.024501
[6,] -73.06040 8.017700
[7,] -73.06550 8.005000
[8,] -73.08230 8.004501
[9,] -73.09030 8.002801
[10,] -73.09150 7.993001
[11,] -73.09900 7.978601
[12,] -73.10070 7.969899
[13,] -73.09720 7.965300
[14,] -73.09720 7.945602
[15,] -73.09660 7.937501
[16,] -73.08860 7.928301
[17,] -73.07470 7.913798
[18,] -73.07070 7.899900
[19,] -73.06490 7.877900
[20,] -73.06030 7.854201
[21,] -73.05740 7.842099
[22,] -73.05160 7.831599
[23,] -73.05390 7.824101
[24,] -73.06370 7.816101
[25,] -73.06430 7.811499
[26,] -73.07000 7.794700
[27,] -73.07290 7.784401
[28,] -73.08440 7.766500
[29,] -73.09420 7.760201
[30,] -73.10860 7.760301
[31,] -73.12710 7.756301
[32,] -73.13690 7.765001
[33,] -73.14550 7.773700
[34,] -73.16570 7.788799
[35,] -73.16986 7.785178
[36,] -73.17165 7.782192
[37,] -73.18652 7.788141
[38,] -73.20119 7.790937
[39,] -73.21936 7.791635
[40,] -73.22705 7.791635
[41,] -73.24102 7.788840
[42,] -73.24731 7.786046
[43,] -73.25988 7.780458
[44,] -73.26897 7.780458
[45,] -73.27735 7.782552
[46,] -73.28014 7.783252
[47,] -73.28433 7.783949
[48,] -73.29132 7.786046
[49,] -73.29971 7.786046
[50,] -73.31158 7.786046
[51,] -73.32136 7.786046
[52,] -73.32625 7.786046
[53,] -73.33813 7.786046
[54,] -73.34721 7.786046
[55,] -73.35280 7.786743
[56,] -73.35839 7.788840
[57,] -73.36328 7.789540
[58,] -73.37003 7.792068
[59,] -73.36810 7.794301
[60,] -73.37040 7.807000
[61,] -73.37330 7.845700
[62,] -73.37500 7.875199
[63,] -73.36580 7.884899
[64,] -73.34390 7.890599
[65,] -73.32780 7.894000
[66,] -73.31620 7.900901
[67,] -73.30930 7.917101
[68,] -73.30360 7.936702
[69,] -73.30530 7.952300
[70,] -73.30710 7.959201
[71,] -73.31860 7.980602
[72,] -73.32730 7.998601
[73,] -73.35490 8.015400
[74,] -73.37970 8.025401
[75,] -73.40340 8.032400
[76,] -73.41140 8.035299
[77,] -73.41430 8.042301
[78,] -73.41490 8.052101
[79,] -73.41720 8.066501
[80,] -73.41720 8.076900
[81,] -73.41720 8.086201
[82,] -73.41260 8.087300
[83,] -73.40800 8.094801
[84,] -73.40501 8.098183
[85,] -73.40340 8.099999
[86,] -73.38500 8.097000
[87,] -73.37230 8.094101
[88,] -73.36250 8.088800
[89,] -73.33940 8.087601
[90,] -73.32960 8.093301
[91,] -73.30480 8.098402
[92,] -73.29040 8.105301
[93,] -73.28750 8.117401
[94,] -73.28760 8.131301
[95,] -73.28870 8.149700
[96,] -73.29280 8.158999
[97,] -73.29160 8.169401
[98,] -73.27660 8.161800
[99,] -73.25990 8.158901
[100,] -73.24090 8.162801
[101,] -73.20570 8.169000
[102,] -73.18900 8.179901
[103,] -73.17350 8.192601
[104,] -73.13140 8.207401
[105,] -73.12390 8.200999
[106,] -73.11060 8.191700
[107,] -73.09220 8.203201
[108,] -73.08990 8.214200
[109,] -73.08530 8.224602
[110,] -73.08360 8.237800
[111,] -73.08300 8.249401
[112,] -73.09050 8.263302
[113,] -73.07200 8.262101
[114,] -73.05940 8.259101
[115,] -73.02590 8.275700
[116,] -73.02190 8.278002
[117,] -73.01440 8.279700
[118,] -73.01550 8.268202
[119,] -73.01500 8.253700
[120,] -73.01030 8.240401
[121,] -73.00170 8.229400
[122,] -72.98840 8.209699
[123,] -72.98260 8.192899
[124,] -72.97980 8.182502
[125,] -72.97280 8.165201
[126,] -72.97630 8.158801
[127,] -72.99360 8.148499
[128,] -73.00110 8.134102
[129,] -73.00740 8.119101
[130,] -73.02580 8.114599
[131,] -73.02470 8.108202
[132,] -73.02350 8.093800
[133,] -73.02060 8.088599
Slot "plotOrder":
[1] 1
Slot "labpt":
[1] -73.18508 8.00005
Slot "ID":
[1] "1"
Slot "area":
[1] 0.1254519
[[2]]
An object of class "Polygons"
Slot "Polygons":
[[1]]
An object of class "Polygon"
Slot "labpt":
[1] -72.896733 7.590502
Slot "area":
[1] 0.04137994
Slot "hole":
[1] FALSE
Slot "ringDir":
[1] 1
Slot "coords":
[,1] [,2]
[1,] -72.95510 7.467899
[2,] -72.97130 7.477800
[3,] -72.98570 7.485900
[4,] -72.99550 7.497501
[5,] -73.00130 7.511401
[6,] -73.00300 7.518899
[7,] -73.01050 7.528800
[8,] -73.02150 7.545000
[9,] -73.02380 7.552498
[10,] -73.02490 7.561800
[11,] -73.02780 7.574499
[12,] -73.02840 7.583700
[13,] -73.02900 7.593600
[14,] -73.03079 7.603398
[15,] -73.00130 7.613700
[16,] -73.00020 7.628699
[17,] -73.00020 7.643700
[18,] -72.99270 7.659301
[19,] -72.96740 7.656299
[20,] -72.95180 7.662601
[21,] -72.93620 7.672901
[22,] -72.91720 7.666501
[23,] -72.89820 7.668700
[24,] -72.86880 7.680100
[25,] -72.85150 7.684700
[26,] -72.83710 7.684000
[27,] -72.82100 7.700100
[28,] -72.81350 7.705900
[29,] -72.80430 7.716202
[30,] -72.79500 7.702901
[31,] -72.78120 7.703400
[32,] -72.77250 7.706299
[33,] -72.76040 7.711400
[34,] -72.76160 7.694698
[35,] -72.76100 7.680800
[36,] -72.75460 7.668700
[37,] -72.73620 7.666900
[38,] -72.73390 7.641401
[39,] -72.73680 7.633900
[40,] -72.73620 7.628699
[41,] -72.73330 7.619501
[42,] -72.73790 7.609700
[43,] -72.75690 7.607401
[44,] -72.76100 7.605101
[45,] -72.76517 7.604082
[46,] -72.77940 7.600600
[47,] -72.79500 7.605899
[48,] -72.80360 7.607601
[49,] -72.82440 7.600800
[50,] -72.83590 7.587000
[51,] -72.83990 7.580600
[52,] -72.84860 7.568500
[53,] -72.85550 7.561100
[54,] -72.85830 7.551799
[55,] -72.85490 7.533301
[56,] -72.86640 7.515501
[57,] -72.87560 7.494700
[58,] -72.89060 7.471101
[59,] -72.90210 7.458400
[60,] -72.91650 7.444600
[61,] -72.94070 7.445901
[62,] -72.94990 7.454001
[63,] -72.95510 7.467899
Slot "plotOrder":
[1] 1
Slot "labpt":
[1] -72.896733 7.590502
Slot "ID":
[1] "2"
Slot "area":
[1] 0.04137994
[[3]]
An object of class "Polygons"
Slot "Polygons":
[[1]]
An object of class "Polygon"
Slot "labpt":
[1] -72.688396 7.602174
Slot "area":
[1] 0.0150288
Slot "hole":
[1] FALSE
Slot "ringDir":
[1] 1
Slot "coords":
[,1] [,2]
[1,] -72.61860 7.663499
[2,] -72.62320 7.662300
[3,] -72.62550 7.657099
[4,] -72.63190 7.653701
[5,] -72.63470 7.649100
[6,] -72.63420 7.639901
[7,] -72.63700 7.631199
[8,] -72.64510 7.620800
[9,] -72.64970 7.608700
[10,] -72.65260 7.599501
[11,] -72.65490 7.594300
[12,] -72.65890 7.579301
[13,] -72.66290 7.566600
[14,] -72.66410 7.558000
[15,] -72.66350 7.549900
[16,] -72.65370 7.548701
[17,] -72.65430 7.544601
[18,] -72.65720 7.541800
[19,] -72.66000 7.538901
[20,] -72.66230 7.537201
[21,] -72.66640 7.532000
[22,] -72.67330 7.529700
[23,] -72.68370 7.529700
[24,] -72.69060 7.528002
[25,] -72.69870 7.525801
[26,] -72.70100 7.518300
[27,] -72.70330 7.512500
[28,] -72.71360 7.506200
[29,] -72.72280 7.503900
[30,] -72.72800 7.506200
[31,] -72.73900 7.507401
[32,] -72.74480 7.512101
[33,] -72.75110 7.518501
[34,] -72.75570 7.526001
[35,] -72.75800 7.531200
[36,] -72.75860 7.540401
[37,] -72.75570 7.547400
[38,] -72.75230 7.553700
[39,] -72.74650 7.562900
[40,] -72.75170 7.572801
[41,] -72.75860 7.580299
[42,] -72.76040 7.592402
[43,] -72.76517 7.604082
[44,] -72.76100 7.605101
[45,] -72.75690 7.607401
[46,] -72.73790 7.609700
[47,] -72.73330 7.619501
[48,] -72.72120 7.623500
[49,] -72.70910 7.634399
[50,] -72.68660 7.647001
[51,] -72.67280 7.653900
[52,] -72.66470 7.666601
[53,] -72.65610 7.697101
[54,] -72.64690 7.707500
[55,] -72.63770 7.725401
[56,] -72.63820 7.732300
[57,] -72.61520 7.721200
[58,] -72.61290 7.720601
[59,] -72.61170 7.712501
[60,] -72.61290 7.704501
[61,] -72.61060 7.700401
[62,] -72.60624 7.701616
[63,] -72.61110 7.695200
[64,] -72.61230 7.690600
[65,] -72.61340 7.679599
[66,] -72.61860 7.663499
Slot "plotOrder":
[1] 1
Slot "labpt":
[1] -72.688396 7.602174
Slot "ID":
[1] "3"
Slot "area":
[1] 0.0150288
[[4]]
An object of class "Polygons"
Slot "Polygons":
[[1]]
An object of class "Polygon"
Slot "labpt":
[1] -72.94723 8.06001
Slot "area":
[1] 0.01922532
Slot "hole":
[1] FALSE
Slot "ringDir":
[1] 1
Slot "coords":
[,1] [,2]
[1,] -72.8765 8.005901
[2,] -72.8909 7.982900
[3,] -72.8949 7.989801
[4,] -72.9093 7.985799
[5,] -72.9278 7.977801
[6,] -72.9451 7.974401
[7,] -72.9681 7.981500
[8,] -72.9820 7.982701
[9,] -72.9975 7.995501
[10,] -72.9935 8.008699
[11,] -72.9947 8.026101
[12,] -73.0097 8.043502
[13,] -73.0125 8.061400
[14,] -73.0074 8.080401
[15,] -73.0206 8.088599
[16,] -73.0235 8.093800
[17,] -73.0247 8.108202
[18,] -73.0258 8.114599
[19,] -73.0074 8.119101
[20,] -73.0011 8.134102
[21,] -72.9936 8.148499
[22,] -72.9763 8.158801
[23,] -72.9728 8.165201
[24,] -72.9723 8.159400
[25,] -72.9509 8.143100
[26,] -72.9353 8.132101
[27,] -72.9140 8.111201
[28,] -72.8973 8.100200
[29,] -72.8696 8.064201
[30,] -72.8696 8.045799
[31,] -72.8701 8.033599
[32,] -72.8747 8.014001
[33,] -72.8765 8.005901
Slot "plotOrder":
[1] 1
Slot "labpt":
[1] -72.94723 8.06001
Slot "ID":
[1] "4"
Slot "area":
[1] 0.01922532
[[5]]
An object of class "Polygons"
Slot "Polygons":
[[1]]
An object of class "Polygon"
Slot "labpt":
[1] -73.196477 7.657177
Slot "area":
[1] 0.05866379
Slot "hole":
[1] FALSE
Slot "ringDir":
[1] 1
Slot "coords":
[,1] [,2]
[1,] -73.42336 7.562488
[2,] -73.42336 7.566679
[3,] -73.42056 7.582747
[4,] -73.41987 7.589035
[5,] -73.41847 7.605801
[6,] -73.41707 7.611389
[7,] -73.41358 7.615583
[8,] -73.41078 7.622568
[9,] -73.40240 7.631651
[10,] -73.40100 7.633745
[11,] -73.40031 7.636542
[12,] -73.39681 7.642130
[13,] -73.38913 7.642827
[14,] -73.38633 7.645624
[15,] -73.38214 7.651910
[16,] -73.38004 7.656101
[17,] -73.37585 7.660992
[18,] -73.37376 7.660992
[19,] -73.36747 7.660992
[20,] -73.36468 7.660992
[21,] -73.36258 7.660295
[22,] -73.35140 7.662389
[23,] -73.34721 7.662389
[24,] -73.34023 7.663789
[25,] -73.33463 7.666581
[26,] -73.32206 7.674266
[27,] -73.31367 7.678457
[28,] -73.29482 7.680554
[29,] -73.27106 7.695225
[30,] -73.25010 7.705005
[31,] -73.24451 7.707801
[32,] -73.22984 7.718281
[33,] -73.22565 7.721772
[34,] -73.21796 7.726663
[35,] -73.21028 7.733649
[36,] -73.20679 7.739237
[37,] -73.20190 7.747622
[38,] -73.19771 7.751813
[39,] -73.19072 7.758102
[40,] -73.18304 7.765087
[41,] -73.17815 7.771375
[42,] -73.17165 7.782192
[43,] -73.16986 7.785178
[44,] -73.16570 7.788799
[45,] -73.14550 7.773700
[46,] -73.13690 7.765001
[47,] -73.12710 7.756301
[48,] -73.10860 7.760301
[49,] -73.09420 7.760201
[50,] -73.08440 7.766500
[51,] -73.07290 7.784401
[52,] -73.07000 7.794700
[53,] -73.06430 7.811499
[54,] -73.06370 7.816101
[55,] -73.05390 7.824101
[56,] -73.05160 7.831599
[57,] -73.04530 7.817200
[58,] -73.04120 7.801601
[59,] -73.03950 7.790000
[60,] -73.03310 7.773800
[61,] -73.02270 7.754701
[62,] -73.02270 7.746601
[63,] -73.02220 7.729902
[64,] -73.02620 7.705599
[65,] -73.02100 7.680800
[66,] -73.01000 7.678400
[67,] -72.99910 7.669701
[68,] -72.99270 7.659301
[69,] -73.00020 7.643700
[70,] -73.00020 7.628699
[71,] -73.00130 7.613700
[72,] -73.03079 7.603398
[73,] -73.04690 7.612700
[74,] -73.04920 7.614999
[75,] -73.06360 7.615100
[76,] -73.07110 7.612800
[77,] -73.08030 7.602400
[78,] -73.08720 7.600201
[79,] -73.08950 7.598400
[80,] -73.09410 7.598498
[81,] -73.10450 7.597901
[82,] -73.12180 7.602601
[83,] -73.13160 7.603800
[84,] -73.15060 7.604499
[85,] -73.15580 7.603300
[86,] -73.17310 7.596499
[87,] -73.18000 7.597099
[88,] -73.19330 7.604100
[89,] -73.19560 7.607002
[90,] -73.20600 7.607002
[91,] -73.22440 7.605899
[92,] -73.24060 7.604800
[93,] -73.26300 7.599699
[94,] -73.28090 7.581899
[95,] -73.28490 7.561100
[96,] -73.28490 7.549000
[97,] -73.29410 7.539799
[98,] -73.31490 7.517400
[99,] -73.33560 7.518601
[100,] -73.35580 7.527900
[101,] -73.37200 7.535501
[102,] -73.38580 7.538500
[103,] -73.40370 7.547200
[104,] -73.41580 7.554201
[105,] -73.42336 7.562488
Slot "plotOrder":
[1] 1
Slot "labpt":
[1] -73.196477 7.657177
Slot "ID":
[1] "5"
Slot "area":
[1] 0.05866379
[[6]]
An object of class "Polygons"
Slot "Polygons":
[[1]]
An object of class "Polygon"
Slot "labpt":
[1] -72.67831 7.25046
Slot "area":
[1] 0.01084088
Slot "hole":
[1] FALSE
Slot "ringDir":
[1] 1
Slot "coords":
[,1] [,2]
[1,] -72.7302 7.226701
[2,] -72.7308 7.233600
[3,] -72.7417 7.233700
[4,] -72.7527 7.238901
[5,] -72.7567 7.245301
[6,] -72.7567 7.251600
[7,] -72.7544 7.256801
[8,] -72.7417 7.271800
[9,] -72.7429 7.285101
[10,] -72.7429 7.293201
[11,] -72.7222 7.299400
[12,] -72.7152 7.292502
[13,] -72.7031 7.292401
[14,] -72.6997 7.295300
[15,] -72.6945 7.298199
[16,] -72.6847 7.297001
[17,] -72.6743 7.296301
[18,] -72.6639 7.296301
[19,] -72.6484 7.297399
[20,] -72.6391 7.294999
[21,] -72.6363 7.290400
[22,] -72.6363 7.277701
[23,] -72.6293 7.270699
[24,] -72.6161 7.269498
[25,] -72.6126 7.262602
[26,] -72.6097 7.249301
[27,] -72.6080 7.242301
[28,] -72.5988 7.231899
[29,] -72.6166 7.223300
[30,] -72.6316 7.211800
[31,] -72.6529 7.199201
[32,] -72.6668 7.190000
[33,] -72.6829 7.187201
[34,] -72.6898 7.195301
[35,] -72.6927 7.208000
[36,] -72.6950 7.217899
[37,] -72.7014 7.229399
[38,] -72.7083 7.228301
[39,] -72.7244 7.226701
[40,] -72.7302 7.226701
Slot "plotOrder":
[1] 1
Slot "labpt":
[1] -72.67831 7.25046
Slot "ID":
[1] "6"
Slot "area":
[1] 0.01084088
Slot "plotOrder":
[1] 1 5 2 4 3 6
Slot "bbox":
min max
x -73.423363 -72.5988
y 7.187201 8.2797
Slot "proj4string":
CRS arguments:
+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
Después, se instala la librería lwgeom para obtener el área de los municipios:
#install.packages("lwgeom")
Cargar la librería
library(lwgeom)
## Linking to liblwgeom 3.0.0beta1 r16016, GEOS 3.6.1, PROJ 4.9.3
En seguida, se va a calcular el área de cada municipio (en metros cuadrados):
mun_Norte.de.Santander$area = st_area(mun_Norte.de.Santander) #Tener cuidado con las unidades
Ahora, se crea un nuevo campo para almacenar el área en kilómetros cuadrados:
mun_Norte.de.Santander$km2 = mun_Norte.de.Santander$area/ (1000000)
Verificar la salida
mun_Norte.de.Santander$km2
Units: [m^2]
[1] 1529.56138 505.00325 183.40764 234.37008 715.82753 132.40233
[7] 181.44132 1152.25765 998.06953 392.94656 182.75656 1838.12016
[13] 638.79501 507.15371 163.12553 356.89687 153.46219 534.58078
[19] 266.56178 249.51168 75.71771 106.44038 141.16264 381.48340
[25] 284.98224 170.41673 161.41362 101.84699 472.28135 441.52525
[31] 153.91790 1182.10259 150.94312 413.04877 1341.46524 871.15429
[37] 3162.85634 1682.44050 380.99595 213.21860
Nuevamente, se necesita una conversión de características simples a polígonos espaciales:
ant_mun = as(mun_Norte.de.Santander, "Spatial")
Luego, se descomenta el siguiente código para ver que hay dentro del objeto espacial:
#head(ant_mun)
head(ant_mun)
An object of class "SpatialPolygonsDataFrame"
Slot "data":
Slot "polygons":
[[1]]
An object of class "Polygons"
Slot "Polygons":
[[1]]
An object of class "Polygon"
Slot "labpt":
[1] -73.18508 8.00005
Slot "area":
[1] 0.1254519
Slot "hole":
[1] FALSE
Slot "ringDir":
[1] 1
Slot "coords":
[,1] [,2]
[1,] -73.02060 8.088599
[2,] -73.01660 8.070102
[3,] -73.02230 8.051600
[4,] -73.02520 8.034301
[5,] -73.04650 8.024501
[6,] -73.06040 8.017700
[7,] -73.06550 8.005000
[8,] -73.08230 8.004501
[9,] -73.09030 8.002801
[10,] -73.09150 7.993001
[11,] -73.09900 7.978601
[12,] -73.10070 7.969899
[13,] -73.09720 7.965300
[14,] -73.09720 7.945602
[15,] -73.09660 7.937501
[16,] -73.08860 7.928301
[17,] -73.07470 7.913798
[18,] -73.07070 7.899900
[19,] -73.06490 7.877900
[20,] -73.06030 7.854201
[21,] -73.05740 7.842099
[22,] -73.05160 7.831599
[23,] -73.05390 7.824101
[24,] -73.06370 7.816101
[25,] -73.06430 7.811499
[26,] -73.07000 7.794700
[27,] -73.07290 7.784401
[28,] -73.08440 7.766500
[29,] -73.09420 7.760201
[30,] -73.10860 7.760301
[31,] -73.12710 7.756301
[32,] -73.13690 7.765001
[33,] -73.14550 7.773700
[34,] -73.16570 7.788799
[35,] -73.16986 7.785178
[36,] -73.17165 7.782192
[37,] -73.18652 7.788141
[38,] -73.20119 7.790937
[39,] -73.21936 7.791635
[40,] -73.22705 7.791635
[41,] -73.24102 7.788840
[42,] -73.24731 7.786046
[43,] -73.25988 7.780458
[44,] -73.26897 7.780458
[45,] -73.27735 7.782552
[46,] -73.28014 7.783252
[47,] -73.28433 7.783949
[48,] -73.29132 7.786046
[49,] -73.29971 7.786046
[50,] -73.31158 7.786046
[51,] -73.32136 7.786046
[52,] -73.32625 7.786046
[53,] -73.33813 7.786046
[54,] -73.34721 7.786046
[55,] -73.35280 7.786743
[56,] -73.35839 7.788840
[57,] -73.36328 7.789540
[58,] -73.37003 7.792068
[59,] -73.36810 7.794301
[60,] -73.37040 7.807000
[61,] -73.37330 7.845700
[62,] -73.37500 7.875199
[63,] -73.36580 7.884899
[64,] -73.34390 7.890599
[65,] -73.32780 7.894000
[66,] -73.31620 7.900901
[67,] -73.30930 7.917101
[68,] -73.30360 7.936702
[69,] -73.30530 7.952300
[70,] -73.30710 7.959201
[71,] -73.31860 7.980602
[72,] -73.32730 7.998601
[73,] -73.35490 8.015400
[74,] -73.37970 8.025401
[75,] -73.40340 8.032400
[76,] -73.41140 8.035299
[77,] -73.41430 8.042301
[78,] -73.41490 8.052101
[79,] -73.41720 8.066501
[80,] -73.41720 8.076900
[81,] -73.41720 8.086201
[82,] -73.41260 8.087300
[83,] -73.40800 8.094801
[84,] -73.40501 8.098183
[85,] -73.40340 8.099999
[86,] -73.38500 8.097000
[87,] -73.37230 8.094101
[88,] -73.36250 8.088800
[89,] -73.33940 8.087601
[90,] -73.32960 8.093301
[91,] -73.30480 8.098402
[92,] -73.29040 8.105301
[93,] -73.28750 8.117401
[94,] -73.28760 8.131301
[95,] -73.28870 8.149700
[96,] -73.29280 8.158999
[97,] -73.29160 8.169401
[98,] -73.27660 8.161800
[99,] -73.25990 8.158901
[100,] -73.24090 8.162801
[101,] -73.20570 8.169000
[102,] -73.18900 8.179901
[103,] -73.17350 8.192601
[104,] -73.13140 8.207401
[105,] -73.12390 8.200999
[106,] -73.11060 8.191700
[107,] -73.09220 8.203201
[108,] -73.08990 8.214200
[109,] -73.08530 8.224602
[110,] -73.08360 8.237800
[111,] -73.08300 8.249401
[112,] -73.09050 8.263302
[113,] -73.07200 8.262101
[114,] -73.05940 8.259101
[115,] -73.02590 8.275700
[116,] -73.02190 8.278002
[117,] -73.01440 8.279700
[118,] -73.01550 8.268202
[119,] -73.01500 8.253700
[120,] -73.01030 8.240401
[121,] -73.00170 8.229400
[122,] -72.98840 8.209699
[123,] -72.98260 8.192899
[124,] -72.97980 8.182502
[125,] -72.97280 8.165201
[126,] -72.97630 8.158801
[127,] -72.99360 8.148499
[128,] -73.00110 8.134102
[129,] -73.00740 8.119101
[130,] -73.02580 8.114599
[131,] -73.02470 8.108202
[132,] -73.02350 8.093800
[133,] -73.02060 8.088599
Slot "plotOrder":
[1] 1
Slot "labpt":
[1] -73.18508 8.00005
Slot "ID":
[1] "1"
Slot "area":
[1] 0.1254519
[[2]]
An object of class "Polygons"
Slot "Polygons":
[[1]]
An object of class "Polygon"
Slot "labpt":
[1] -72.896733 7.590502
Slot "area":
[1] 0.04137994
Slot "hole":
[1] FALSE
Slot "ringDir":
[1] 1
Slot "coords":
[,1] [,2]
[1,] -72.95510 7.467899
[2,] -72.97130 7.477800
[3,] -72.98570 7.485900
[4,] -72.99550 7.497501
[5,] -73.00130 7.511401
[6,] -73.00300 7.518899
[7,] -73.01050 7.528800
[8,] -73.02150 7.545000
[9,] -73.02380 7.552498
[10,] -73.02490 7.561800
[11,] -73.02780 7.574499
[12,] -73.02840 7.583700
[13,] -73.02900 7.593600
[14,] -73.03079 7.603398
[15,] -73.00130 7.613700
[16,] -73.00020 7.628699
[17,] -73.00020 7.643700
[18,] -72.99270 7.659301
[19,] -72.96740 7.656299
[20,] -72.95180 7.662601
[21,] -72.93620 7.672901
[22,] -72.91720 7.666501
[23,] -72.89820 7.668700
[24,] -72.86880 7.680100
[25,] -72.85150 7.684700
[26,] -72.83710 7.684000
[27,] -72.82100 7.700100
[28,] -72.81350 7.705900
[29,] -72.80430 7.716202
[30,] -72.79500 7.702901
[31,] -72.78120 7.703400
[32,] -72.77250 7.706299
[33,] -72.76040 7.711400
[34,] -72.76160 7.694698
[35,] -72.76100 7.680800
[36,] -72.75460 7.668700
[37,] -72.73620 7.666900
[38,] -72.73390 7.641401
[39,] -72.73680 7.633900
[40,] -72.73620 7.628699
[41,] -72.73330 7.619501
[42,] -72.73790 7.609700
[43,] -72.75690 7.607401
[44,] -72.76100 7.605101
[45,] -72.76517 7.604082
[46,] -72.77940 7.600600
[47,] -72.79500 7.605899
[48,] -72.80360 7.607601
[49,] -72.82440 7.600800
[50,] -72.83590 7.587000
[51,] -72.83990 7.580600
[52,] -72.84860 7.568500
[53,] -72.85550 7.561100
[54,] -72.85830 7.551799
[55,] -72.85490 7.533301
[56,] -72.86640 7.515501
[57,] -72.87560 7.494700
[58,] -72.89060 7.471101
[59,] -72.90210 7.458400
[60,] -72.91650 7.444600
[61,] -72.94070 7.445901
[62,] -72.94990 7.454001
[63,] -72.95510 7.467899
Slot "plotOrder":
[1] 1
Slot "labpt":
[1] -72.896733 7.590502
Slot "ID":
[1] "2"
Slot "area":
[1] 0.04137994
[[3]]
An object of class "Polygons"
Slot "Polygons":
[[1]]
An object of class "Polygon"
Slot "labpt":
[1] -72.688396 7.602174
Slot "area":
[1] 0.0150288
Slot "hole":
[1] FALSE
Slot "ringDir":
[1] 1
Slot "coords":
[,1] [,2]
[1,] -72.61860 7.663499
[2,] -72.62320 7.662300
[3,] -72.62550 7.657099
[4,] -72.63190 7.653701
[5,] -72.63470 7.649100
[6,] -72.63420 7.639901
[7,] -72.63700 7.631199
[8,] -72.64510 7.620800
[9,] -72.64970 7.608700
[10,] -72.65260 7.599501
[11,] -72.65490 7.594300
[12,] -72.65890 7.579301
[13,] -72.66290 7.566600
[14,] -72.66410 7.558000
[15,] -72.66350 7.549900
[16,] -72.65370 7.548701
[17,] -72.65430 7.544601
[18,] -72.65720 7.541800
[19,] -72.66000 7.538901
[20,] -72.66230 7.537201
[21,] -72.66640 7.532000
[22,] -72.67330 7.529700
[23,] -72.68370 7.529700
[24,] -72.69060 7.528002
[25,] -72.69870 7.525801
[26,] -72.70100 7.518300
[27,] -72.70330 7.512500
[28,] -72.71360 7.506200
[29,] -72.72280 7.503900
[30,] -72.72800 7.506200
[31,] -72.73900 7.507401
[32,] -72.74480 7.512101
[33,] -72.75110 7.518501
[34,] -72.75570 7.526001
[35,] -72.75800 7.531200
[36,] -72.75860 7.540401
[37,] -72.75570 7.547400
[38,] -72.75230 7.553700
[39,] -72.74650 7.562900
[40,] -72.75170 7.572801
[41,] -72.75860 7.580299
[42,] -72.76040 7.592402
[43,] -72.76517 7.604082
[44,] -72.76100 7.605101
[45,] -72.75690 7.607401
[46,] -72.73790 7.609700
[47,] -72.73330 7.619501
[48,] -72.72120 7.623500
[49,] -72.70910 7.634399
[50,] -72.68660 7.647001
[51,] -72.67280 7.653900
[52,] -72.66470 7.666601
[53,] -72.65610 7.697101
[54,] -72.64690 7.707500
[55,] -72.63770 7.725401
[56,] -72.63820 7.732300
[57,] -72.61520 7.721200
[58,] -72.61290 7.720601
[59,] -72.61170 7.712501
[60,] -72.61290 7.704501
[61,] -72.61060 7.700401
[62,] -72.60624 7.701616
[63,] -72.61110 7.695200
[64,] -72.61230 7.690600
[65,] -72.61340 7.679599
[66,] -72.61860 7.663499
Slot "plotOrder":
[1] 1
Slot "labpt":
[1] -72.688396 7.602174
Slot "ID":
[1] "3"
Slot "area":
[1] 0.0150288
[[4]]
An object of class "Polygons"
Slot "Polygons":
[[1]]
An object of class "Polygon"
Slot "labpt":
[1] -72.94723 8.06001
Slot "area":
[1] 0.01922532
Slot "hole":
[1] FALSE
Slot "ringDir":
[1] 1
Slot "coords":
[,1] [,2]
[1,] -72.8765 8.005901
[2,] -72.8909 7.982900
[3,] -72.8949 7.989801
[4,] -72.9093 7.985799
[5,] -72.9278 7.977801
[6,] -72.9451 7.974401
[7,] -72.9681 7.981500
[8,] -72.9820 7.982701
[9,] -72.9975 7.995501
[10,] -72.9935 8.008699
[11,] -72.9947 8.026101
[12,] -73.0097 8.043502
[13,] -73.0125 8.061400
[14,] -73.0074 8.080401
[15,] -73.0206 8.088599
[16,] -73.0235 8.093800
[17,] -73.0247 8.108202
[18,] -73.0258 8.114599
[19,] -73.0074 8.119101
[20,] -73.0011 8.134102
[21,] -72.9936 8.148499
[22,] -72.9763 8.158801
[23,] -72.9728 8.165201
[24,] -72.9723 8.159400
[25,] -72.9509 8.143100
[26,] -72.9353 8.132101
[27,] -72.9140 8.111201
[28,] -72.8973 8.100200
[29,] -72.8696 8.064201
[30,] -72.8696 8.045799
[31,] -72.8701 8.033599
[32,] -72.8747 8.014001
[33,] -72.8765 8.005901
Slot "plotOrder":
[1] 1
Slot "labpt":
[1] -72.94723 8.06001
Slot "ID":
[1] "4"
Slot "area":
[1] 0.01922532
[[5]]
An object of class "Polygons"
Slot "Polygons":
[[1]]
An object of class "Polygon"
Slot "labpt":
[1] -73.196477 7.657177
Slot "area":
[1] 0.05866379
Slot "hole":
[1] FALSE
Slot "ringDir":
[1] 1
Slot "coords":
[,1] [,2]
[1,] -73.42336 7.562488
[2,] -73.42336 7.566679
[3,] -73.42056 7.582747
[4,] -73.41987 7.589035
[5,] -73.41847 7.605801
[6,] -73.41707 7.611389
[7,] -73.41358 7.615583
[8,] -73.41078 7.622568
[9,] -73.40240 7.631651
[10,] -73.40100 7.633745
[11,] -73.40031 7.636542
[12,] -73.39681 7.642130
[13,] -73.38913 7.642827
[14,] -73.38633 7.645624
[15,] -73.38214 7.651910
[16,] -73.38004 7.656101
[17,] -73.37585 7.660992
[18,] -73.37376 7.660992
[19,] -73.36747 7.660992
[20,] -73.36468 7.660992
[21,] -73.36258 7.660295
[22,] -73.35140 7.662389
[23,] -73.34721 7.662389
[24,] -73.34023 7.663789
[25,] -73.33463 7.666581
[26,] -73.32206 7.674266
[27,] -73.31367 7.678457
[28,] -73.29482 7.680554
[29,] -73.27106 7.695225
[30,] -73.25010 7.705005
[31,] -73.24451 7.707801
[32,] -73.22984 7.718281
[33,] -73.22565 7.721772
[34,] -73.21796 7.726663
[35,] -73.21028 7.733649
[36,] -73.20679 7.739237
[37,] -73.20190 7.747622
[38,] -73.19771 7.751813
[39,] -73.19072 7.758102
[40,] -73.18304 7.765087
[41,] -73.17815 7.771375
[42,] -73.17165 7.782192
[43,] -73.16986 7.785178
[44,] -73.16570 7.788799
[45,] -73.14550 7.773700
[46,] -73.13690 7.765001
[47,] -73.12710 7.756301
[48,] -73.10860 7.760301
[49,] -73.09420 7.760201
[50,] -73.08440 7.766500
[51,] -73.07290 7.784401
[52,] -73.07000 7.794700
[53,] -73.06430 7.811499
[54,] -73.06370 7.816101
[55,] -73.05390 7.824101
[56,] -73.05160 7.831599
[57,] -73.04530 7.817200
[58,] -73.04120 7.801601
[59,] -73.03950 7.790000
[60,] -73.03310 7.773800
[61,] -73.02270 7.754701
[62,] -73.02270 7.746601
[63,] -73.02220 7.729902
[64,] -73.02620 7.705599
[65,] -73.02100 7.680800
[66,] -73.01000 7.678400
[67,] -72.99910 7.669701
[68,] -72.99270 7.659301
[69,] -73.00020 7.643700
[70,] -73.00020 7.628699
[71,] -73.00130 7.613700
[72,] -73.03079 7.603398
[73,] -73.04690 7.612700
[74,] -73.04920 7.614999
[75,] -73.06360 7.615100
[76,] -73.07110 7.612800
[77,] -73.08030 7.602400
[78,] -73.08720 7.600201
[79,] -73.08950 7.598400
[80,] -73.09410 7.598498
[81,] -73.10450 7.597901
[82,] -73.12180 7.602601
[83,] -73.13160 7.603800
[84,] -73.15060 7.604499
[85,] -73.15580 7.603300
[86,] -73.17310 7.596499
[87,] -73.18000 7.597099
[88,] -73.19330 7.604100
[89,] -73.19560 7.607002
[90,] -73.20600 7.607002
[91,] -73.22440 7.605899
[92,] -73.24060 7.604800
[93,] -73.26300 7.599699
[94,] -73.28090 7.581899
[95,] -73.28490 7.561100
[96,] -73.28490 7.549000
[97,] -73.29410 7.539799
[98,] -73.31490 7.517400
[99,] -73.33560 7.518601
[100,] -73.35580 7.527900
[101,] -73.37200 7.535501
[102,] -73.38580 7.538500
[103,] -73.40370 7.547200
[104,] -73.41580 7.554201
[105,] -73.42336 7.562488
Slot "plotOrder":
[1] 1
Slot "labpt":
[1] -73.196477 7.657177
Slot "ID":
[1] "5"
Slot "area":
[1] 0.05866379
[[6]]
An object of class "Polygons"
Slot "Polygons":
[[1]]
An object of class "Polygon"
Slot "labpt":
[1] -72.67831 7.25046
Slot "area":
[1] 0.01084088
Slot "hole":
[1] FALSE
Slot "ringDir":
[1] 1
Slot "coords":
[,1] [,2]
[1,] -72.7302 7.226701
[2,] -72.7308 7.233600
[3,] -72.7417 7.233700
[4,] -72.7527 7.238901
[5,] -72.7567 7.245301
[6,] -72.7567 7.251600
[7,] -72.7544 7.256801
[8,] -72.7417 7.271800
[9,] -72.7429 7.285101
[10,] -72.7429 7.293201
[11,] -72.7222 7.299400
[12,] -72.7152 7.292502
[13,] -72.7031 7.292401
[14,] -72.6997 7.295300
[15,] -72.6945 7.298199
[16,] -72.6847 7.297001
[17,] -72.6743 7.296301
[18,] -72.6639 7.296301
[19,] -72.6484 7.297399
[20,] -72.6391 7.294999
[21,] -72.6363 7.290400
[22,] -72.6363 7.277701
[23,] -72.6293 7.270699
[24,] -72.6161 7.269498
[25,] -72.6126 7.262602
[26,] -72.6097 7.249301
[27,] -72.6080 7.242301
[28,] -72.5988 7.231899
[29,] -72.6166 7.223300
[30,] -72.6316 7.211800
[31,] -72.6529 7.199201
[32,] -72.6668 7.190000
[33,] -72.6829 7.187201
[34,] -72.6898 7.195301
[35,] -72.6927 7.208000
[36,] -72.6950 7.217899
[37,] -72.7014 7.229399
[38,] -72.7083 7.228301
[39,] -72.7244 7.226701
[40,] -72.7302 7.226701
Slot "plotOrder":
[1] 1
Slot "labpt":
[1] -72.67831 7.25046
Slot "ID":
[1] "6"
Slot "area":
[1] 0.01084088
Slot "plotOrder":
[1] 1 5 2 4 3 6
Slot "bbox":
min max
x -73.423363 -72.5988
y 7.187201 8.2797
Slot "proj4string":
CRS arguments:
+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
Ahora, se prepara 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_Norte.de.Santander$NAME_2
labels
[1] "Abrego" "Arboledas"
[3] "Bochalema" "Bucarasica"
[5] "Cáchira" "Cácota"
[7] "Chinácota" "Chitagá"
[9] "Convención" "Cucutilla"
[11] "Durania" "El Carmen"
[13] "El Tarra" "El Zulia"
[15] "Gramalote" "Hacarí"
[17] "Herrán" "La Esperanza"
[19] "La Playa de Belén" "Labateca"
[21] "Los Patios" "Lourdes"
[23] "Mutiscua" "Ocaña"
[25] "Pamplona" "Pamplonita"
[27] "Puerto Santander" "Ragonvalia"
[29] "Salazar de las Palmas" "San Calixto"
[31] "San Cayetano" "San José de Cúcuta"
[33] "Santiago" "Santo Domingo de Silos"
[35] "Sardinata" "Teorama"
[37] "Tibú" "Toledo"
[39] "Villa Caro" "Villa del Rosario"
Posteriormente, se crea el gráfico:
m <- leaflet(ant_mun) %>% setView(-73,8,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")
Ver el gráfico
m
A continuación, se muestra otra forma de graficar, que puede resultar 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)
Finalmente, se pueden probar diferentes proveedores para mejorar el mapa. ¡Aproveche la función de completar pestañas para seleccionar el mapa base preferido con solo desplazarse por la lista de 110 proveedores! Para ello, se descomenta el siguiente chunk, se completa el código y se crea una visualización para la capital del departamento utilizado. En caso de cualquier problema, use la ayuda de R.
# leaflet()
# addProviderTiles(providers$
leaflet() %>%
addProviderTiles(providers$Esri.WorldImagery, options = providerTileOptions(opacity = 0.88)) %>%
addPolygons(data = ant_mun, popup = ant_mun$NAME_2, stroke = TRUE, fillOpacity = 0.25, smoothFactor = 0.25) %>%
setView(lng = -72.5078201, lat = 7.8939099, zoom = 20) %>%
addMarkers(lng = -72.5078201, lat = 7.8939099, popup = "Cúcuta")