R-动画图(gganimate)-我可以订购Y轴吗?

时间:2019-04-03 12:44:47

标签: r ggplot2 gganimate

我需要制作一张动画图,显示前十名用友。

我正在尝试,但是该图显示的是前14名,而且顺序不正确。

将有8个图表(每个CBO一个),显示随着时间的流逝用UF减少的CBO量。用友必须有序,从大到小。

# 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
}

我的实际结果:

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" )

但是我有30种不同的超滤,7种不同的ano和8种不同的CBO

这就是我想做的:youtube.com/watch?v=tjNXULSlFio

0 个答案:

没有答案