{"word_count": 254, "character_count": 2074}
Très joli billet sur blog.revolutionanalytics.com avec un code de @kyle_e_walker permettant, très simplement (moyennant une inscription pour avoir une clé permettant d'utiliser l'API du census) de construire une pyramide des âges dynamiques.&#62; devtools::install_github('walkerke/idbr')&#62; library(idbr)&#62; library(ggplot2)&#62; library(animation)&#62; library(dplyr)&#62; library(ggthemes)&#62; idb_api_key("mykey1239F2f324zf9GGZgege32R2ii4")On importe alors les données pour les hommes et les femmes,&#62; male &#60;- idb1('FR', 2010:2050, sex = 'male') %&#62;%+   mutate(POP = POP * -1,+   SEX = 'Male')&#62; female &#60;- idb1('FR', 2010:2050, sex = 'female') %&#62;% mutate(SEX = 'Female')et on stocke le tout&#62; france &#60;- rbind(male, female) %&#62;%+   mutate(abs_pop = abs(POP))Ensuite, on crée l'animation,&#62; saveGIF({+   +   for (i in 2010:2050) {+     +     title &#60;- as.character(i)+     +     year_data &#60;- filter(france, time == i)+     +     g1 &#60;- ggplot(year_data, aes(x = AGE, y = POP, fill = SEX, width = 1)) ++       coord_fixed() + +       coord_flip() ++       annotate('text', x = 98, y = -800000, +       label = 'Data: US Census Bureau IDB; idbr R package', size = 3) + +       geom_bar(data = subset(year_data, SEX == "Female"), stat = "identity") ++       geom_bar(data = subset(year_data, SEX == "Male"), stat = "identity") ++       scale_y_continuous(breaks = seq(-1000000, 1000000, 500000),+       labels = paste0(as.character(c(seq(1, 0, -0.5), c(0.5, 1))), "m"), +       limits = c(min(france$POP), max(france$POP))) ++       theme_economist(base_size = 14) + +       scale_fill_manual(values = c('#ff9896', '#d62728')) + +       ggtitle(paste0('Population structure of France, ', title)) + +       ylab('Population') + +       xlab('Age') + +       theme(legend.position = "bottom", legend.title = element_blank()) + +       guides(fill = guide_legend(reverse = TRUE))+     print(g1) +   }+ }, movie.name = 'france_pyramid.gif', interval = 0.1, ani.width = 700, ani.height = 600)Et le résultat est vraiment joli, non ?