Este cuaderno muestra como agregar atributos a un simple feature.
Primero caergamos la libreria sf:
library(sf)
Luego, vamos a leer el shapefile de los municipios del departamento de Huila.
(mun_hui<-read_sf("HUMUN.shp"))
Simple feature collection with 37 features and 6 fields
geometry type: POLYGON
dimension: XY
bbox: xmin: -76.6402 ymin: 1.493471 xmax: -74.5054 ymax: 3.807901
epsg (SRID): 4326
proj4string: +proj=longlat +datum=WGS84 +no_defs
### :D
Ahora identificamos la clase de objeto que es esta variable:
class(mun_hui)
[1] "sf" "tbl_df" "tbl" "data.frame"
Ahora identifiquemos los atributos de mun_hui:
attributes(mun_hui)
$names
[1] "ISO" "NAME_0" "NAME_1" "NAME_2" "TYPE_2" "ENGTYPE_2" "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 26 27 28 29 30 31 32 33 34 35 36
[37] 37
$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
Vamos a conocer el sistema coordinado de referencia (CRS por sus siglas en inglés)
st_crs(mun_hui)
Coordinate Reference System:
EPSG: 4326
proj4string: "+proj=longlat +datum=WGS84 +no_defs"
Ahora vamos a extraer los limites:
st_bbox(mun_hui)
xmin ymin xmax ymax
-76.640198 1.493471 -74.505402 3.807901
Instalción de dos librerías adicionales
### install.packages("lwgeom")
### install.packages("units")
library(lwgeom)
library(units)
Vamos a guardar el area medida por la funcion st_area en un vector de la siguietne manera:
(mun_hui$area<-st_area(mun_hui))
Units: [m^2]
[1] 521220868 250115182 686474286 841656751 168938262 400655279 385598858 1617567362 95917674
[10] 541001806 699625854 446870878 173695303 583969387 461807525 303692783 1230699797 437749546
[19] 1472158852 259051762 182852039 681052830 455426296 221338955 710933560 518557982 393416033
[28] 1470839841 477081011 309846293 388844769 655189242 683374347 515511593 204301815 647476546
[37] 206602007
Estas areas estan expresadas en metros cuadrados, pero podemos cambiarla a otras unidades de la siguiente manera:
(x<-set_units(mun_hui$area, km^2))
Units: [km^2]
[1] 521.22087 250.11518 686.47429 841.65675 168.93826 400.65528 385.59886 1617.56736 95.91767
[10] 541.00181 699.62585 446.87088 173.69530 583.96939 461.80752 303.69278 1230.69980 437.74955
[19] 1472.15885 259.05176 182.85204 681.05283 455.42630 221.33896 710.93356 518.55798 393.41603
[28] 1470.83984 477.08101 309.84629 388.84477 655.18924 683.37435 515.51159 204.30182 647.47655
[37] 206.60201
(hectareas<-x*100)
Units: [km^2]
[1] 52122.087 25011.518 68647.429 84165.675 16893.826 40065.528 38559.886 161756.736 9591.767
[10] 54100.181 69962.585 44687.088 17369.530 58396.939 46180.752 30369.278 123069.980 43774.955
[19] 147215.885 25905.176 18285.204 68105.283 45542.630 22133.896 71093.356 51855.798 39341.603
[28] 147083.984 47708.101 30984.629 38884.477 65518.924 68337.435 51551.159 20430.182 64747.655
[37] 20660.201
library(readxl)
(mun_data<- read_excel("ANEXOSMUNICIPALES.xls"))
### :D
library(tidyverse)
(hui_data <- mun_data%>% filter(DEPTO=="Huila"))
Ahora se debe renombrar los municipios para que coincida con los datos del sf, de la siguieten manera:
(datos_censo <- rename(hui_data, NAME_2= NOMBRE_MUN))
Luego de lo anterior se debe hacer una unión, una lef_join o unión por la izquierda, el cual hace que el atributo de la tabla se una el valor contiguo.
(hui_censo <- left_join(mun_hui,datos_censo))
Joining, by = "NAME_2"
Simple feature collection with 37 features and 14 fields
geometry type: POLYGON
dimension: XY
bbox: xmin: -76.6402 ymin: 1.493471 xmax: -74.5054 ymax: 3.807901
epsg (SRID): 4326
proj4string: +proj=longlat +datum=WGS84 +no_defs
suppressPackageStartupMessages(library(mapview))
library(tidyverse)
library(sf)
hui_censo %>% mapview(zcol="NAME_2",legend=T, col.regions= sf.colors)
Vamos a ordenar los municipios basados en las hectareas de bosque
(hui_clean<- hui_censo%>%arrange(desc(HA_BOSQUE))%>% select(NAME_2, DMUN, HA_BOSQUE, HA_AGRO, HA_NOAGRO, HA_OTROS))
Simple feature collection with 37 features and 6 fields
geometry type: POLYGON
dimension: XY
bbox: xmin: -76.6402 ymin: 1.493471 xmax: -74.5054 ymax: 3.807901
epsg (SRID): 4326
proj4string: +proj=longlat +datum=WGS84 +no_defs