1. Introducción

“En este cuaderno se va a elaborar un mapa temático cuya función sera mostrar la participación de los dos grupos de cultivos mas importantes del departamento de Boyacá”

2. Configuración

library(sf)
## Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1; sf_use_s2() is TRUE
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(readr)

3. Leer fichas relacionadas con municipios, cultivos y ciudades.

list.files("C:/Users/GERARDO/Desktop/GB2022/datos", pattern=c('csv'))
## [1] "co.csv"                                        
## [2] "Evaluaciones_Agropecuarias_Municipales_EVA.csv"
## [3] "J.ber_Hort_2020.csv"                           
## [4] "J.ber_Tuber_2020.csv"
list.files('C:/Users/GERARDO/Desktop/GB2022/datos/15_BOYACA/Administrativo')
##  [1] "MGN_DPTO_POLITICO.cpg"                            
##  [2] "MGN_DPTO_POLITICO.dbf"                            
##  [3] "MGN_DPTO_POLITICO.prj"                            
##  [4] "MGN_DPTO_POLITICO.sbn"                            
##  [5] "MGN_DPTO_POLITICO.sbx"                            
##  [6] "MGN_DPTO_POLITICO.shp"                            
##  [7] "MGN_DPTO_POLITICO.shp.DG_EST54.1344.12652.sr.lock"
##  [8] "MGN_DPTO_POLITICO.shp.xml"                        
##  [9] "MGN_DPTO_POLITICO.shx"                            
## [10] "MGN_MPIO_POLITICO.cpg"                            
## [11] "MGN_MPIO_POLITICO.dbf"                            
## [12] "MGN_MPIO_POLITICO.prj"                            
## [13] "MGN_MPIO_POLITICO.sbn"                            
## [14] "MGN_MPIO_POLITICO.sbx"                            
## [15] "MGN_MPIO_POLITICO.shp"                            
## [16] "MGN_MPIO_POLITICO.shp.DG_EST54.1344.12652.sr.lock"
## [17] "MGN_MPIO_POLITICO.shp.xml"                        
## [18] "MGN_MPIO_POLITICO.shx"
(Tuberculos = read_csv("C:/Users/GERARDO/Desktop/GB2022/datos/J.ber_Tuber_2020.csv",show_col_types = FALSE))
(mun.tmp = st_read('C:/Users/GERARDO/Desktop/GB2022/datos/15_BOYACA/Administrativo/MGN_MPIO_POLITICO.shp'))
## Reading layer `MGN_MPIO_POLITICO' from data source 
##   `C:\Users\GERARDO\Desktop\GB2022\Datos\15_BOYACA\ADMINISTRATIVO\MGN_MPIO_POLITICO.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 123 features and 9 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -74.66496 ymin: 4.655196 xmax: -71.94885 ymax: 7.055557
## Geodetic CRS:  WGS 84
mun.tmp %>% select(MPIO_CCDGO, MPIO_CNMBR, MPIO_NAREA) -> municipios 
municipios
(cities = read_csv("C:/Users/GERARDO/Desktop/GB2022/datos/co.csv"))
## Rows: 1102 Columns: 9
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (5): city, country, iso2, admin_name, capital
## dbl (4): lat, lng, population, population_proper
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
sf.cities <-  st_as_sf(x = cities, 
                        coords = c("lng", "lat"))
sf.cities
st_crs(sf.cities) <- 4326

4. Subconjunto de datos relevantes para nuestro departamento

sf.cities.joined <- st_join(sf.cities, municipios, join = st_within)
sf.cities.joined
byc.cities = dplyr::filter(sf.cities.joined, admin_name=='Boyacá')
byc.cities

5. Hacer un mapa para el grupo de cultivos mas importante

library(tmap)
library(ggplot2)
library(ggrepel)
library(classInt)
class(Tuberculos$Cod_Mun)
## [1] "numeric"
class(municipios$MPIO_CCDGO)
## [1] "character"
Tuberculos$Cod_Mun = as.character(Tuberculos$Cod_Mun)
munic_Tuberculos = left_join(municipios, Tuberculos, by = c("MPIO_CCDGO" = "Cod_Mun"))
munic_Tuberculos
breaks <- classIntervals(munic_Tuberculos$max_prod, n = 6, style = 'fisher')
## Warning in classIntervals(munic_Tuberculos$max_prod, n = 6, style = "fisher"):
## var has missing values, omitted in finding classes
#label breaks
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], ']')
}
munic_Tuberculos <-  munic_Tuberculos %>%
  mutate(faktor_class = factor(cut(max_prod, breaks$brks, include.lowest = T), labels = lab_vec))
# change attribute name
munic_Tuberculos$Produccion = munic_Tuberculos$faktor_class
munic_Tuberculos$mid <- sf::st_centroid(munic_Tuberculos$geometry)
LONG = st_coordinates(munic_Tuberculos$mid)[,1]
LAT = st_coordinates(munic_Tuberculos$mid)[,2]
ggplot(data = munic_Tuberculos) +
   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: 111 unlabeled data points (too many overlaps). Consider
## increasing max.overlaps

6. Hacer un nuevo mapa para el segundo grupo de cultivos mas grande

(Hortalizas = read_csv("C:/Users/GERARDO/Desktop/GB2022/datos/J.ber_Hort_2020.csv",show_col_types = FALSE))
Hortalizas$Cod_Mun = as.character(Hortalizas$Cod_Mun)
munic_Hortalizas = left_join(municipios, Hortalizas, by = c("MPIO_CCDGO" = "Cod_Mun"))
munic_Hortalizas
facet = "max_prod"
Hortalizas_map =  
  tm_shape(munic_Hortalizas) + tm_polygons(facet) + tm_text(text = "MPIO_CNMBR", size = 0.7, fontfamily = "sans") +
  tm_shape(byc.cities) + tm_symbols(shape = 2, col = "red", size = 0.20) +
  tm_credits("Data source: UPRA (2020)", fontface = "bold") +
  tm_layout(main.title = "Produccion de hortalizas en 2020",
            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
Hortalizas_map
## Credits not supported in view mode.
## Symbol shapes other than circles or icons are not supported in view mode.

7. Referencias

“Lizarazo, I. Reading and processing municipal agricultural statistics for 2020. Available at: https://rpubs.com/ials2un/readingEVAv1.”

sessionInfo()
## R version 4.1.3 (2022-03-10)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 19043)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=Spanish_Colombia.1252  LC_CTYPE=Spanish_Colombia.1252   
## [3] LC_MONETARY=Spanish_Colombia.1252 LC_NUMERIC=C                     
## [5] LC_TIME=Spanish_Colombia.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] classInt_0.4-3 ggrepel_0.9.1  ggplot2_3.3.5  tmap_3.3-3     readr_2.1.2   
## [6] dplyr_1.0.8    sf_1.0-7      
## 
## loaded via a namespace (and not attached):
##  [1] sass_0.4.1              bit64_4.0.5             vroom_1.5.7            
##  [4] jsonlite_1.8.0          viridisLite_0.4.0       bslib_0.3.1            
##  [7] assertthat_0.2.1        highr_0.9               sp_1.4-6               
## [10] yaml_2.3.5              pillar_1.7.0            lattice_0.20-45        
## [13] glue_1.6.2              digest_0.6.29           RColorBrewer_1.1-2     
## [16] leaflet.providers_1.9.0 colorspace_2.0-3        htmltools_0.5.2        
## [19] XML_3.99-0.9            pkgconfig_2.0.3         raster_3.5-15          
## [22] stars_0.5-5             s2_1.0.7                purrr_0.3.4            
## [25] scales_1.1.1            terra_1.5-21            tzdb_0.2.0             
## [28] tibble_3.1.6            proxy_0.4-26            farver_2.1.0           
## [31] generics_0.1.2          ellipsis_0.3.2          withr_2.5.0            
## [34] leafsync_0.1.0          cli_3.2.0               mime_0.12              
## [37] magrittr_2.0.2          crayon_1.5.1            evaluate_0.15          
## [40] fansi_1.0.3             lwgeom_0.2-8            class_7.3-20           
## [43] tools_4.1.3             hms_1.1.1               lifecycle_1.0.1        
## [46] stringr_1.4.0           munsell_0.5.0           compiler_4.1.3         
## [49] jquerylib_0.1.4         e1071_1.7-9             rlang_1.0.2            
## [52] units_0.8-0             grid_4.1.3              tmaptools_3.1-1        
## [55] dichromat_2.0-0         rstudioapi_0.13         htmlwidgets_1.5.4      
## [58] crosstalk_1.2.0         leafem_0.1.6            base64enc_0.1-3        
## [61] rmarkdown_2.13          wk_0.6.0                gtable_0.3.0           
## [64] codetools_0.2-18        abind_1.4-5             DBI_1.1.2              
## [67] markdown_1.1            R6_2.5.1                knitr_1.38             
## [70] fastmap_1.1.0           bit_4.0.4               utf8_1.2.2             
## [73] KernSmooth_2.23-20      stringi_1.7.6           parallel_4.1.3         
## [76] Rcpp_1.0.8.3            vctrs_0.3.8             png_0.1-7              
## [79] leaflet_2.1.1           tidyselect_1.1.2        xfun_0.30