#1. Introducción. A partir de las informaciones previamente obtenidas en el análisis de los datos de la evaluación agrícola para el 2021, usamos los datos que se obtuvieron de esta.
#2. Setup
Se intalan en primer lugar ciertos paquetes que permitirán las funciones de lectura de datos
#install.packages("tidyverse")
#install.packages("sf")
#install.packages("tibble")
#3. Lectura de los archivos relacionados con el departamento del Tolima; sus ciudades y cultivos.
Se analizará la información relativa al departamento del Tolima. Para ello, en primer lugar se debe conocer qué tipo de información existe en la carpeta en la que se está trabajando.
Determinamos la ruta de trabajo:
setwd("~/AGRONOMIA/GEOMATICA/GEOMATICABASICA/GB/SegundoNotebookR/INPUTS")
library("tidyverse")
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.4
## ✔ tibble 3.1.8 ✔ dplyr 1.0.9
## ✔ tidyr 1.2.0 ✔ stringr 1.4.1
## ✔ readr 2.1.2 ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
En primer lugar, identificamos los archivos de formato .csv
list.files(pattern = c("csv"))
## [1] "cereales_tolima_2021.csv" "CITIESTOLIMA.csv"
## [3] "COL_adm1.csv" "CTTolima_2021.csv"
## [5] "MUNTOLIMA.csv" "tolima_frutales_2021.csv"
## [7] "tolima_leguminosas_2021.csv" "tolima_oleaginosas_2021.csv"
## [9] "TOLIMAMUNICIPIOS.csv" "TOLIMAMUNICIPIOSAHORASI.csv"
Después identificamos los archivos en formato shapefile.
list.files(pattern = c("shp"))
## [1] "Departamentos.shp"
Ahora, los archivos formato geojson
list.files(pattern = c("geojson"))
## [1] "MunicipiosTolima.geojson"
list.files(pattern = c("gpkg"))
## [1] "DepartamentosTol.gpkg" "Tolima.gpkg" "TolimaCities.gpkg"
## [4] "TolimaCiudades.gpkg"
read_csv("tolima_frutales_2021.csv")
## Rows: 47 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): MUN, GRUPOCULT
## dbl (2): CODMUN, max_prod
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Ahora que se tiene claro a qué archivos vamos a recurrir, se puede proceder a mostrar el contenido de aquellos que consideramos relevantes.
(frutales = read_csv("tolima_frutales_2021.csv", show_col_types = FALSE))
frutales
library("sf")
## Linking to GEOS 3.9.1, GDAL 3.4.3, PROJ 7.2.1; sf_use_s2() is TRUE
municipios = st_read("MunicipiosTolima.geojson")
## Reading layer `MunicipiosTolima' from data source
## `C:\Users\gabri\Documents\AGRONOMIA\GEOMATICA\GEOMATICABASICA\GB\SegundoNotebookR\INPUTS\MunicipiosTolima.geojson'
## using driver `GeoJSON'
## Simple feature collection with 47 features and 12 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -76.10574 ymin: 2.871081 xmax: -74.47482 ymax: 5.319342
## Geodetic CRS: MAGNA-SIRGAS
municipios
(cities = st_read("TolimaCities.gpkg"))
## Reading layer `TolimaCities' from data source
## `C:\Users\gabri\Documents\AGRONOMIA\GEOMATICA\GEOMATICABASICA\GB\SegundoNotebookR\INPUTS\TolimaCities.gpkg'
## using driver `GPKG'
## Simple feature collection with 18 features and 11 fields
## Geometry type: POINT
## Dimension: XYM
## Bounding box: xmin: -75.6447 ymin: 3.1964 xmax: -74.6428 ymax: 5.2069
## m_range: mmin: 7563 mmax: 541101
## Geodetic CRS: WGS 84
#4. Mapa de los grupos de cultivos más importantes para el departamento del Tolima
#install.packages ("tmap")
#install.packages ("ggplot2")
#install.packages ("classInt")
library(tmap)
library(ggplot2)
library(ggrepel)
library(classInt)
Ahora podemos unir por medio del código de los municipios tolimenses, la información relativa a la EVA 2021, junto con los municipios.
class(frutales$CODMUN)
## [1] "numeric"
class(municipios$MPIO_CCDGO)
## [1] "character"
Ya que observamos que uno de los códigos es de tipo numérico y el otro es carácter, debemos ponerlos en el mismo tipo.
frutales$CODMUN=as.character(frutales$CODMUN)
frutales
Hecho esto, podemos determinar un vector que los una:
mun_frutales= dplyr::left_join(municipios,frutales,by=c('MPIO_CDPMP'='CODMUN'))
mun_frutales
breaks <- classIntervals(frutales$max_prod,n=6, style= "fisher")
lab_vec <- vector(length = length(breaks$brks)-1)
rounded_breaks<-round(breaks$brks,2)
lab_vec[1]<-paste0("[",rounded_breaks[1],"-",rounded_breaks[2],"]")
for(i in 2:(length(breaks$brks)-1)){
lab_vec[i]<-paste0("(",rounded_breaks[i],"-",rounded_breaks[i+1],"]")
}
mun_frutales <- mun_frutales %>%
dplyr::mutate(faktor_class = factor(cut(max_prod, breaks$brks, include.lowest = T), labels = lab_vec))
mun_frutales$produccion = mun_frutales$faktor_class
(mun_frutales$mid<-sf::st_centroid(mun_frutales$geometry))
## Geometry set for 47 features
## Geometry type: POINT
## Dimension: XY
## Bounding box: xmin: -75.85482 ymin: 3.098974 xmax: -74.53964 ymax: 5.235322
## Geodetic CRS: MAGNA-SIRGAS
## First 5 geometries:
## POINT (-75.25258 4.451919)
## POINT (-74.94078 3.390013)
## POINT (-74.98631 4.582622)
## POINT (-74.80442 4.824907)
## POINT (-75.19117 4.631786)
LONG=st_coordinates(mun_frutales$mid)[,1]
LAT=st_coordinates(mun_frutales$mid)[,2]
ggplot(data= mun_frutales)+
geom_sf(aes(fill= produccion))+
geom_label_repel (aes(x = LONG, y =LAT, label = MPIO_CNMBR),
label.padding = unit(0.05,"lines"),
label.r=unit(0.025,"lines"),
label.size=0.05)
## Warning: ggrepel: 10 unlabeled data points (too many overlaps). Consider
## increasing max.overlaps
read_csv("CTTolima_2021.csv")
## Rows: 44 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): MUN, GRUPOCULT
## dbl (2): CODMUN, max_prod
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
CTT=read_csv("CTTolima_2021.csv",show_col_types = FALSE)
CTT$CODMUN=as.character((CTT$CODMUN))
munCTT = left_join(municipios, CTT, by=c("MPIO_CDPMP"="CODMUN"))
munCTT
facet="max_prod"
CTT_map=
tm_shape(munCTT)+tm_polygons(facet)+tm_text(text="MPIO_CNMBR",size = 0.5,fontfamily = "sans")+
tm_shape(cities)+tm_symbols(shape = 2,col="red",size = 0.25)+
tm_credits("Fuente: UPRA (2021)",fontface="bold")+
tm_layout(main.title="Producción de cultivos tropicales tradicionales en 2021",
main.title.fontface = "bold.italic",
legend.title.fontfamily = "monospace")+
tm_scale_bar(position=c("left","bottom"))
tmap_mode("view")
## tmap mode set to interactive viewing
CTT_map
## Credits not supported in view mode.
## Symbol shapes other than circles or icons are not supported in view mode.
read_csv("cereales_tolima_2021.csv")
## Rows: 44 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): MUN, GRUPOCULT
## dbl (2): CODMUN, max_prod
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
cereales=read_csv("cereales_tolima_2021.csv",show_col_types = FALSE)
cereales$CODMUN=as.character((cereales$CODMUN))
cereales_tolima=left_join(municipios,cereales,by=c("MPIO_CDPMP"="CODMUN"))
cereales_tolima
facet="max_prod"
cereales_mapa=
tm_shape(cereales_tolima)+tm_polygons(facet)+tm_text(text="MPIO_CNMBR",size = 0.5,fontfamily = "sans")+
tm_shape(cities)+tm_symbols(shape = 2,col="red",size = 0.25)+
tm_credits("Fuente: UPRA (2021)",fontface="bold")+
tm_layout(main.title="Producción de cereales en el Tolima (2021)",
main.title.fontface = "bold.italic",
legend.title.fontfamily = "monospace")+
tm_scale_bar(position=c("left","bottom"))
tmap_mode("view")
## tmap mode set to interactive viewing
cereales_mapa
## Credits not supported in view mode.
## Symbol shapes other than circles or icons are not supported in view mode.
#5. Reproducibilidad
Citar como Campos, G., 2022.
sessionInfo()
## R version 4.2.1 (2022-06-23 ucrt)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 19044)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=Spanish_Colombia.utf8 LC_CTYPE=Spanish_Colombia.utf8
## [3] LC_MONETARY=Spanish_Colombia.utf8 LC_NUMERIC=C
## [5] LC_TIME=Spanish_Colombia.utf8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] classInt_0.4-7 ggrepel_0.9.1 tmap_3.3-3 sf_1.0-8
## [5] forcats_0.5.2 stringr_1.4.1 dplyr_1.0.9 purrr_0.3.4
## [9] readr_2.1.2 tidyr_1.2.0 tibble_3.1.8 ggplot2_3.3.6
## [13] tidyverse_1.3.2
##
## loaded via a namespace (and not attached):
## [1] fs_1.5.2 lubridate_1.8.0 bit64_4.0.5
## [4] RColorBrewer_1.1-3 httr_1.4.4 tools_4.2.1
## [7] backports_1.4.1 bslib_0.4.0 utf8_1.2.2
## [10] R6_2.5.1 KernSmooth_2.23-20 DBI_1.1.3
## [13] colorspace_2.0-3 raster_3.5-29 sp_1.5-0
## [16] withr_2.5.0 tidyselect_1.1.2 leaflet_2.1.1
## [19] bit_4.0.4 compiler_4.2.1 leafem_0.2.0
## [22] cli_3.3.0 rvest_1.0.3 xml2_1.3.3
## [25] sass_0.4.2 scales_1.2.1 proxy_0.4-27
## [28] digest_0.6.29 rmarkdown_2.16 base64enc_0.1-3
## [31] dichromat_2.0-0.1 pkgconfig_2.0.3 htmltools_0.5.3
## [34] highr_0.9 dbplyr_2.2.1 fastmap_1.1.0
## [37] htmlwidgets_1.5.4 rlang_1.0.4 readxl_1.4.1
## [40] rstudioapi_0.13 farver_2.1.1 jquerylib_0.1.4
## [43] generics_0.1.3 jsonlite_1.8.0 crosstalk_1.2.0
## [46] vroom_1.5.7 googlesheets4_1.0.1 magrittr_2.0.3
## [49] s2_1.1.0 Rcpp_1.0.9 munsell_0.5.0
## [52] fansi_1.0.3 abind_1.4-5 terra_1.6-17
## [55] lifecycle_1.0.1 stringi_1.7.8 leafsync_0.1.0
## [58] yaml_2.3.5 tmaptools_3.1-1 grid_4.2.1
## [61] parallel_4.2.1 crayon_1.5.1 lattice_0.20-45
## [64] haven_2.5.0 stars_0.5-6 hms_1.1.2
## [67] knitr_1.40 pillar_1.8.1 markdown_1.1
## [70] codetools_0.2-18 wk_0.6.0 reprex_2.0.2
## [73] XML_3.99-0.11 glue_1.6.2 evaluate_0.16
## [76] leaflet.providers_1.9.0 modelr_0.1.9 png_0.1-7
## [79] vctrs_0.4.1 tzdb_0.3.0 cellranger_1.1.0
## [82] gtable_0.3.1 assertthat_0.2.1 cachem_1.0.6
## [85] xfun_0.32 mime_0.12 lwgeom_0.2-9
## [88] broom_1.0.1 e1071_1.7-11 class_7.3-20
## [91] googledrive_2.0.0 viridisLite_0.4.1 gargle_1.2.1
## [94] units_0.8-0 ellipsis_0.3.2