Aquí se mostrará como agregar atributos a un objetos sf
Lo primero es cargar la librería sf:
library(sf)
ahora leemos un shapefile de los municipios de mi departamento:
(Mun_valle <- read_sf("val_mun.shp"))
Simple feature collection with 42 features and 6 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -81.61708 ymin: 2.931806 xmax: -75.7098 ymax: 4.975899
epsg (SRID): 4326
proj4string: +proj=longlat +datum=WGS84 +no_defs
Ahora identificamos la clase de la variable que tenemos, para eso utilizo la función class():
class(Mun_valle)
[1] "sf" "tbl_df" "tbl" "data.frame"
uso la función attribute() para conocer los atributos de mi shapefile:
attributes(Mun_valle)
$names
[1] "ISO" "NAME_0" "NAME_1" "NAME_2" "TYPE_2" "ENGTYPE_2"
[7] "geometry"
$row.names
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
[25] 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
$class
[1] "sf" "tbl_df" "tbl" "data.frame"
$sf_column
[1] "geometry"
$agr
ISO NAME_0 NAME_1 NAME_2 TYPE_2 ENGTYPE_2
<NA> <NA> <NA> <NA> <NA> <NA>
Levels: constant aggregate identity
st_crs(Mun_valle)
Uso la función st_bbox() para recuperar la caja envolvente de mi departamento:
st_bbox(Mun_valle)
xmin ymin xmax ymax
-81.617081 2.931806 -75.709801 4.975899
Ahora descargo los paquetes units y lwgeom:
install.packages("units")
Error in install.packages : Updating loaded packages
install.packages("lwgeom")
WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:
https://cran.rstudio.com/bin/windows/Rtools/
Installing package into 㤼㸱C:/Users/CABRERA/Documents/R/win-library/3.6㤼㸲
(as 㤼㸱lib㤼㸲 is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.6/lwgeom_0.1-7.zip'
Content type 'application/zip' length 5200836 bytes (5.0 MB)
downloaded 5.0 MB
package ‘lwgeom’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\CABRERA\AppData\Local\Temp\RtmpGicT86\downloaded_packages
install.packages("units")
WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:
https://cran.rstudio.com/bin/windows/Rtools/
Installing package into 㤼㸱C:/Users/CABRERA/Documents/R/win-library/3.6㤼㸲
(as 㤼㸱lib㤼㸲 is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.6/units_0.6-5.zip'
Content type 'application/zip' length 1757610 bytes (1.7 MB)
downloaded 1.7 MB
package ‘units’ successfully unpacked and MD5 sums checked
Warning in install.packages :
cannot remove prior installation of package ‘units’
Warning in install.packages :
problem copying C:\Users\CABRERA\Documents\R\win-library\3.6\00LOCK\units\libs\x64\units.dll to C:\Users\CABRERA\Documents\R\win-library\3.6\units\libs\x64\units.dll: Permission denied
Warning in install.packages :
restored ‘units’
The downloaded binary packages are in
C:\Users\CABRERA\AppData\Local\Temp\RtmpGicT86\downloaded_packages
library(lwgeom)
Linking to liblwgeom 2.5.0dev r16016, GEOS 3.6.1, PROJ 4.9.3
library(units)
udunits system database from C:/Users/CABRERA/Documents/R/win-library/3.6/units/share/udunits
Utilizo la función st_area() para calcular el área de cada uno de los municipios de mi departamento, eso lo guardo en una nueva variable que llamaré Mun_valle$area que me diferencia el area de otros atributos:
(Mun_valle$area <- st_area(Mun_valle))
Units: [m^2]
[1] 90473278 217037317 317633248 193841997 695820520 6789961578
[7] 371490773 131566706 829249011 260718595 354942250 797823378
[13] 270854215 145154379 506817569 327785029 434966936 232890424
[19] 188217360 975698436 683322745 205687416 123932908 309700291
[25] 149545306 1036861962 427677544 136320205 238461088 274895749
[31] 300769314 503339237 920251798 192057393 241248270 608834117
[37] 65844783 448327809 170367579 318317944 158853900 354388187
Uso la función set_units()
#cambio de unidades de area
set_units(Mun_valle$area, km^2)
Units: [km^2]
[1] 90.47328 217.03732 317.63325 193.84200 695.82052 6789.96158
[7] 371.49077 131.56671 829.24901 260.71860 354.94225 797.82338
[13] 270.85422 145.15438 506.81757 327.78503 434.96694 232.89042
[19] 188.21736 975.69844 683.32275 205.68742 123.93291 309.70029
[25] 149.54531 1036.86196 427.67754 136.32020 238.46109 274.89575
[31] 300.76931 503.33924 920.25180 192.05739 241.24827 608.83412
[37] 65.84478 448.32781 170.36758 318.31794 158.85390 354.38819
library(readxl)
(my_data <- read_excel("CensoAgropecuario.xls"))
Para esta parte hay que asegurarnos de que la libreria tidyverse este ejecutado:
(valle_data <- my_data %>% filter(Departamento == "Valle del Cauca"))
(valle_ndata <- rename(valle_data, NAME_2=Municipios))
llego el tiempo de hacer una union left_join:
(valle_censo <- left_join(Mun_valle, valle_ndata))
Joining, by = "NAME_2"
Simple feature collection with 42 features and 14 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -81.61708 ymin: 2.931806 xmax: -75.7098 ymax: 4.975899
epsg (SRID): 4326
proj4string: +proj=longlat +datum=WGS84 +no_defs
Ahora utilizo la librería mapview para hacer el mapa de mi municipio con los nuevos atributos que le añadi:
install.packages("mapview")
Error in install.packages : Updating loaded packages
suppressPackageStartupMessages(library(mapview))
valle_censo %>% mapview(zcol = "NAME_2", legend = TRUE, col.regions = sf.colors)
install.packages("mapview")
WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:
https://cran.rstudio.com/bin/windows/Rtools/
Installing package into 㤼㸱C:/Users/CABRERA/Documents/R/win-library/3.6㤼㸲
(as 㤼㸱lib㤼㸲 is unspecified)
Warning in install.packages :
package ‘mapview’ is in use and will not be installed
ahora vamos a ordenar las municipalidades basados en las hectareas de bosque, para eso uso la función ****
(Valle_Balto <- valle_censo %>% arrange(desc(Bosques_Ha)))
Simple feature collection with 42 features and 14 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -81.61708 ymin: 2.931806 xmax: -75.7098 ymax: 4.975899
epsg (SRID): 4326
proj4string: +proj=longlat +datum=WGS84 +no_defs