0
Hi, I need to make an animated chart that shows the top 10 states.
I’m trying, but it’s showing the top 14 and it’s not in the order from the highest to the lowest.
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
}
There are 8 graphs (one for each BOD) that show the amount of BOD per state over the years. The Fus must be in order, from higher to lower.
Keep it up 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 Ufs, 7 different year and 8 different CBO
Please share the
dput(head(SuperRais , 30))
or similar data so that we can reproduce the problem.– Tomás Barcellos
I edited the post with the information.
– RxT
But they are in order.... I don’t know if the order is using the data of all the years, or just the last year. You want something other than that?
– Tomás Barcellos
They are not in order no, see example I posted. Over the years one state goes "passing" the other and the y axis is not changed. It should be organized by the amount of BOD. You can see that at the beginning there are states with quantity 0 on top of states that already have values.
– RxT
The order I would like would be according to the years, as the year passes, the state should update the order. I don’t know if I’m getting clear.
– RxT
Now I understand. As soon as I can I see the question
– Tomás Barcellos
OK, thank you very much !
– RxT
This is what I wanted to do: https://www.youtube.com/watch?v=tjNXULSlFio
– RxT
Your dput is not working...
– Tomás Barcellos