Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

128 rindas
3.3KB

  1. ---
  2. title: "Lab 07 - Données temporelles et textuelles"
  3. author: "Antoine Neuraz"
  4. date: "22/11/2019"
  5. output:
  6. xaringan::moon_reader:
  7. css: ['default','css/my_style.css']
  8. lib_dir: libs
  9. seal: false
  10. nature:
  11. ratio: '4:3'
  12. countIncrementalSlides: false
  13. self-contained: true
  14. beforeInit: "addons/macros.js"
  15. ---
  16. ```{r setup, include=FALSE}
  17. knitr::opts_chunk$set(echo = TRUE, fig.asp = .7, fig.width = 12)
  18. library(vizoR)
  19. library(ggplot2)
  20. library(gghighlight)
  21. library(dplyr)
  22. library(ggTimeSeries)
  23. ```
  24. ## TODO
  25. #### 1. charger le dataset `us_city_populations` de la librairie `vizoR`
  26. #### 2. tracer un line chart de l'évolution de la population des villes US
  27. #### 3. Mettez en évidence les 5 plus grandes villes (hint: package gghighlight)
  28. [introduction gghighlight](https://cran.r-project.org/web/packages/gghighlight/vignettes/gghighlight.html)
  29. #### 4. Appliquez les principes de design de Tufte
  30. ##### 5. BONUS: affichez le nom des villes directement à la fin de la ligne
  31. #### 6. Réalisez un streamgraph des 5 plus grandes villes US (hint: package ggTimeSeries)
  32. ---
  33. ## TODO 2
  34. #### Trouver une 3e visualization pertinente pour montrer l'évolution de la population des villes US.
  35. ---
  36. ```{r}
  37. data("us_city_populations")
  38. n_cities = 5
  39. # top_cities <-
  40. # us_city_populations %>%
  41. # filter(Rank <= n_cities) %>%
  42. # select(City, State, Region) %>%
  43. # distinct()
  44. #
  45. # to_plot <- filter(us_city_populations, City %in% top_cities$City)
  46. #to_plot <- us_city_populations
  47. last_ranks <- us_city_populations %>%
  48. filter(Year == max(Year)) %>%
  49. mutate(last_rank = Rank) %>%
  50. select(City, last_rank)
  51. to_plot <- left_join(us_city_populations, last_ranks, by= 'City')
  52. right_axis <- to_plot %>%
  53. group_by(City) %>%
  54. top_n(1, Year) %>%
  55. ungroup() %>%
  56. top_n(n_cities, -last_rank)
  57. ends <- right_axis %>%
  58. pull(Population)
  59. labels <- right_axis %>%
  60. pull(City)
  61. ```
  62. ---
  63. class: full
  64. ```{r, echo = FALSE}
  65. ggplot(to_plot, aes(x=Year, y = Population, group = City, color = City)) +
  66. geom_line(size=1) +
  67. #geom_text(data = subset(to_plot, Year == 2010), aes(x=Inf, y = Population, label=City), hjust = 1) +
  68. scale_x_continuous("", expand=c(0,0))+
  69. scale_y_continuous("",
  70. labels=scales::comma_format(big.mark = " "),
  71. sec.axis = sec_axis(~ ., breaks = ends, labels = labels ))+
  72. scale_color_viridis_d()+
  73. theme_elegant_dark()+
  74. theme(legend.position = "none",
  75. plot.margin = unit(c(1,3,1,1), "lines"),
  76. axis.line.y = element_blank(),
  77. axis.line.x = element_blank(),
  78. axis.ticks.x = element_line(),
  79. panel.grid.major.y = element_line(color= 'grey30', size = .2) ) +
  80. gghighlight(max(last_rank) <= n_cities, use_direct_label = FALSE, label_key = City,unhighlighted_colour = "grey20")
  81. ```
  82. ---
  83. class: full
  84. ```{r, echo = FALSE}
  85. library(ggTimeSeries)
  86. to_plot %>% filter(City %in% labels) %>%
  87. ggplot(aes(x = Year, y = Population, group = City, fill = City)) +
  88. scale_y_continuous("", labels = scales::comma_format(big.mark = " "))+
  89. stat_steamgraph() +
  90. theme_elegant_dark() +
  91. scale_fill_viridis_d() +
  92. theme(plot.margin = unit(c(1,3,1,1), "lines"),
  93. axis.line.y = element_blank(),
  94. axis.line.x = element_blank(),
  95. axis.ticks.x = element_line(),
  96. panel.grid.major.y = element_line(color= 'grey30', size = .2) )
  97. ```
  98. ---
  99. ![](bar_race.gif)