1. Introducción

En este cuaderno se realizará un mapa temático con los dos grupos de cultivos más importantes del departamento de Casanare. Para ello se utilizaran los archivo csv que se obtuvieron mediante el análisis de la base de datos EVA de 2020.

2. Instalación y lectura de librerias

#install.packages("sf")
#install.packages("readr")
#install.packages("dplyr")
#install.packages("tmap")
#install.packages("ggplot2")
#install.packages("ggrepel")
#install.packages("classInt")
#install.packages("leaflet")
library(sf)
## Warning: package 'sf' was built under R version 4.1.3
library(readr)
library(dplyr)
library(tmap)
## Warning: package 'tmap' was built under R version 4.1.3
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.1.3
library(ggrepel)
## Warning: package 'ggrepel' was built under R version 4.1.3
library(classInt)
## Warning: package 'classInt' was built under R version 4.1.3
library(leaflet)
## Warning: package 'leaflet' was built under R version 4.1.3

3. Lectura de archivos relacionados con municipios, cultivos y ciudades

list.files(pattern = c('csv'))
## [1] "casanare_arroz_2020.csv"                       
## [2] "casanare_cereales_2020.csv"                    
## [3] "casanare_frutales_2020.csv"                    
## [4] "casanare_oleag_2020.csv"                       
## [5] "casanare_pa_2020.csv"                          
## [6] "co.csv"                                        
## [7] "Evaluaciones_Agropecuarias_Municipales_EVA.csv"
list.files("../DATOS")
##  [1] "COL_alt.grd"               "COL_alt.gri"              
##  [3] "COL_alt.vrt"               "COL_alt.zip"              
##  [5] "DPTOCASANARE.cpg"          "DPTOCASANARE.dbf"         
##  [7] "DPTOCASANARE.prj"          "DPTOCASANARE.qmd"         
##  [9] "DPTOCASANARE.shp"          "DPTOCASANARE.shx"         
## [11] "ELV_CASANARE.tif"          "ELV_CASANARE.tif.aux.xml" 
## [13] "MGN_MPIO_POLITICO.CPG"     "MGN_MPIO_POLITICO.dbf"    
## [15] "MGN_MPIO_POLITICO.prj"     "MGN_MPIO_POLITICO.sbn"    
## [17] "MGN_MPIO_POLITICO.sbx"     "MGN_MPIO_POLITICO.shp"    
## [19] "MGN_MPIO_POLITICO.shp.xml" "MGN_MPIO_POLITICO.shx"    
## [21] "MGN2020_MPIO_POLITICO.rar"
(mun.tmp = st_read("C:\\Users\\ynata\\OneDrive\\Documentos\\GGB2022\\Datos\\DPTOCASANARE.shp"))
## Reading layer `DPTOCASANARE' from data source 
##   `C:\Users\ynata\OneDrive\Documentos\GGB2022\Datos\DPTOCASANARE.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 19 features and 11 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -73.07777 ymin: 4.287476 xmax: -69.83591 ymax: 6.346111
## Geodetic CRS:  WGS 84
mun.tmp %>% select( MPIO_CCNCT, MPIO_CNMBR, MPIO_NAREA) -> munic
munic
(cereales <- read_csv("casanare_cereales_2020.csv", show_col_types = FALSE))
(cities <- read_csv("C:\\Users\\ynata\\OneDrive\\Documentos\\GGB2022\\misegundocuaderno\\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.
# Convert data frame to simple feature object
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

# find points within polygons
sf.cities.joined <- st_join(sf.cities, munic, join = st_within)
sf.cities.joined
cnare.cities <- dplyr::filter(sf.cities.joined, admin_name=='Casanare')
cnare.cities 

5. Mapa para el grupo de cultivo de los Cereales

names(munic)
## [1] "MPIO_CCNCT" "MPIO_CNMBR" "MPIO_NAREA" "geometry"
class(cereales$Cod_Mun)
## [1] "numeric"
class(munic$MPIO_CCNCT)
## [1] "character"
cereales$Cod_Mun <- as.character(cereales$Cod_Mun)
munic_cereales <- left_join(munic, cereales,  by = c("MPIO_CCNCT" = "Cod_Mun"))
munic_cereales
breaks <- classIntervals(na.omit(munic_cereales$max_prod), n = 6, style = 'fisher')

# Etiqueta
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_cereales <-  munic_cereales %>%
  mutate(faktor_class = factor(cut(max_prod, breaks$brks, include.lowest = T), labels = lab_vec))
# change attribute name
munic_cereales$Produccion = munic_cereales$faktor_class
munic_cereales$mid <- sf::st_centroid(munic_cereales$geometry)
LONG = st_coordinates(munic_cereales$mid)[,1]
LAT = st_coordinates(munic_cereales$mid)[,2]
ggplot(data = munic_cereales) +
   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) +
  scale_fill_brewer(palette = "BuGn" )

facet = "max_prod"
cer_map =  
  tm_shape(munic_cereales) + tm_polygons(facet, palette = "BuGn") + tm_text(text = "MPIO_CNMBR", size = 0.7, fontfamily = "sans") +
  tm_shape(cnare.cities) + tm_symbols(shape = 2, col = "red", size = 0.20) +
  tm_credits("Data source: UPRA (2020)", fontface = "bold") +
  tm_layout(main.title = "Produccion de frutales en 2020",
            main.title.fontface = "bold.italic", 
            legend.title.fontfamily = "monospace") +
  tm_scale_bar(position = c("left", "bottom"))
tmap_mode("view")

cer_map

6. Mapa para el grupo de cultivo de las Leguminosas Y Oleaginosas

(oleag <- read_csv("casanare_oleag_2020.csv"))
## Rows: 14 Columns: 4
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (2): Municipio, Grupo
## dbl (2): Cod_Mun, max_prod
## 
## 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.
oleag$Cod_Mun = as.character(oleag$Cod_Mun)
names(oleag)
## [1] "Cod_Mun"   "Municipio" "Grupo"     "max_prod"
names(munic)
## [1] "MPIO_CCNCT" "MPIO_CNMBR" "MPIO_NAREA" "geometry"
(munic_oleag = left_join(munic, oleag, by = c("MPIO_CCNCT" = "Cod_Mun")))
facet = "max_prod"
oleag_map =  
  tm_shape(munic_oleag) + tm_polygons(facet, palette = "BuGn") + tm_text(text = "MPIO_CNMBR", size = 0.7, fontfamily = "sans") +
  tm_shape(cnare.cities) + tm_symbols(shape = 2, col = "red", size = 0.20) +
  tm_credits("Data source: UPRA (2020)", fontface = "bold") +
  tm_layout(main.title = "Produccion de frutales en 2020",
            main.title.fontface = "bold.italic", 
            legend.title.fontfamily = "monospace") +
  tm_scale_bar(position = c("left", "bottom"))
tmap_mode("view")

oleag_map
(arroz <- read_csv("casanare_arroz_2020.csv", show_col_types = FALSE))
arroz$Cod_Mun <- as.character(arroz$Cod_Mun)
munic_arroz <- left_join(munic, arroz,  by = c("MPIO_CCNCT" = "Cod_Mun"))
munic_arroz
facet = "max_prod_arroz"
arrozmap =  
  tm_shape(munic_arroz) + tm_polygons(facet, palette = "BuGn") + tm_text(text = "MPIO_CNMBR", size = 0.7, fontfamily = "sans") +
  tm_shape(cnare.cities) + tm_symbols(shape = 2, col = "red", size = 0.20) +
  tm_credits("Data source: UPRA (2020)", fontface = "bold") +
  tm_layout(main.title = "Produccion de Arroz en 2020",
            main.title.fontface = "bold.italic", 
            legend.title.fontfamily = "monospace") +
  tm_scale_bar(position = c("left", "bottom"))
tmap_mode("view")

arrozmap
(palmadeaceite <- read_csv("casanare_pa_2020.csv", show_col_types = FALSE))
palmadeaceite$Cod_Mun <- as.character(palmadeaceite$Cod_Mun)
munic_palmadeaceite <- left_join(munic, palmadeaceite,  by = c("MPIO_CCNCT" = "Cod_Mun"))
munic_palmadeaceite
facet = "max_prod_pa"
palmaceitemap =  
  tm_shape(munic_palmadeaceite) + tm_polygons(facet, palette = "BuGn") + tm_text(text = "MPIO_CNMBR", size = 0.7, fontfamily = "sans") +
  tm_shape(cnare.cities) + tm_symbols(shape = 2, col = "red", size = 0.20) +
  tm_credits("Data source: UPRA (2020)", fontface = "bold") +
  tm_layout(main.title = "Produccion de aceite de palma 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
palmaceitemap
## Credits not supported in view mode.
## Symbol shapes other than circles or icons are not supported in view mode.

Bibliografía

Lizarazo, I., 2022. Getting started with thematic maps. Available at https://rpubs.com/ials2un/thematic_maps_v2.