1. Introduccion

En este libro de r se planea filtrar datos de las evaluaciones agropecuarias del Cauca, esto con el fin de identificar cultivos mas importantes o mas representativos del Cauca, dando importancia a los respectivos municipios en los cuales se cultivan, con esta informacion se pueden hacer analisis de comportamiento de las politicas agropecucarias de cada region del Cauca o identificar oportunidades en el campo agricola.

2. Setup

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(dplyr)
library(readr)
library(ggplot2)
library(knitr)
library(rmarkdown)

3. Leer archivos

list.files("./datos", pattern=c('csv'))
## [1] "Evaluaciones_Agropecuarias_Municipales_EVA.csv"
(eva = read_csv("./datos/Evaluaciones_Agropecuarias_Municipales_EVA.csv", col_names = TRUE,
                show_col_types = NULL))
## Rows: 8385 Columns: 17
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (10): DEPARTAMENTO, MUNICIPIO, GRUPO 
## DE CULTIVO, SUBGRUPO 
## DE CULTIVO, ...
## dbl  (7): CÓD. 
## DEP., CÓD. MUN., AÑO, Área Sembrada
## (ha), Área Cosechada
## (ha...
## 
## 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.
names(eva)
##  [1] "CÓD. \nDEP."                                  
##  [2] "DEPARTAMENTO"                                 
##  [3] "CÓD. MUN."                                    
##  [4] "MUNICIPIO"                                    
##  [5] "GRUPO \nDE CULTIVO"                           
##  [6] "SUBGRUPO \nDE CULTIVO"                        
##  [7] "CULTIVO"                                      
##  [8] "DESAGREGACIÓN REGIONAL Y/O SISTEMA PRODUCTIVO"
##  [9] "AÑO"                                          
## [10] "PERIODO"                                      
## [11] "Área Sembrada\n(ha)"                          
## [12] "Área Cosechada\n(ha)"                         
## [13] "Producción\n(t)"                              
## [14] "Rendimiento\n(t/ha)"                          
## [15] "ESTADO FISICO PRODUCCION"                     
## [16] "NOMBRE \nCIENTIFICO"                          
## [17] "CICLO DE CULTIVO"
##  [1] "CÓD. \nDEP."                                  
##  [2] "DEPARTAMENTO"                                 
##  [3] "CÓD. MUN."                                    
##  [4] "MUNICIPIO"                                    
##  [5] "GRUPO \nDE CULTIVO"                           
##  [6] "SUBGRUPO \nDE CULTIVO"                        
##  [7] "CULTIVO"                                      
##  [8] "DESAGREGACIÓN REGIONAL Y/O SISTEMA PRODUCTIVO"
##  [9] "AÑO"                                          
## [10] "PERIODO"                                      
## [11] "Área Sembrada\n(ha)"                          
## [12] "Área Cosechada\n(ha)"                         
## [13] "Producción\n(t)"                              
## [14] "Rendimiento\n(t/ha)"                          
## [15] "ESTADO FISICO PRODUCCION"                     
## [16] "NOMBRE \nCIENTIFICO"                          
## [17] "CICLO DE CULTIVO"
# check the objet output in the last chunk and
# change attribute names according to your own data
eva %>% dplyr::select('CÓD. MUN.':'ESTADO FISICO PRODUCCION') -> eva.tmp
# make sure to use the column names that are in your eva.tmp object
eva.tmp %>%  dplyr::rename('Cod_Mun' = 'CÓD. MUN.', 
                         'Grupo' = 'GRUPO \nDE CULTIVO',
                         'Subgrupo' = 'SUBGRUPO \nDE CULTIVO', 
                         'Year' = 'AÑO',
                         'AreaSembrada' = 'Área Sembrada\n(ha)',
                         'AreaCosechada' = 'Área Sembrada\n(ha)',
                         'Produccion' = 'Producción\n(t)',                                                                 'Rendimiento' =  'Rendimiento\n(t/ha)',   
                         'Sistema' = 'DESAGREGACIÓN REGIONAL Y/O SISTEMA PRODUCTIVO',
                         'Estado' = 'ESTADO FISICO PRODUCCION') -> new_eva
new_eva %>%
  ##filter(Produccion > 0) %>%
  group_by(Grupo) %>%
  summarize(total_produccion = sum(Produccion)) %>% 
  arrange(desc(total_produccion)) 
new_eva %>%
  group_by(Grupo) %>%
  summarize(total_produccion = sum(Produccion)) -> PT 
PT %>% 
  filter(total_produccion > 1000000) -> main.groups
(value = sum(main.groups$total_produccion))
## [1] 52493160
main.groups$percent = main.groups$total_produccion/value 
library(ggplot2)
# Barplot
bp<- ggplot(main.groups, aes(x="", y=percent, fill=Grupo))+
geom_bar(width = 1, stat = "identity")
# Piechart
pie <- bp + coord_polar("y", start=0)
pie

new_eva %>%
  group_by(Grupo, MUNICIPIO) %>%
  summarize(total_prod = sum(Produccion, na.rm = TRUE)) %>%
  slice(which.max(total_prod))  %>%
  arrange(desc(total_prod))
## `summarise()` has grouped output by 'Grupo'. You can override using the
## `.groups` argument.
new_eva %>%
  group_by(Grupo, MUNICIPIO) %>%
  summarize(total_prod = sum(Produccion, na.rm = TRUE)) %>%
  slice(which.max(total_prod))  -> leaders
## `summarise()` has grouped output by 'Grupo'. You can override using the
## `.groups` argument.
leaders %>% 
  filter(total_prod > 50000) -> main.leaders
# Basic barplot
p<-ggplot(data=main.leaders, aes(x=MUNICIPIO, y=total_prod)) +
  geom_bar(stat="identity")
p

grafica de produccion de otros permanentes (2008-2014)

###Bibliography cite this work as Lizarazo, I., 2022. Understanding dynamic productivity of crops. Available at https://rpubs.com/ials2un/production_dyn_v1.

sessionInfo()
## R version 4.1.3 (2022-03-10)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 19044)
## 
## 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] rmarkdown_2.13  knitr_1.38      forcats_0.5.1   stringr_1.4.0  
##  [5] dplyr_1.0.8     purrr_0.3.4     readr_2.1.2     tidyr_1.2.0    
##  [9] tibble_3.1.6    ggplot2_3.3.5   tidyverse_1.3.1
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_1.0.8.3     lubridate_1.8.0  assertthat_0.2.1 digest_0.6.29   
##  [5] utf8_1.2.2       R6_2.5.1         cellranger_1.1.0 backports_1.4.1 
##  [9] reprex_2.0.1     evaluate_0.15    highr_0.9        httr_1.4.2      
## [13] pillar_1.7.0     rlang_1.0.2      readxl_1.3.1     rstudioapi_0.13 
## [17] jquerylib_0.1.4  labeling_0.4.2   bit_4.0.4        munsell_0.5.0   
## [21] broom_0.7.12     compiler_4.1.3   modelr_0.1.8     xfun_0.30       
## [25] pkgconfig_2.0.3  htmltools_0.5.2  tidyselect_1.1.2 fansi_1.0.3     
## [29] crayon_1.5.1     tzdb_0.3.0       dbplyr_2.1.1     withr_2.5.0     
## [33] grid_4.1.3       jsonlite_1.8.0   gtable_0.3.0     lifecycle_1.0.1 
## [37] DBI_1.1.2        magrittr_2.0.2   scales_1.1.1     cli_3.2.0       
## [41] stringi_1.7.6    vroom_1.5.7      farver_2.1.0     fs_1.5.2        
## [45] xml2_1.3.3       bslib_0.3.1      ellipsis_0.3.2   generics_0.1.2  
## [49] vctrs_0.3.8      tools_4.1.3      bit64_4.0.5      glue_1.6.2      
## [53] hms_1.1.1        parallel_4.1.3   fastmap_1.1.0    yaml_2.3.5      
## [57] colorspace_2.0-3 rvest_1.0.2      haven_2.4.3      sass_0.4.1