#1.datos multianuales 2007-2018 dinamicas de produccion de platano y cereales en el departamento del meta.

##2.instalacion de libreria

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)
library(ggplot2)

###3.carga de archivos

list.files(".", pattern=c('csv'))
## [1] "co (1).csv"                 "co.csv"                    
## [3] "meta_cereales_2020 (1).csv" "meta_platanos_2020 (1).csv"
(eva = read_csv("C:\\Users\\PERSONAL\\Documents\\GB\\Evaluaciones_Agropecuarias_Municipales_EVA.csv", col_names = TRUE))
## Rows: 5241 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"

####4.limpiar datos

eva %>% dplyr::select('CÓD. MUN.':'ESTADO FISICO PRODUCCION') -> eva.tmp
eva.tmp
eva.tmp %>% dplyr::select('Cod_Mun' = 'CÓD. MUN.',
                         'municipio'= 'MUNICIPIO',
                         'Grupo' = 'GRUPO \nDE CULTIVO',
                         'Subgrupo' = 'SUBGRUPO \nDE CULTIVO',
                         'cultivo' = '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 %>%
  ##filter(Produccion > 0) %>%
  group_by(Grupo) %>%
  summarize(total_produccion = sum(Produccion)) %>% 
  arrange(desc(total_produccion)) 

#####5.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] 27629576
main.groups$percent = main.groups$total_produccion/value

#5.1.diagrama produccion cultivos mas importantes.

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
leaders %>% 
  filter(total_prod > 50000) -> main.leaders

##5.2.Tabla total produccion anual municipios.

# Basic barplot
p<-ggplot(data=main.leaders, aes(x=municipio, y=total_prod)) +
  geom_bar(stat="identity")
p

#5.3.estadistica 2007 a 2018, comportamiento cultivo de platano toneladas anuales en Puerto lopez.

new_eva %>% 
  filter(municipio=="PUERTO LOPEZ" & cultivo=="PLATANO") %>% 
  group_by(Year, cultivo) %>%
  select(municipio, cultivo, Produccion, Year) ->  PUERTOLOPEZ_PLATANO
PUERTOLOPEZ_PLATANO
g <- ggplot(aes(x=Year, y=Produccion/1000), data =PUERTOLOPEZ_PLATANO) + geom_bar(stat='identity') + labs(y='Produccion de PLATANO [Ton x 1000]')
g + ggtitle("Evolution of PLATANO Crop Production in PUERTO LOPEZ from 2007 to 2018") + labs(caption= "Based on EVA data (Minagricultura, 2020)")

https://rpubs.com/gundap0411/892327

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