Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

121 lignes
3.0KB

  1. ---
  2. title: "Make a bar chart race of the biggest cities in France"
  3. output: rmarkdown::html_vignette
  4. vignette: >
  5. %\VignetteIndexEntry{bar-chart-race}
  6. %\VignetteEngine{knitr::rmarkdown}
  7. %\VignetteEncoding{UTF-8}
  8. ---
  9. ```{r, include = FALSE}
  10. knitr::opts_chunk$set(
  11. collapse = TRUE,
  12. comment = "#>"
  13. )
  14. ```
  15. ```{r setup}
  16. library(vizoR)
  17. library(dplyr, warn.conflicts = FALSE)
  18. library(ggplot2)
  19. library(hrbrthemes)
  20. library(gganimate)
  21. ```
  22. Load data
  23. ```{r load_data}
  24. # load data
  25. data("base_pop")
  26. # create the ranks based on census populations
  27. base_pop<- base_pop %>%
  28. group_by( annee) %>%
  29. arrange(desc(population)) %>%
  30. mutate(rang = 1:n()) %>%
  31. ungroup()
  32. ```
  33. Extract the list of top n cities
  34. ```{r extract_top_n}
  35. n_cities = 10
  36. top_cities <- base_pop %>% filter(rang <= n_cities) %>%
  37. select(ville, dep, region) %>% distinct()
  38. ```
  39. Create all missing dates
  40. ```{r, combine_dates}
  41. # create a data frame with all the years between min and max Year
  42. all_years <- data.frame(annee = seq(min(base_pop$annee), max(base_pop$annee), 1))
  43. # combine top_cities and all_years
  44. all_combos <- merge(top_cities, all_years, all = T)
  45. # combine all_combos with the original dataset
  46. res_interp <- merge(base_pop, all_combos, all.y = T)
  47. ```
  48. Interpolate the populations when missing (linear interpolation here)
  49. ```{r, interpolate}
  50. res_interp <- res_interp %>%
  51. group_by(ville) %>%
  52. mutate(population=approx(annee,population,annee)$y)
  53. ```
  54. ```{r, filter_for_plot}
  55. # filter the top ten cities per year
  56. to_plot <- res_interp %>%
  57. group_by(annee) %>%
  58. arrange(-population) %>%
  59. mutate(rang=row_number()) %>%
  60. filter(rang<=n_cities) %>%
  61. rename(`Région` = region)
  62. ```
  63. Make the plot
  64. ```{r, make_plot}
  65. p <- to_plot %>%
  66. ggplot(aes(x = -rang,y = population, group =ville)) +
  67. geom_tile(aes(y = population / 2, height = population, fill = `Région`), width = 0.9) +
  68. geom_text(aes(label = ville), hjust = "right", colour = "white", fontface="bold", nudge_y = -10000) +
  69. geom_text(aes(label = scales::comma(population,big.mark = ' ')), hjust = "left", nudge_y = 10000, colour = "grey90") +
  70. coord_flip(clip="off") +
  71. hrbrthemes::scale_fill_ipsum() +
  72. scale_x_discrete("") +
  73. scale_y_continuous("",labels=scales::comma_format(big.mark = " ")) +
  74. theme_elegant_dark(base_size = 20) +
  75. theme(
  76. panel.grid.minor.x=element_blank(),
  77. axis.line = element_blank(),
  78. panel.grid.major= element_line(color='lightgrey', size=.2),
  79. legend.position = c(0.6, 0.2),
  80. plot.margin = margin(1,1,1,2,"cm"),
  81. plot.title = element_text(hjust = 0),
  82. axis.text.y=element_blank(),
  83. legend.text = element_text(size = 15),
  84. legend.background = element_blank()) +
  85. # gganimate code to transition by year:
  86. transition_time(annee) +
  87. ease_aes('cubic-in-out') +
  88. labs(title='Evolution des plus grandes villes de France',
  89. subtitle='Population en {round(frame_time,0)}',
  90. caption='Source: INSEE Base populations historiques 1876-2015')
  91. ```
  92. ```{r, animate}
  93. animate(p, nframes = 350, fps = 25, end_pause = 30, width = 1200, height = 1200, start_pause = 15 )
  94. ```