Animated Graphics-R

Hello, I need to make an animated chart that shows the first 10 states.

I'm trying, but it's showing the first 14 and it's not in the order from major to minor.

Below is the code used:

# Packages
library(dplyr)
library(ggplot2)
library(gganimate)
library(gifski)

# Agrupando por CBO, UF e Ano
dGrafico <- SuperRais %>% select(CBO.Ocupação.2002,uf,ano) %>% 
            group_by(CBO.Ocupação.2002,uf,ano) %>%
            summarise(qtd=n())


iAno <- 2010
lCbo <- unique(as.character(dGrafico$CBO.Ocupação.2002))
iCbo <- lCbo[1]

# Gerando gráficos por CBO e Ano
for (iCbo in lCbo) {
  dGraficoA <- data.frame()

  for (i in 2010:2016) {
      dGrafico2 <- dGrafico %>% filter(ano == i & CBO.Ocupação.2002 == iCbo) %>% arrange(desc(qtd))
      dGraficoA <- bind_rows(dGrafico2[1:10,],dGraficoA)
  }

  # Ordenando do maior, para o menor  
  dGraficoA <- dGraficoA %>% arrange(desc(qtd))

  # Gerando Gráfico animado 
  g <- ggplot(dGraficoA, aes(x=reorder(uf, qtd), y=qtd)) +
             geom_bar(stat='identity') +
             coord_flip() +
             transition_time(ano) +
             # view_follow(fixed_x = TRUE) +
             shadow_mark() +
             enter_grow()  +
             enter_fade()  +
             labs(title = paste0("Ano: {frame_time} - ",iCbo))
       anim_save(paste0('g_',iCbo,'.gif'),animation = g) # Salvando animacao em .gif
}

Are 8 graphs (one for each CBO) that show the amount of CBO per UF over the years. The UFS must be in order, from largest to smallest.

Follows as it is: https://imgur.com/a/N7nq7Rq

>dput(head(dGrafico, 10))
structure(list(CBO.Ocupação.2002 = structure(c(201L, 201L, 201L, 201L, 201L, 201L, 201L, 201L, 201L, 201L), .Label = c( "842305", "991410", "991415", "991416", "322310", "223162", "223135", "223152", "111235", "223119"), class = "factor"), uf = c("AC", "AC", "AC", "AC", "AC", "AC", "AC", "AL", "AL", "AL"), ano = c(2010L, 2011L, 2012L, 2013L, 2014L, 2015L, 2016L, 2010L, 2011L, 2012L), qtd = c(94L, 96L, 90L, 73L, 75L, 73L, 68L, 16L, 16L, 14L)), row.names = c(NA, -10L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), groups = structure(list(CBO.Ocupação.2002 = structure(c(201L, 201L), .Label = c( "842230", "848410", "848415", "848420", "848605", "861120", "910120", "911125", "911130", "911135" )

But I have 30 different UF, 7 different year and 8 different CBO

Author: RxT, 2019-04-02