I need to do a animated graph which shows top ten UF.
I'm trying, but the graph are showing top 14 and they are not in order.
There will be 8 graphs (one for each CBO) which shows de amount of CBO by UF as time goes by. UF must be in order, from bigger to smaller.
# 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
}
My actual results:
They must be in order and just appear the top ten.
> 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 ano and 8 different CBO
This is what I what I want to do: youtube.com/watch?v=tjNXULSlFio