library(openxlsx)
url3<-"https://docs.google.com/spreadsheets/d/e/2PACX-1vS2hfNsPDjmYX0SHVR-4lpa-HbDQyGNXXEPFkb2OsleOujLClyOuVOwPOKC0K0tJAo-ECZw2yFnomUk/pub?output=xlsx"
df10 = read.xlsx(url3)
head(df10,8)
##   FECHA_REGISTRO MID_MONEDERO   PLACA RECURSO_PROPIO      TERRITORIO NOMBRE_DIA
## 1       44440.64       M-1860 NNN4822              0        BALANCAN      DIA.1
## 2       44440.72       M-4955 CY14422              0       TAPACHULA      DIA.1
## 3       44447.54       M-4542 NTF3747           2903        OCOSINGO      DIA.1
## 4       44440.74       M-4872 CY14412              0       TAPACHULA      DIA.1
## 5       44440.74       M-3015 NXA8650              0 SAN LUIS POTOSI      DIA.1
## 6       44440.78       M-2991 NXA8312              0 SAN LUIS POTOSI      DIA.1
## 7       44440.78       M-3866 NNN3919              0         CORDOBA      DIA.1
## 8       44440.79       M-4914 CY12994              0       TAPACHULA      DIA.1
##          PERIODO               MARCA TOTAL_ACTIVIDADES_DIA ACTIVIDAD
## 1 a- agosto 2021         FORD_RANGER                     1       A 1
## 2 a- agosto 2021        NISSAN-NP300                     1       A 1
## 3 a- agosto 2021      DODGE_RAM_1500                     2       A 1
## 4 a- agosto 2021        NISSAN-NP300                     1       A 1
## 5 a- agosto 2021 CHEVROLET_SILVERADO                     1       A 1
## 6 a- agosto 2021 CHEVROLET_SILVERADO                     1       A 1
## 7 a- agosto 2021        TOYOTA-HILUX                     1       A 1
## 8 a- agosto 2021        TOYOTA-HILUX                     1       A 1
##     ACTIVIDADES_SEPARADAS
## 1 RECEPCION DE DOCUMENTOS
## 2           SIN ACTIVIDAD
## 3           SIN ACTIVIDAD
## 4           SIN ACTIVIDAD
## 5           SIN ACTIVIDAD
## 6           SIN ACTIVIDAD
## 7        VISITA A VIVEROS
## 8           SIN ACTIVIDAD
dFiltrado<-df10[df10$TERRITORIO=="ACAYUCAN",]

colores<-c("#1373BF","#00A79D","#8CC731","#FEB02F","#F45918","#00AFBB","#E7B800","#FC4E07","red","green3","purple","pink","gray","black")

queSale<-table(dFiltrado$ACTIVIDADES_SEPARADAS,dFiltrado$PERIODO)
grafico1<-barplot(queSale,
         col=colores,#colores
         legend.text = rownames(queSale), #leyenda
         beside = TRUE, #para que los ponga en diferentes lugares
         # ylim=c(0,300), #cambiar el limite
         # xlab="Género", #etiqueta x
         # ylab="Pasajeros", #etiqueta en Y
         # main="pasajeros x clase vivieron " #titulo

        )

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.1     v dplyr   1.0.5
## v tidyr   1.1.4     v stringr 1.4.0
## v readr   1.4.0     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
df10 %>%
  select(TERRITORIO,PERIODO,ACTIVIDADES_SEPARADAS)%>%
  filter(TERRITORIO=="ACAYUCAN")%>%
  
  ggplot(aes(ACTIVIDADES_SEPARADAS))+
  geom_bar(aes(y = (..count..)/sum(..count..)*100),fill="pink2")+
  
  geom_text(aes(y = ..count.. / sum(..count..),
                label = paste0(round(..count.. / sum(..count..)* 100, 0),"%"),
                ),
            stat="count")

#https://www.youtube.com/watch?v=aJBiXcjQZiA&ab_channel=JORGEESTADISTICA


#ggplot(df10,aes(x=PERIODO))+geom_bar() #gráfico simple
#ggplot(df10,aes(x=PERIODO))+geom_bar(fill="red3") #gráfico gráfico coloreado
#ggplot(df10,aes(x=PERIODO))+geom_bar(aes(fill=ACTIVIDADES_SEPARADAS)) #gráfico gráfico coloreado actividades


#ggplot(df10,aes(x=PERIODO))+geom_bar(aes(fill=ACTIVIDADES_SEPARADAS))+theme_classic() #cambiar tema

 # ggplot(df10,aes(x=PERIODO))+ #grafico
 #   geom_bar(aes(fill=ACTIVIDADES_SEPARADAS))+ #colores por actividad
 #   theme_classic()+ #fondo blanco
 #   theme(legend.position="bottom") #posicion de la leyenda

ggplot(df10,aes(x=PERIODO))+
  geom_bar(aes(fill=ACTIVIDADES_SEPARADAS),position = "dodge")+ #separar las lineas
  theme_classic()+ #fondo blanco
  theme(legend.position="bottom")

ggplot(df10,aes(x=PERIODO))+
  geom_bar(aes(fill=ACTIVIDADES_SEPARADAS),position = "dodge")+
  theme(legend.position="bottom")+ coord_polar()+
  facet_grid(.~PERIODO)

#ggplot(df10,aes(x=ACTIVIDADES_SEPARADAS))+geom_bar()

#ggplot(df10,aes(x=ACTIVIDADES_SEPARADAS))+geom_bar(aes(fill=ACTIVIDADES_SEPARADAS))

 # ggplot(df10,aes(x=ACTIVIDADES_SEPARADAS))+geom_bar(aes(fill=ACTIVIDADES_SEPARADAS))+
 #   facet_wrap(.~PERIODO,5)


# ggplot(df10,aes(x=ACTIVIDADES_SEPARADAS))+geom_bar(aes(fill=ACTIVIDADES_SEPARADAS))+
#   facet_wrap(.~PERIODO,5)+
#    geom_text(stat='count', aes(label=..count..), vjust=-1)


ggplot(df10,aes(x=ACTIVIDADES_SEPARADAS))+geom_bar(aes(fill=ACTIVIDADES_SEPARADAS))+
  facet_wrap(.~PERIODO,5)+
  geom_text(stat='count', aes(label=..count..), vjust=0)+
  ylim(0, 5000)

enporcentaje <- prop.table(table(df10$ACTIVIDADES_SEPARADAS))*100

enporcentaje <- round(enporcentaje,2)

enporcentaje
## 
##   ASISTENCIA A TECNICOS           DOCUMENTACION        OTRAS COMISIONES 
##                    9.43                    0.04                   17.58 
## RECEPCION DE DOCUMENTOS            REUNION CACS           SIN ACTIVIDAD 
##                    6.59                    6.11                   10.70 
##  SUPERVISION BIOFABRICA TALLERES Y CAPACITACION      TRABAJO DE OFICINA 
##                    2.97                    7.23                   13.79 
##           VISITA A CACS       VISITA A PARCELAS        VISITA A VIVEROS 
##                    8.53                    6.82                   10.20
enporcentaje <- as.data.frame(enporcentaje)
enporcentaje
##                       Var1  Freq
## 1    ASISTENCIA A TECNICOS  9.43
## 2            DOCUMENTACION  0.04
## 3         OTRAS COMISIONES 17.58
## 4  RECEPCION DE DOCUMENTOS  6.59
## 5             REUNION CACS  6.11
## 6            SIN ACTIVIDAD 10.70
## 7   SUPERVISION BIOFABRICA  2.97
## 8  TALLERES Y CAPACITACION  7.23
## 9       TRABAJO DE OFICINA 13.79
## 10           VISITA A CACS  8.53
## 11       VISITA A PARCELAS  6.82
## 12        VISITA A VIVEROS 10.20
colnames(enporcentaje) <- c("actividad","porcentaje")
enporcentaje
##                  actividad porcentaje
## 1    ASISTENCIA A TECNICOS       9.43
## 2            DOCUMENTACION       0.04
## 3         OTRAS COMISIONES      17.58
## 4  RECEPCION DE DOCUMENTOS       6.59
## 5             REUNION CACS       6.11
## 6            SIN ACTIVIDAD      10.70
## 7   SUPERVISION BIOFABRICA       2.97
## 8  TALLERES Y CAPACITACION       7.23
## 9       TRABAJO DE OFICINA      13.79
## 10           VISITA A CACS       8.53
## 11       VISITA A PARCELAS       6.82
## 12        VISITA A VIVEROS      10.20
ggplot(enporcentaje,aes(x=actividad,y=porcentaje))+geom_bar(stat="identity",aes(fill=actividad))

  # facet_wrap(.~PERIODO,5)+
  # geom_text(stat='count', aes(label=..count..), vjust=0)+
  # ylim(0, 5000)
library(gganimate)
library(gifski)
library(av)

#Me va a servir para shiny



df10 %>%
  select(TERRITORIO,PERIODO,ACTIVIDADES_SEPARADAS)%>%
  arrange(PERIODO)%>%
  filter(TERRITORIO=="ACAYUCAN")%>%


  ggplot(aes(x=ACTIVIDADES_SEPARADAS))+
  geom_bar(aes(fill=ACTIVIDADES_SEPARADAS))+
  coord_flip()+ #poner en horizontaL
  ylim(0, 600)+
  #theme_classic()+ #QUITAR EL FONDO GRIS
  theme(legend.position="botto")+ # posicion de la leyenda
  geom_text(stat='count', aes(label=..count..), hjust=-1)+ #NUMEROS
  
  labs(title = "{closest_state}")+
  theme(plot.title = element_text(size=25))+ #tamanio titulo

  transition_states(PERIODO,state_length = 5, transition_length = 2)

***Me va a servir para shiny

df10 %>%
  select(TERRITORIO,PERIODO,ACTIVIDADES_SEPARADAS)%>%
  arrange(PERIODO)%>%
  filter(TERRITORIO=="TAPACHULA")%>%


  ggplot(aes(x=ACTIVIDADES_SEPARADAS))+
  geom_bar(aes(fill=ACTIVIDADES_SEPARADAS))+
  coord_flip()+ #poner en horizontaL
  #ylim(0, 350)+
  theme_bw()+ #QUITAR EL FONDO GRIS
  theme(legend.position="botto")+ # posicion de la leyenda
  geom_text(stat='count', aes(label=..count..), hjust=0)+#NUMEROS
  labs(title = "{closest_state}")+ #titulo conforme cambia teritorio
  theme(plot.title = element_text(size=22))+ #tamanio titulo
  coord_polar()+


  transition_states(PERIODO,state_length = 5, transition_length = 2)
## Coordinate system already present. Adding new coordinate system, which will replace the existing one.

df10 %>%
  select(TERRITORIO,PERIODO,ACTIVIDADES_SEPARADAS)%>%
  arrange(PERIODO)%>%
  filter(TERRITORIO=="TAPACHULA"| TERRITORIO=="ACAYUCAN"
         |TERRITORIO=="BALANCAN"| TERRITORIO=="OCOSINGO")%>%


  ggplot(aes(x=ACTIVIDADES_SEPARADAS))+
  geom_bar(aes(fill=ACTIVIDADES_SEPARADAS))+
  coord_flip()+ #poner en horizontaL
  #ylim(0, 350)+
  theme_bw()+ #QUITAR EL FONDO GRIS
  theme(legend.position="botto")+ # posicion de la leyenda
  geom_text(stat='count', aes(label=..count..), hjust=0)+#NUMEROS
  labs(title = "{closest_state}")+ #titulo conforme cambia teritorio
  theme(plot.title = element_text(size=22))+ #tamanio titulo
  #coord_polar()+
  facet_wrap(.~TERRITORIO,nrow = 2)+
  coord_polar()+

  transition_states(PERIODO,state_length = 5, transition_length = 2)
## Coordinate system already present. Adding new coordinate system, which will replace the existing one.

g110 <- df10 %>%
          select(TERRITORIO,PERIODO,ACTIVIDADES_SEPARADAS)%>%
          arrange(PERIODO)%>%
          filter(TERRITORIO=="TAPACHULA"| TERRITORIO=="ACAYUCAN"
                 |TERRITORIO=="BALANCAN"| TERRITORIO=="OCOSINGO")%>%

          ggplot(aes(x=ACTIVIDADES_SEPARADAS))+
          geom_bar(aes(fill=ACTIVIDADES_SEPARADAS))+
          coord_flip()+ #poner en horizontaL
          #ylim(0, 350)+
          theme_bw()+ #QUITAR EL FONDO GRIS
          theme(legend.position="botto")+ # posicion de la leyenda
          geom_text(stat='count', aes(label=..count..), hjust=0)+#NUMEROS
          labs(title = "{closest_state}")+ #titulo conforme cambia teritorio
          theme(plot.title = element_text(size=22))+ #tamanio titulo
  
          theme(axis.text =element_text(color="blue", size=7, face="bold",angle = 45),)+
  
          coord_polar()+ #en circulo
          facet_wrap(.~TERRITORIO,nrow = 2)+ #dividido por territorio

          transition_states(PERIODO,state_length = 5, transition_length = 2)
## Coordinate system already present. Adding new coordinate system, which will replace the existing one.
animate(g110,height = 750, width = 1000)

g2 <- ggplot(df10,aes(x=ACTIVIDADES_SEPARADAS))+geom_bar(aes(fill=ACTIVIDADES_SEPARADAS))+
  facet_wrap(.~PERIODO,5)+
  geom_text(stat='count', aes(label=..count..), hjust=-1)+
  theme(legend.position="rigth")+
  coord_flip()

g2

g3 <- ggplot(df10,aes(x=ACTIVIDADES_SEPARADAS))+geom_bar(aes(fill=ACTIVIDADES_SEPARADAS))+
  facet_wrap(.~PERIODO,5)+
  geom_text(stat='count', aes(label=..count..), hjust=-1)+
  theme(legend.position="rigth")+
  coord_flip()+
  transition_states(TERRITORIO)+
  geom_text(aes(x = 10, y = 300, label = TERRITORIO))

g3

g21 <- ggplot(df10,aes(x=ACTIVIDADES_SEPARADAS))+geom_bar(aes(fill=ACTIVIDADES_SEPARADAS))+
  facet_wrap(.~PERIODO,5)+
  geom_text(stat='count', aes(label=..count..), vjust=-1)+
  theme(legend.position="rigth")



g21

 g110 <- df10 %>%
          select(TERRITORIO,PERIODO,ACTIVIDADES_SEPARADAS)%>%
          arrange(PERIODO)%>%
          # filter(TERRITORIO=="TAPACHULA"| TERRITORIO=="ACAYUCAN"
          #         |TERRITORIO=="BALANCAN"| TERRITORIO=="OCOSINGO")%>%
        
        
          ggplot(aes(x=ACTIVIDADES_SEPARADAS))+
          geom_bar(aes(fill=ACTIVIDADES_SEPARADAS))+
          #coord_flip()+ #poner en horizontaL
          #ylim(0, 350)+ #limites eje y
  theme_bw()+#QUITAR EL FONDO GRIS

          theme(legend.position="botto")+ # posicion de la leyenda
          geom_text(stat='count', aes(label=..count..), vjust=0)+#NUMEROS
           
  
  theme(axis.text.x = element_text(angle = 90, vjust =0, hjust=1))+
          
  theme(plot.title = element_text(size=22))+ #tamanio titulo
          
  #coord_polar()+ #tipo radial grafico
           
  facet_wrap(.~PERIODO,nrow =  5)+ #divicion 5 filas
  labs(title = "{closest_state}")+ #titulo conforme cambia teritorio

        
          #transition_states(PERIODO,state_length = 5, transition_length = 2)+
  transition_states(TERRITORIO,state_length = 10, transition_length = 2)

animate(g110,height = 750, width = 1000, duration = 465)

 g111 <- df10 %>%
          select(TERRITORIO,PERIODO,ACTIVIDADES_SEPARADAS)%>%
          arrange(PERIODO)%>%
          # filter(TERRITORIO=="TAPACHULA"| TERRITORIO=="ACAYUCAN"
          #         |TERRITORIO=="BALANCAN"| TERRITORIO=="OCOSINGO")%>%
        
        
          ggplot(aes(x=ACTIVIDADES_SEPARADAS))+
          geom_bar(aes(fill=ACTIVIDADES_SEPARADAS))+
          #coord_flip()+ #poner en horizontaL
          #ylim(0, 350)+ #limites eje y
  theme_bw()+#QUITAR EL FONDO GRIS

          theme(legend.position="botto")+ # posicion de la leyenda
          geom_text(stat='count', aes(label=..count..), vjust=0)+#NUMEROS
           
  
  theme(axis.text.x = element_text(angle = 45, vjust =0, hjust=1))+
          
  theme(plot.title = element_text(size=22))+ #tamanio titulo
          
  coord_polar()+ #tipo radial grafico
           
  facet_wrap(.~PERIODO,nrow =  2)+ #divicion 5 filas
  labs(title = "{closest_state}")+ #titulo conforme cambia teritorio

        
          #transition_states(PERIODO,state_length = 5, transition_length = 2)+
  transition_states(TERRITORIO,state_length = 10, transition_length = 2)

animate(g111,height = 750, width = 1000, duration = 310)

 g112 <- df10 %>%
          select(TERRITORIO,PERIODO,ACTIVIDADES_SEPARADAS)%>%
          arrange(PERIODO)%>%
          # filter(TERRITORIO=="TAPACHULA"| TERRITORIO=="ACAYUCAN"
          #         |TERRITORIO=="BALANCAN"| TERRITORIO=="OCOSINGO")%>%
        
        
          ggplot(aes(x=ACTIVIDADES_SEPARADAS))+
  
          geom_bar(aes(fill=ACTIVIDADES_SEPARADAS))+
          #coord_flip()+ #poner en horizontaL
          #ylim(0, 350)+ #limites eje y
  theme_bw()+#QUITAR EL FONDO GRIS

          theme(legend.position="botto")+ # posicion de la leyenda
          geom_text(stat='count', aes(label=..count..), vjust=0)+#NUMEROS
           
  
  theme(axis.text.x = element_text(angle = 45,size=7, vjust =0, hjust=1))+
          
  theme(plot.title = element_text(size=22))+ #tamanio titulo
          
  coord_polar()+ #tipo radial grafico
           
  facet_wrap(.~PERIODO,nrow =  2)+ #divicion 5 filas
  labs(title = "{closest_state}")+ #titulo conforme cambia teritorio

        
          #transition_states(PERIODO,state_length = 5, transition_length = 2)+
  transition_states(TERRITORIO,state_length = 10, transition_length = 2)

animate(g112,height = 750, width = 1000, duration = 310)

ggplot(df10,aes(x=PERIODO))+
  geom_bar(aes(fill=ACTIVIDADES_SEPARADAS),position = "dodge")+
  theme(legend.position="bottom")+
  transition_states(TERRITORIO,transition_length = 20,state_length = 4)