Introducción

Este cuaderno de R tiene como finalidad aprender a obtener estadisticas multianuales de cultivos de nuestro departamento, utilizaremos los datos obtenidos del Ministerio de Agricultura y Desarollo Rural, las Evaluaciones Agropecuarias Municipales (EVA) del 2007 al 2018.

Parte Práctica

library(tidyverse)
library(dplyr)
library(readr)
library(ggplot2)
list.files("C:/Users/TEMP/Downloads/GB2-20250527T194536Z-1-001/GB2/Projecto 4", pattern=c('csv'))
## [1] "Evaluaciones_Agropecuarias_Municipales_EVA_20250603.csv"
(eva = read_csv("C:/Users/TEMP/Downloads/GB2-20250527T194536Z-1-001/GB2/Projecto 4/Evaluaciones_Agropecuarias_Municipales_EVA_20250603.csv", col_names = TRUE,
                show_col_types = FALSE))
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"
eva %>% dplyr::select('CÓD. MUN.':'ESTADO FISICO PRODUCCION') -> eva.tmp
eva.tmp
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
new_eva %>%
  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] 8685540
main.groups$percent = main.groups$total_produccion/value
library(ggplot2)
bp<- ggplot(main.groups, aes(x="", y=percent, fill=Grupo))+
geom_bar(width = 1, stat = "identity")
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
leaders %>% 
  filter(total_prod > 50000) -> main.leaders
p<-ggplot(data=main.leaders, aes(x=MUNICIPIO, y=total_prod)) +
  geom_bar(stat="identity")
p

new_eva %>% 
  filter(MUNICIPIO=="CAMPOALEGRE" & CULTIVO=="ARROZ") %>% 
  group_by(Year, CULTIVO) %>%
  select(MUNICIPIO, CULTIVO, Produccion, Year) ->  campoalegre_rice
campoalegre_rice
g <- ggplot(aes(x=Year, y=Produccion/1000), data = campoalegre_rice) + geom_bar(stat='identity') + labs(y='Produccion de Arroz [Ton x 1000]')
g + ggtitle("Evolution of Pineapple Crop Production in Lebrija from 2007 to 2018") + labs(caption= "Based on EVA data (Minagricultura, 2020)")

sessionInfo()
## R version 4.5.0 (2025-04-11 ucrt)
## Platform: x86_64-w64-mingw32/x64
## Running under: Windows 11 x64 (build 26100)
## 
## Matrix products: default
##   LAPACK version 3.12.1
## 
## 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    
## 
## time zone: America/Bogota
## tzcode source: internal
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] lubridate_1.9.4 forcats_1.0.0   stringr_1.5.1   dplyr_1.1.4    
##  [5] purrr_1.0.4     readr_2.1.5     tidyr_1.3.1     tibble_3.2.1   
##  [9] ggplot2_3.5.2   tidyverse_2.0.0
## 
## loaded via a namespace (and not attached):
##  [1] bit_4.6.0          gtable_0.3.6       jsonlite_2.0.0     crayon_1.5.3      
##  [5] compiler_4.5.0     tidyselect_1.2.1   parallel_4.5.0     jquerylib_0.1.4   
##  [9] scales_1.4.0       yaml_2.3.10        fastmap_1.2.0      R6_2.6.1          
## [13] labeling_0.4.3     generics_0.1.4     knitr_1.50         bslib_0.9.0       
## [17] pillar_1.10.2      RColorBrewer_1.1-3 tzdb_0.5.0         rlang_1.1.6       
## [21] cachem_1.1.0       stringi_1.8.7      xfun_0.52          sass_0.4.10       
## [25] bit64_4.6.0-1      timechange_0.3.0   cli_3.6.5          withr_3.0.2       
## [29] magrittr_2.0.3     digest_0.6.37      grid_4.5.0         vroom_1.6.5       
## [33] rstudioapi_0.17.1  hms_1.1.3          lifecycle_1.0.4    vctrs_0.6.5       
## [37] evaluate_1.0.3     glue_1.8.0         farver_2.1.2       rmarkdown_2.29    
## [41] tools_4.5.0        pkgconfig_2.0.3    htmltools_0.5.8.1