Datos_de_miercoles

#Bibliotecas necesarias
library(tidyverse)
library(ggsci)
library(gganimate)
#Lectura de Datos
menstruaccion <- readr::read_csv("https://raw.githubusercontent.com/cienciadedatos/datos-de-miercoles/master/datos/2019/2019-12-04/menstruaccion.csv")
save(menstruaccion,file = "menstruacion.Rdata") #Guardar datos 
load("menstruacion.Rdata") # Cargando datos 

top.marca <- menstruaccion %>%
  ungroup() %>% 
  group_by(Marca) %>% 
  summarise(count= n()) %>% 
  top_n(10) %>% 
  arrange(-count)

top.marca <- c(top.marca$Marca)
#Seleccion de datos
g <-  menstruaccion %>% 
  filter(Marca %in% top.marca) %>%
  filter(Categoría == "toallitas") %>% 
  mutate_if(is.character,as.factor) %>% 
  mutate(Marca = factor(Marca, levels = top.marca)) %>% 
  ungroup() %>% 
  group_by(Categoría,Marca,Region,Region_label) %>% 
  summarise(count=n()) %>% 
  ungroup() %>% 
  mutate(orden = c(rep(1:6,5),1:4,rep(1:6,3))) %>% 
  ggplot(.,aes(x=reorder(Marca,count), y = count, fill = Marca))+
  geom_bar(stat = "identity") +
  facet_wrap(Region~., scales = "free_x") + 
  scale_fill_jco()+
  coord_flip() +
  labs(title = "Toallitas en Argentina",
       subtitle = "Top 10 de Marcas",
       caption = "Vizualization by @DuvanNievesRui1 | Data: 'Datos canasta gestión menstrual' • Menstruscrapper",
       y = "Conteo",
       x = "Marca")+
  theme(legend.position = "none",
        panel.background = element_rect(fill="grey10"),
        plot.background = element_rect(fill="grey10"),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        strip.background = element_rect(fil="grey30"),
        plot.caption = element_text(size = 11,color = "grey76", hjust = .98),
        plot.title = element_text(hjust = 0.5, size=20, color="grey76"),
        plot.subtitle  = element_text(hjust = 0.5, size=13, color="grey76"),
        axis.text = element_text(family = "Roboto Mono",
                                 size = 8,
                                 colour = "grey76"), #Control de los valores en los ejes
        strip.text.x =element_text(family = "Roboto Mono",
                                   size = 10,
                                   colour = "grey76"), #Control de texto en cada minipanel facet
        axis.title =  element_text(family = "Roboto Mono",
                                   size = 14,
                                   colour = "white"),
        panel.spacing = unit(1, "lines")) +
  transition_states(reorder(count,orden),transition_length = 1,state_length = 1,wrap = FALSE) +
  shadow_mark() 

animate(g, renderer = gifski_renderer(),height = 500, width =700,fps = 4)