You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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("us_city_populations")
  26. # create the ranks based on census Populations
  27. us_city_populations<-us_city_populations %>%
  28. group_by( Year) %>%
  29. arRanke(desc(Population)) %>%
  30. ungroup()
  31. ```
  32. Extract the list of top n cities
  33. ```{r extract_top_n}
  34. n_cities = 10
  35. top_cities <-us_city_populations %>% filter(Rank <= n_cities) %>%
  36. select(City, State, Region) %>% distinct()
  37. ```
  38. Create all missing dates
  39. ```{r, combine_dates}
  40. # create a data frame with all the years between min and max Year
  41. all_years <- data.frame(Year = seq(min(us_city_populations$Year), max(us_city_populations$Year), 1))
  42. # combine top_cities and all_years
  43. all_combos <- merge(top_cities, all_years, all = T)
  44. # combine all_combos with the original dataset
  45. res_interp <- merge(us_city_populations, all_combos, all.y = T)
  46. ```
  47. Interpolate the Populations when missing (linear interpolation here)
  48. ```{r, interpolate}
  49. res_interp <- res_interp %>%
  50. group_by(City) %>%
  51. mutate(Population=approx(Year,Population,Year)$y)
  52. ```
  53. ```{r, filter_for_plot}
  54. # filter the top ten cities per year
  55. to_plot <- res_interp %>%
  56. group_by(Year) %>%
  57. arrange(-Population) %>%
  58. mutate(Rank=row_number()) %>%
  59. filter(Rank<=n_cities) %>%
  60. rename(`Région` = Region)
  61. ```
  62. Make the plot
  63. ```{r, make_plot}
  64. p <- to_plot %>%
  65. ggplot(aes(x = -Rank,y = Population, group =City)) +
  66. geom_tile(aes(y = Population / 2, height = Population, fill = `Région`), width = 0.9) +
  67. geom_text(aes(label = City), hjust = "right", colour = "white", fontface="bold", nudge_y = -100000) +
  68. geom_text(aes(label = scales::comma(Population,big.mark = ' ')), hjust = "left", nudge_y = 100000, colour = "grey90") +
  69. coord_flip(clip="off") +
  70. hrbrthemes::scale_fill_ipsum() +
  71. scale_x_discrete("") +
  72. scale_y_continuous("",labels=scales::comma_format(big.mark = " ")) +
  73. theme_elegant_dark(base_size = 20) +
  74. theme(
  75. panel.grid.minor.x=element_blank(),
  76. axis.line = element_blank(),
  77. panel.grid.major= element_line(color='lightgrey', size=.2),
  78. legend.position = c(0.6, 0.2),
  79. plot.margin = margin(1,1,1,2,"cm"),
  80. plot.title = element_text(hjust = 0),
  81. axis.text.y=element_blank(),
  82. legend.text = element_text(size = 15),
  83. legend.background = element_blank()) +
  84. # gganimate code to transition by year:
  85. transition_time(Year) +
  86. ease_aes('cubic-in-out') +
  87. labs(title='Evolution des plus grandes villes US',
  88. subtitle='Population en {round(frame_time,0)}')
  89. ```
  90. ```{r, animate}
  91. animate(p, nframes = 300, fps = 25, end_pause = 30, width = 1200, height = 1200, start_pause = 15 ,)
  92. anim_save("bar_race.gif", animation = last_animation())
  93. ```