diff --git a/courses/01-dataviz-intro.pdf b/courses/01-dataviz-intro.pdf index e35018e..2ee8c35 100644 Binary files a/courses/01-dataviz-intro.pdf and b/courses/01-dataviz-intro.pdf differ diff --git a/courses/02-perception-couleurs.pdf b/courses/02-perception-couleurs.pdf new file mode 100644 index 0000000..61a31db Binary files /dev/null and b/courses/02-perception-couleurs.pdf differ diff --git a/courses/03-design-tabulaire.pdf b/courses/03-design-tabulaire.pdf new file mode 100644 index 0000000..1e548f8 Binary files /dev/null and b/courses/03-design-tabulaire.pdf differ diff --git a/courses/04-interactivite.pdf b/courses/04-interactivite.pdf new file mode 100644 index 0000000..0098081 Binary files /dev/null and b/courses/04-interactivite.pdf differ diff --git a/courses/06_graphes.pdf b/courses/06_graphes.pdf new file mode 100644 index 0000000..f1a8d8c Binary files /dev/null and b/courses/06_graphes.pdf differ diff --git a/courses/07_time_text.pdf b/courses/07_time_text.pdf new file mode 100644 index 0000000..797aea0 Binary files /dev/null and b/courses/07_time_text.pdf differ diff --git a/courses/bar_race.gif b/courses/bar_race.gif index ec31562..0d4c042 100644 Binary files a/courses/bar_race.gif and b/courses/bar_race.gif differ diff --git a/courses/lab01-correction.Rmd b/courses/lab01-correction.Rmd index e28aa5a..baf2423 100644 --- a/courses/lab01-correction.Rmd +++ b/courses/lab01-correction.Rmd @@ -7,6 +7,7 @@ output: html_document ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) +library(ggplot2) ``` ## Ouvrir le dataset "mtcars" diff --git a/courses/lab01-correction.html b/courses/lab01-correction.html new file mode 100644 index 0000000..2750729 --- /dev/null +++ b/courses/lab01-correction.html @@ -0,0 +1,502 @@ + + + + + + + + + + + + + + + +correction + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + +
+

Ouvrir le dataset “mtcars”

+
+
+

représenter le “Gross horsepower” en fonction du nombre de cylindres

+
data("mtcars")
+
+ggplot(data = mtcars, 
+       aes(x = as.factor(cyl),
+           y = hp)) + 
+  geom_jitter(width = .2)
+

+
+
+

utiliser l’encodage multiple sur le nombre de cylindres

+
ggplot(data = mtcars, 
+       aes(x = as.factor(cyl),
+           y = hp, 
+           size = cyl,
+           color = cyl)) + 
+  geom_jitter(width = .2, alpha = .6) +
+  theme_minimal() +
+  theme(legend.position = "none")
+

+
+
+

ajouter l’information du nombre de carburateurs

+
ggplot(data = mtcars, 
+       aes(x = as.factor(cyl),
+           y = carb, 
+           size = hp,
+           color = hp)) + 
+  geom_jitter(width = .2, alpha = .6) +
+  theme_minimal() 
+

+
  #facet_grid(~as.factor(carb))
+  #theme(legend.position = "none")
+
+
+

Paufiner le plot (axes, titres, thème)

+
ggplot(data = mtcars, 
+       aes(x = as.factor(cyl),
+           y = carb, 
+           size = hp,
+           color = hp)) + 
+  geom_jitter(width = .2, alpha = .6) +
+  theme_minimal() + 
+  labs(x = "Cylinders", 
+       y = "Carburators")
+

+
+
+

représenter la distribution du nombre de miles per gallon en histogramme

+
ggplot(mtcars, 
+       aes(x= mpg)) + 
+  geom_histogram(bins = sqrt(nrow(mtcars)))
+

+
+
+

représenter la distribution du nombre de miles per gallon en boxplot

+
ggplot(mtcars, 
+       aes(x= 1, y= mpg)) + 
+  geom_boxplot()
+

+
+
+

representer la distribution du nombre de miles per gallon en fonction du nombre de cylindres

+
ggplot(mtcars, 
+       aes(x= as.factor(cyl), y= mpg)) + 
+  geom_violin(fill = "grey70")
+

+
+
+

ajouter les points par dessus la distribution

+
ggplot(mtcars, 
+       aes(x= as.factor(cyl), y= mpg)) + 
+  geom_violin(fill = "grey70") + 
+  geom_jitter(aes(color = cyl),width = .15) 
+

+
+
+

paufiner le plot (axes, titres, thème)

+
+ + + + +
+ + + + + + + + + + + + + + + diff --git a/courses/lab01-ggplot-intro.Rmd b/courses/lab01-ggplot-intro.Rmd index bba7e18..1b228ba 100644 --- a/courses/lab01-ggplot-intro.Rmd +++ b/courses/lab01-ggplot-intro.Rmd @@ -13,9 +13,6 @@ output: self-contained: true beforeInit: "addons/macros.js" highlightLines: true - pdf_document: - seal: false - --- ```{r setup, include=FALSE} diff --git a/courses/lab01-ggplot-intro.pdf b/courses/lab01-ggplot-intro.pdf index 0b7fbff..38a970b 100644 Binary files a/courses/lab01-ggplot-intro.pdf and b/courses/lab01-ggplot-intro.pdf differ diff --git a/courses/lab02-perception-colors.Rmd b/courses/lab02-perception-colors.Rmd index 9407271..a620a6b 100644 --- a/courses/lab02-perception-colors.Rmd +++ b/courses/lab02-perception-colors.Rmd @@ -25,6 +25,18 @@ library(see) library(RColorBrewer) ``` +class: center, middle, title + +# Lab 2: Perception et couleurs + +### 2019-2020 + +## Dr. Antoine Neuraz + +### AHU Informatique médicale +#### Hôpital Necker-Enfants malades,
Université de Paris + + --- class: inverse, center, middle # Perception des différentes marques dans ggplot2 @@ -240,6 +252,14 @@ class: full #### changer la palette par défaut vers une autre palette disponible +--- +```{r} +dsamp <- diamonds[sample(nrow(diamonds), 1000), ] +ggplot(dsamp, aes(carat, price)) + + geom_point(aes(colour = color)) + + scale_color_brewer(palette = "Set3") + + facet_wrap(~color) +``` --- @@ -254,17 +274,8 @@ class: full #### Caler la palette sur le carat moyen #### Annoter le plot avec une ligne désignant le carat moyen et un texte expliquant cette ligne - - - --- -```{r} -dsamp <- diamonds[sample(nrow(diamonds), 1000), ] -ggplot(dsamp, aes(carat, price)) + - geom_point(aes(colour = color)) + - scale_color_brewer(palette = "Set3") + - facet_wrap(~color) -``` + ```{r} @@ -273,7 +284,8 @@ ggplot(dsamp, aes(carat, price)) + scale_color_distiller(palette="RdYlBu") ``` -```{r} +--- +```{r, eval = F} #showtext_auto() #font_add_google("Schoolbell", "bell") @@ -307,7 +319,40 @@ ggplot(dsamp, aes(carat, price)) + legend.position = "none") ``` +--- +```{r, echo = F} + +#showtext_auto() +#font_add_google("Schoolbell", "bell") + +font_family = "sans" +annotate_color = "grey50" + +midpoint = (max(dsamp$carat)-min(dsamp$carat))/2 + +ggplot(dsamp, aes(carat, price)) + + geom_vline(xintercept = midpoint, color = annotate_color) + + geom_point(aes(colour = carat)) + + scale_color_gradient2(low = "#d8b365", + mid="#f5f5f5", + high="#5ab4ac", + midpoint = midpoint) + + annotate("text", + x=.78, y=15000, hjust=1, srt=40, + label ="this is the midpoint", + family=font_family, + color=annotate_color) + + annotate("curve", + x = .8, xend=midpoint-.01, y=15000, yend = 14000, + curvature = -.5, + color=annotate_color , + arrow=arrow(length = unit(0.03, "npc") )) + + theme_elegant() + + theme(panel.grid.minor = element_blank(), + panel.grid.major.x = element_blank(), + legend.position = "none") +``` diff --git a/courses/lab02-perception-colors.html b/courses/lab02-perception-colors.html index 8b8f082..d80ca31 100644 --- a/courses/lab02-perception-colors.html +++ b/courses/lab02-perception-colors.html @@ -13,6 +13,18 @@ +class: center, middle, title + +# Lab 2: Perception et couleurs + +### 2019-2020 + +## Dr. Antoine Neuraz + +### AHU Informatique médicale +#### Hôpital Necker-Enfants malades, </br> Université de Paris + + --- class: inverse, center, middle # Perception des différentes marques dans ggplot2 @@ -236,6 +248,19 @@ class: full #### changer la palette par défaut vers une autre palette disponible +--- + +```r +dsamp <- diamonds[sample(nrow(diamonds), 1000), ] +ggplot(dsamp, aes(carat, price)) + + geom_point(aes(colour = color)) + + scale_color_brewer(palette = "Set3") + + facet_wrap(~color) +``` + +![](lab02-perception-colors_files/figure-html/unnamed-chunk-12-1.png)<!-- --> + + --- ## TODO: couleurs 2 @@ -248,20 +273,8 @@ class: full #### Caler la palette sur le carat moyen #### Annoter le plot avec une ligne désignant le carat moyen et un texte expliquant cette ligne - - - --- -```r -dsamp <- diamonds[sample(nrow(diamonds), 1000), ] -ggplot(dsamp, aes(carat, price)) + - geom_point(aes(colour = color)) + - scale_color_brewer(palette = "Set3") + - facet_wrap(~color) -``` - -![](lab02-perception-colors_files/figure-html/unnamed-chunk-12-1.png)<!-- --> ```r @@ -272,6 +285,7 @@ ggplot(dsamp, aes(carat, price)) + ![](lab02-perception-colors_files/figure-html/unnamed-chunk-13-1.png)<!-- --> +--- ```r #showtext_auto() @@ -306,7 +320,8 @@ ggplot(dsamp, aes(carat, price)) + legend.position = "none") ``` -![](lab02-perception-colors_files/figure-html/unnamed-chunk-14-1.png)<!-- --> +--- +![](lab02-perception-colors_files/figure-html/unnamed-chunk-15-1.png)<!-- --> diff --git a/courses/lab02-perception-colors.pdf b/courses/lab02-perception-colors.pdf new file mode 100644 index 0000000..b21ab7f Binary files /dev/null and b/courses/lab02-perception-colors.pdf differ diff --git a/courses/lab02-perception-colors_files/figure-html/unnamed-chunk-10-1.png b/courses/lab02-perception-colors_files/figure-html/unnamed-chunk-10-1.png index b2b90bd..425b5bc 100644 Binary files a/courses/lab02-perception-colors_files/figure-html/unnamed-chunk-10-1.png and b/courses/lab02-perception-colors_files/figure-html/unnamed-chunk-10-1.png differ diff --git a/courses/lab02-perception-colors_files/figure-html/unnamed-chunk-11-1.png b/courses/lab02-perception-colors_files/figure-html/unnamed-chunk-11-1.png index 01c18e0..81a117d 100644 Binary files a/courses/lab02-perception-colors_files/figure-html/unnamed-chunk-11-1.png and b/courses/lab02-perception-colors_files/figure-html/unnamed-chunk-11-1.png differ diff --git a/courses/lab02-perception-colors_files/figure-html/unnamed-chunk-13-1.png b/courses/lab02-perception-colors_files/figure-html/unnamed-chunk-13-1.png index c13cffd..0e36201 100644 Binary files a/courses/lab02-perception-colors_files/figure-html/unnamed-chunk-13-1.png and b/courses/lab02-perception-colors_files/figure-html/unnamed-chunk-13-1.png differ diff --git a/courses/lab02-perception-colors_files/figure-html/unnamed-chunk-14-1.png b/courses/lab02-perception-colors_files/figure-html/unnamed-chunk-14-1.png index 945b326..95c6f7c 100644 Binary files a/courses/lab02-perception-colors_files/figure-html/unnamed-chunk-14-1.png and b/courses/lab02-perception-colors_files/figure-html/unnamed-chunk-14-1.png differ diff --git a/courses/lab02-perception-colors_files/figure-html/unnamed-chunk-15-1.png b/courses/lab02-perception-colors_files/figure-html/unnamed-chunk-15-1.png index 945b326..95c6f7c 100644 Binary files a/courses/lab02-perception-colors_files/figure-html/unnamed-chunk-15-1.png and b/courses/lab02-perception-colors_files/figure-html/unnamed-chunk-15-1.png differ diff --git a/courses/lab02-perception-colors_files/figure-html/unnamed-chunk-7-1.png b/courses/lab02-perception-colors_files/figure-html/unnamed-chunk-7-1.png index f29a83c..993b357 100644 Binary files a/courses/lab02-perception-colors_files/figure-html/unnamed-chunk-7-1.png and b/courses/lab02-perception-colors_files/figure-html/unnamed-chunk-7-1.png differ diff --git a/courses/lab03-tabulaire.html b/courses/lab03-tabulaire.html index 0920651..19253ef 100644 --- a/courses/lab03-tabulaire.html +++ b/courses/lab03-tabulaire.html @@ -44,8 +44,8 @@ class: center, middle, title read_csv("lab03_data/notes.csv") -> notes ``` -
- +
+ --- @@ -61,8 +61,8 @@ pivot_longer(notes, values_to = "Note") -> notes_long ``` -
- +
+ --- @@ -77,8 +77,8 @@ pivot_wider(notes_long, values_from = Note) ``` -
- +
+ --- diff --git a/courses/lab03-tabulaire.pdf b/courses/lab03-tabulaire.pdf new file mode 100644 index 0000000..a7f5120 Binary files /dev/null and b/courses/lab03-tabulaire.pdf differ diff --git a/courses/lab03_webscraping.html b/courses/lab03_webscraping.html index e5add58..bfb9a5a 100644 --- a/courses/lab03_webscraping.html +++ b/courses/lab03_webscraping.html @@ -65,7 +65,7 @@ GET("https://en.wikipedia.org/wiki/Comparison_of_operating_systems") -> wiki ``` ## Response [https://en.wikipedia.org/wiki/Comparison_of_operating_systems] -## Date: 2019-11-18 18:17 +## Date: 2019-11-21 22:05 ## Status: 200 ## Content-Type: text/html; charset=UTF-8 ## Size: 241 kB @@ -153,8 +153,8 @@ wiki_html %>% html_table -> wikitable ``` -
- +
+ --- diff --git a/courses/lab03_webscraping.pdf b/courses/lab03_webscraping.pdf new file mode 100644 index 0000000..7e69cda Binary files /dev/null and b/courses/lab03_webscraping.pdf differ diff --git a/courses/lab06_graphes.html b/courses/lab06_graphes.html index 1b45d82..ba8f0a6 100644 --- a/courses/lab06_graphes.html +++ b/courses/lab06_graphes.html @@ -233,8 +233,8 @@ graph.star(n = 10, mode = "out") read_csv("lab06_data/OMIM.csv") -> OMIM ``` -
- +
+ --- # Chargement du graphe @@ -246,9 +246,9 @@ graph.data.frame(OMIM, directed = F) -> graphe ``` -## IGRAPH 01bad3b UN-- 6288 4234 -- +## IGRAPH d3876e8 UN-- 6288 4234 -- ## + attr: name (v/c) -## + edges from 01bad3b (vertex names): +## + edges from d3876e8 (vertex names): ## [1] ADRENAL HYPERPLASIA, CONGENITAL, DUE TO 17-ALPHA-HYDROXYLASE DEFICIENCY--CYP17A1 ## [2] 17-BETA-HYDROXYSTEROID DEHYDROGENASE X DEFICIENCY --HSD17B10 ## [3] 2-METHYLBUTYRYL-CoA DEHYDROGENASE DEFICIENCY --ACADSB @@ -304,7 +304,7 @@ neighbors(graphe, V(graphe)[2019]) ``` ``` -## + 1/6288 vertex, named, from 01bad3b: +## + 1/6288 vertex, named, from d3876e8: ## [1] SLC25A3 ``` @@ -333,9 +333,9 @@ HDN ``` ``` -## IGRAPH 8db9c9b UNW- 3512 2839 -- +## IGRAPH 58fb0bd UNW- 3512 2839 -- ## + attr: name (v/c), weight (e/n) -## + edges from 8db9c9b (vertex names): +## + edges from 58fb0bd (vertex names): ## [1] 17-BETA-HYDROXYSTEROID DEHYDROGENASE X DEFICIENCY--MENTAL RETARDATION, X-LINKED 17 ## [2] 17-BETA-HYDROXYSTEROID DEHYDROGENASE X DEFICIENCY--MENTAL RETARDATION, X-LINKED, SYNDROMIC 10 ## [3] 3-HYDROXYACYL-CoA DEHYDROGENASE DEFICIENCY --HYPERINSULINEMIC HYPOGLYCEMIA, FAMILIAL, 4 @@ -353,9 +353,9 @@ HGN ``` ``` -## IGRAPH 65dfdde UNW- 2776 2810 -- +## IGRAPH f5f35d8 UNW- 2776 2810 -- ## + attr: name (v/c), weight (e/n) -## + edges from 65dfdde (vertex names): +## + edges from f5f35d8 (vertex names): ## [1] AKR1C2--AKR1C4 LMNA --MYBPC3 LMNA --ZMPSTE24 GNAS --SSTR5 ## [5] GNAS --AIP GNAS --STX16 GNAS --GNASAS1 COL2A1--COL11A2 ## [9] FGFR3 --KRAS FGFR3 --HRAS FGFR3 --RB1 FGFR3 --PIK3CA diff --git a/courses/lab06_graphes.pdf b/courses/lab06_graphes.pdf new file mode 100644 index 0000000..3427fcd Binary files /dev/null and b/courses/lab06_graphes.pdf differ diff --git a/courses/lab06_graphes_files/figure-html/Ostéogénèses-1.png b/courses/lab06_graphes_files/figure-html/Ostéogénèses-1.png index 28edad8..aca3ded 100644 Binary files a/courses/lab06_graphes_files/figure-html/Ostéogénèses-1.png and b/courses/lab06_graphes_files/figure-html/Ostéogénèses-1.png differ diff --git a/courses/lab06_graphes_files/figure-html/cardiaques-1.png b/courses/lab06_graphes_files/figure-html/cardiaques-1.png index c0b2c1e..f6ac5d1 100644 Binary files a/courses/lab06_graphes_files/figure-html/cardiaques-1.png and b/courses/lab06_graphes_files/figure-html/cardiaques-1.png differ diff --git a/courses/lab06_graphes_files/figure-html/surdités-1.png b/courses/lab06_graphes_files/figure-html/surdités-1.png index 8663582..979726b 100644 Binary files a/courses/lab06_graphes_files/figure-html/surdités-1.png and b/courses/lab06_graphes_files/figure-html/surdités-1.png differ diff --git a/courses/lab7-temporal_data.Rmd b/courses/lab7-temporal_data.Rmd index 1f03aff..193560f 100644 --- a/courses/lab7-temporal_data.Rmd +++ b/courses/lab7-temporal_data.Rmd @@ -21,6 +21,8 @@ library(ggplot2) library(gghighlight) library(dplyr) library(ggTimeSeries) +library(hrbrthemes) +library(gganimate) ``` ## TODO @@ -47,27 +49,16 @@ library(ggTimeSeries) --- ```{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') +to_plot <- left_join(us_city_populations, last_ranks, by= 'City') right_axis <- to_plot %>% group_by(City) %>% @@ -85,14 +76,16 @@ labels <- right_axis %>% --- class: full -```{r, echo = FALSE} -ggplot(to_plot, aes(x=Year, y = Population, group = City, color = City)) + +```{r, echo = TRUE} +p <- 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 ))+ + sec.axis = sec_axis(~ ., + breaks = ends, + labels = labels ))+ scale_color_viridis_d()+ theme_elegant_dark()+ theme(legend.position = "none", @@ -101,14 +94,23 @@ ggplot(to_plot, aes(x=Year, y = Population, group = City, color = City)) + 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") + gghighlight(max(last_rank) <= n_cities, + use_direct_label = FALSE, + label_key = City, + unhighlighted_colour = "grey20") ``` --- class: full -```{r, echo = FALSE} +```{r, echo = TRUE} +p +``` +--- + +class: full +```{r, echo = TRUE} library(ggTimeSeries) -to_plot %>% filter(City %in% labels) %>% +p <- 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() + @@ -121,7 +123,122 @@ to_plot %>% filter(City %in% labels) %>% panel.grid.major.y = element_line(color= 'grey30', size = .2) ) ``` + +--- +class: full +```{r} +p +``` --- +class: inverse, center, middle +# Barchart race + +--- +## Load data +```{r load_data} +data("us_city_populations") + +n_cities = 10 + +top_cities <-us_city_populations %>% filter(Rank <= n_cities) %>% + select(City, State, Region) %>% distinct() + +``` + +--- +## Create all missing dates +```{r, combine_dates} +# create a data frame with all the years between min and max Year +all_years <- data.frame(Year = seq(min(us_city_populations$Year), + max(us_city_populations$Year), 1)) + +# combine top_cities and all_years +all_combos <- merge(top_cities, all_years, all = T) + +# combine all_combos with the original dataset +res_interp <- merge(us_city_populations, all_combos, all.y = T) +``` + +## Interpolate the Populations when missing (linear interpolation here) +```{r, interpolate} +res_interp <- res_interp %>% + group_by(City) %>% + mutate(Population=approx(Year,Population,Year)$y) +``` + +--- +## Filter data +```{r, filter_for_plot} +to_plot <- res_interp %>% + group_by(Year) %>% + arrange(-Population) %>% + mutate(Rank=row_number()) %>% + filter(Rank<=n_cities) + +``` + +--- +## Ease transitions + +```{r} +to_plot_trans <- to_plot %>% + group_by(City) %>% + arrange(Year) %>% + mutate(lag_rank = lag(Rank, 1), + change = ifelse(Rank > lag(Rank, 1), 1, 0), + change = ifelse(Rank < lag(Rank, 1), -1, 0)) %>% + mutate(transition = ifelse(lead(change, 1) == -1, -.9, 0), + transition = ifelse(lead(change,2) == -1, -.5, transition), + transition = ifelse(lead(change,3) == -1, -.3, transition), + transition = ifelse(lead(change, 1) == 1, .9, transition), + transition = ifelse(lead(change,2) == 1, .5, transition), + transition = ifelse(lead(change,3) == 1, .3, transition)) %>% + mutate(trans_rank = Rank + transition) + +``` + + +--- +## Make the plot +.small[ +```{r, make_plot} +p <- to_plot_trans %>% + ggplot(aes(x = -trans_rank,y = Population, group =City)) + + geom_tile(aes(y = Population / 2, height = Population, fill = Region), + width = 0.9) + + geom_text(aes(label = City), + hjust = "right", colour = "white", + fontface="bold", nudge_y = -100000) + + geom_text(aes(label = scales::comma(Population,big.mark = ' ')), + hjust = "left", nudge_y = 100000, colour = "grey90") + + coord_flip(clip="off") + + hrbrthemes::scale_fill_ipsum() + + scale_x_discrete("") + + scale_y_continuous("",labels=scales::comma_format(big.mark = " ")) + + theme_elegant_dark(base_size = 20) + + theme( + panel.grid.minor.x=element_blank(), + axis.line = element_blank(), + panel.grid.major= element_line(color='lightgrey', size=.2), + legend.position = c(0.6, 0.2), + plot.margin = margin(1,1,1,2,"cm"), + plot.title = element_text(hjust = 0), + axis.text.y=element_blank(), + legend.text = element_text(size = 15), + legend.background = element_blank()) + + # gganimate code to transition by year: + transition_time(Year) + + ease_aes('cubic-in-out') + + labs(title='Evolution des plus grandes villes US', + subtitle='Population en {round(frame_time,0)}') + +``` +] +--- +```{r, animate, eval = FALSE} +animate(p, nframes = 400, fps = 25, end_pause = 30, width = 1200) +anim_save("bar_race.gif", animation = last_animation()) +``` -![](bar_race.gif) +![:scale 80%](bar_race.gif) diff --git a/courses/lab7-temporal_data.html b/courses/lab7-temporal_data.html index e7af9f3..408db75 100644 --- a/courses/lab7-temporal_data.html +++ b/courses/lab7-temporal_data.html @@ -1,8 +1,8 @@ - + Lab 07 - Données temporelles et textuelles - + @@ -42,22 +42,12 @@ 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') +to_plot <- left_join(us_city_populations, last_ranks, by= 'City') right_axis <- to_plot %>% group_by(City) %>% @@ -74,14 +64,182 @@ labels <- right_axis %>% --- class: full -![](lab7-temporal_data_files/figure-html/unnamed-chunk-2-1.png)<!-- --> + +```r +p <- ggplot(to_plot, aes(x=Year, y = Population, + group = City, color = City)) + + geom_line(size=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 +p +``` + ![](lab7-temporal_data_files/figure-html/unnamed-chunk-3-1.png)<!-- --> --- -![](bar_race.gif) +class: full + +```r +library(ggTimeSeries) +p <- 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) ) +``` + +--- +class: full + +```r +p +``` + +![](lab7-temporal_data_files/figure-html/unnamed-chunk-5-1.png)<!-- --> +--- +class: inverse, center, middle +# Barchart race + +--- +## Load data + +```r +data("us_city_populations") + +n_cities = 10 + +top_cities <-us_city_populations %>% filter(Rank <= n_cities) %>% + select(City, State, Region) %>% distinct() +``` + +--- +## Create all missing dates + +```r +# create a data frame with all the years between min and max Year +all_years <- data.frame(Year = seq(min(us_city_populations$Year), + max(us_city_populations$Year), 1)) + +# combine top_cities and all_years +all_combos <- merge(top_cities, all_years, all = T) + +# combine all_combos with the original dataset +res_interp <- merge(us_city_populations, all_combos, all.y = T) +``` + +## Interpolate the Populations when missing (linear interpolation here) + +```r +res_interp <- res_interp %>% + group_by(City) %>% + mutate(Population=approx(Year,Population,Year)$y) +``` + +--- +## Filter data + +```r +to_plot <- res_interp %>% + group_by(Year) %>% + arrange(-Population) %>% + mutate(Rank=row_number()) %>% + filter(Rank<=n_cities) +``` + +--- +## Ease transitions + + +```r +to_plot_trans <- to_plot %>% + group_by(City) %>% + arrange(Year) %>% + mutate(lag_rank = lag(Rank, 1), + change = ifelse(Rank > lag(Rank, 1), 1, 0), + change = ifelse(Rank < lag(Rank, 1), -1, 0)) %>% + mutate(transition = ifelse(lead(change, 1) == -1, -.9, 0), + transition = ifelse(lead(change,2) == -1, -.5, transition), + transition = ifelse(lead(change,3) == -1, -.3, transition), + transition = ifelse(lead(change, 1) == 1, .9, transition), + transition = ifelse(lead(change,2) == 1, .5, transition), + transition = ifelse(lead(change,3) == 1, .3, transition)) %>% + mutate(trans_rank = Rank + transition) +``` + + +--- +## Make the plot +.small[ + +```r +p <- to_plot_trans %>% + ggplot(aes(x = -trans_rank,y = Population, group =City)) + + geom_tile(aes(y = Population / 2, height = Population, fill = Region), + width = 0.9) + + geom_text(aes(label = City), + hjust = "right", colour = "white", + fontface="bold", nudge_y = -100000) + + geom_text(aes(label = scales::comma(Population,big.mark = ' ')), + hjust = "left", nudge_y = 100000, colour = "grey90") + + coord_flip(clip="off") + + hrbrthemes::scale_fill_ipsum() + + scale_x_discrete("") + + scale_y_continuous("",labels=scales::comma_format(big.mark = " ")) + + theme_elegant_dark(base_size = 20) + + theme( + panel.grid.minor.x=element_blank(), + axis.line = element_blank(), + panel.grid.major= element_line(color='lightgrey', size=.2), + legend.position = c(0.6, 0.2), + plot.margin = margin(1,1,1,2,"cm"), + plot.title = element_text(hjust = 0), + axis.text.y=element_blank(), + legend.text = element_text(size = 15), + legend.background = element_blank()) + + # gganimate code to transition by year: + transition_time(Year) + + ease_aes('cubic-in-out') + + labs(title='Evolution des plus grandes villes US', + subtitle='Population en {round(frame_time,0)}') +``` +] +--- + +```r +animate(p, nframes = 400, fps = 25, end_pause = 30, width = 1200) +anim_save("bar_race.gif", animation = last_animation()) +``` + +![:scale 80%](bar_race.gif) + + +