--- title: "Lab 07 - Données temporelles et textuelles" author: "Antoine Neuraz" date: "22/11/2019" output: xaringan::moon_reader: css: ['default','css/my_style.css'] lib_dir: libs seal: false nature: ratio: '4:3' countIncrementalSlides: false self-contained: true beforeInit: "addons/macros.js" --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE, fig.asp = .7, fig.width = 12) library(vizoR) library(ggplot2) library(gghighlight) library(dplyr) library(ggTimeSeries) ``` ## TODO #### 1. charger le dataset `us_city_populations` de la librairie `vizoR` #### 2. tracer un line chart de l'évolution de la population des villes US #### 3. Mettez en évidence les 5 plus grandes villes (hint: package gghighlight) [introduction gghighlight](https://cran.r-project.org/web/packages/gghighlight/vignettes/gghighlight.html) #### 4. Appliquez les principes de design de Tufte ##### 5. BONUS: affichez le nom des villes directement à la fin de la ligne #### 6. Réalisez un streamgraph des 5 plus grandes villes US (hint: package ggTimeSeries) --- ## TODO 2 #### Trouver une 3e visualization pertinente pour montrer l'évolution de la population des villes US. --- ```{r} data("us_city_populations") n_cities = 5 # top_cities <- # us_city_populations %>% # filter(Rank <= n_cities) %>% # select(City, State, Region) %>% # distinct() # # to_plot <- filter(us_city_populations, City %in% top_cities$City) #to_plot <- us_city_populations last_ranks <- us_city_populations %>% filter(Year == max(Year)) %>% mutate(last_rank = Rank) %>% select(City, last_rank) to_plot <- left_join(us_city_populations, last_ranks, by= 'City') right_axis <- to_plot %>% group_by(City) %>% top_n(1, Year) %>% ungroup() %>% top_n(n_cities, -last_rank) ends <- right_axis %>% pull(Population) labels <- right_axis %>% pull(City) ``` --- class: full ```{r, echo = FALSE} ggplot(to_plot, aes(x=Year, y = Population, group = City, color = City)) + geom_line(size=1) + #geom_text(data = subset(to_plot, Year == 2010), aes(x=Inf, y = Population, label=City), hjust = 1) + scale_x_continuous("", expand=c(0,0))+ scale_y_continuous("", labels=scales::comma_format(big.mark = " "), sec.axis = sec_axis(~ ., breaks = ends, labels = labels ))+ scale_color_viridis_d()+ theme_elegant_dark()+ theme(legend.position = "none", plot.margin = unit(c(1,3,1,1), "lines"), axis.line.y = element_blank(), axis.line.x = element_blank(), axis.ticks.x = element_line(), panel.grid.major.y = element_line(color= 'grey30', size = .2) ) + gghighlight(max(last_rank) <= n_cities, use_direct_label = FALSE, label_key = City,unhighlighted_colour = "grey20") ``` --- class: full ```{r, echo = FALSE} library(ggTimeSeries) to_plot %>% filter(City %in% labels) %>% ggplot(aes(x = Year, y = Population, group = City, fill = City)) + scale_y_continuous("", labels = scales::comma_format(big.mark = " "))+ stat_steamgraph() + theme_elegant_dark() + scale_fill_viridis_d() + theme(plot.margin = unit(c(1,3,1,1), "lines"), axis.line.y = element_blank(), axis.line.x = element_blank(), axis.ticks.x = element_line(), panel.grid.major.y = element_line(color= 'grey30', size = .2) ) ``` --- ![](bar_race.gif)