list.files("C:/Users/Lenovo/Documents/CuadernosR", pattern=c('csv'))
## [1] "co.csv"                                        
## [2] "Evaluaciones_Agropecuarias_Municipales_EVA.csv"
## [3] "narino_platanos_2020.csv"                      
## [4] "narino_tuberculosyplatanos_2020.csv"
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()
(eva = read_csv("C:/Users/Lenovo/Documents/CuadernosR/Evaluaciones_Agropecuarias_Municipales_EVA.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] 13775235
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=="TUQUERRES" & CULTIVO=="PAPA") %>% 
  group_by(Year, CULTIVO) %>%
  select(MUNICIPIO, CULTIVO, Produccion, Year) ->  papa_tuq
papa_tuq
g <- ggplot(aes(x=Year, y=Produccion/1000), data = papa_tuq) + geom_bar(stat='identity') + labs(y='Produccion de Papa [Ton x 1000]')
g + ggtitle("Evolution of Potato Crop Production in Tuquerres from 2007 to 2018") + labs(caption= "Based on EVA data (Minagricultura, 2020)")

library(rsconnect)

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