class: center, middle, inverse, title-slide # R : des premiers pas au modèle linéaire ## Visualisation de données ### Marie-Pierre Etienne ###
https://marieetienne.github.io/premierspas/
### Mars 2021 --- name: intro count: false # Introduction --- template: intro ## Pourquoi la visualisation des données est importante ? <br> <br> - La chronique de Constance par Constance dans Par Jupiter <br> <audio controls> <source src="../resources/audio/2020_10_27_Constance_Par_Jupiter.mp3" type="audio/ogg"/> </audio> --- template: intro ## Un exemple inspirant : le réchauffement climatique en une image <img src="../resources/figs/lemonde_rechauffement.png" width="46%" style="display: block; margin: auto;" /> [Extrait du journal Le Monde le 3 janvier 2020](https://www.lemonde.fr/les-decodeurs/article/2020/01/03/2019-est-la-troisieme-annee-la-plus-chaude-en-france-visualisez-la-hausse-dans-votre-ville_6024699_4355770.html?xtor=CS2-33281034-%5BKW%5D-%5Bgratuit%5D-%5Barticleacquisition%5D&utm_campaign=keywee_acquisition&utm_medium=PaidSocial&utm_source=Facebook&kwp_0=1527732&kwp_4=4629376&kwp_1=1961091) -- <p class="question"> Qu'est ce qui est efficace dans cette visualisation ? </p> --- template: intro ## Le musée des horreurs - [Le pourcentage de fumeurs aux Pays-Bas](https://viz.wtf/image/630887965804150786) <img src="../resources/figs/smockers.png" width="50%" style="display: block; margin: auto;" /> --- template: intro count: false ## Le musée des horreurs - [Le Tumblr WTFViz](https://viz.wtf/) - [Probabilité d'infection par le coronavirus par G. Forestier](https://i.redd.it/b3vvel2xtsv51.jpg) <img src="../resources/figs/corona_gf.png" width="50%" style="display: block; margin: auto;" /> --- template: intro count: false ## Le musée des horreurs - [Le Tumblr WTFViz](https://viz.wtf/) - [Probabilité d'infection par le coronavirus par G. Forestier](https://i.redd.it/b3vvel2xtsv51.jpg) - [Le coronavirus par Jean Castex](https://www.ouest-france.fr/sante/virus/coronavirus/covid-19-couvre-feu-que-voulaient-dire-les-droles-de-graphiques-montres-par-jean-castex-7016245) <img src="../resources/figs/corona_j_castex.png" width="50%" style="display: block; margin: auto;" /> --- template: intro ## Quels sont les éléments d'une visualisation efficace ? - $$ $$ - $$ $$ - $$ $$ - $$ $$ - $$ $$ - $$ $$ - $$ $$ -- .care[ Objectif du jour :] Acquérir des outils pour gérer ces différents aspects --- template: intro ## Some resources - [Data visualization with R](https://rkabacoff.github.io/datavis/) - [R for Data Science](https://r4ds.had.co.nz/) - [R Graphics Cookbook](http://www.cookbook-r.com/) - [ggplot2: Elegant Graphics for Data Analysis](https://ggplot2-book.org/index.html) - [R graph gallery](https://www.r-graph-gallery.com/ggplot2-package.html) - [Tutoriel de Raphaelle Momal et Marie Perrot Dockes](https://stateofther.github.io/post/fancy-plotting/) - [Don’t cha wish your ggplot had colours like me?](https://www.dataembassy.co.nz/Liza-colours-in-R#22) - [Valoriser ses données avec R](https://mtes-mct.github.io/parcours-r/m5/) (formation du Ministere de la Transition Écologique et Solidaire (MTES) et du Ministère de la Cohésion des Territoires et des relations avec les collectivités territoriales (MCTRCT)) <img src="visu_files/figure-html/graph_bib-1.png" width="250px" height="150px" style="display: block; margin: auto;" /> --- # ggplot presentation gg stands for Grammar of Graphics and ggplot2 is *based on the Grammar of Graphics , that allows you to compose graphs by combining independent components* . *[The] grammar tells us that a graphic maps the* - *data* - *to the aesthetic attributes (colour, shape, size)* - *of geometric objects (points, lines, bars)*. - *The plot may also include statistical transformations of the data and information about the plot’s coordinate system*. - *Facetting can be used to plot for different subsets of the data.* *The combination of these independent components are what make up a graphic.* . --- # ggplot presentation gg stands for Grammar of Graphics and ggplot2 is *based on the Grammar of Graphics , that allows you to compose graphs by combining independent components* . *[The] grammar tells us that a graphic maps the* - *data* - *to the aesthetic attributes (colour, shape, size)* - *of geometric objects (points, lines, bars)*. - *The plot may also include statistical transformations of the data and information about the plot’s coordinate system*. - *Facetting can be used to plot for different subsets of the data.* *The combination of these independent components are what make up a graphic.* . ```r library(tidyverse) library(ggplot2) ``` --- name: exemple # Premier exemple : les manchots de Palmers --- template: exemple ## Un peu de révision * Installer le package `palmerpenguins` * Charger ce package. -- Ce package contient un jeu de données qu'on peut charger à l'aide de ```r #remotes::install_github("allisonhorst/palmerpenguins") data(penguins,package = "palmerpenguins") ``` * Renommer les colonnes en `bill_l`, `bill_d`, `flip_l`, `bm` pour raccourcir les noms et stocker le résultat dans la table penguins -- ```r penguins <- penguins %>% rename(bill_l = bill_length_mm, bill_d = bill_depth_mm, flip_l = flipper_length_mm, bm = body_mass_g) penguins %>% print(n=2) ``` ``` ## # A tibble: 344 x 8 ## species island bill_l bill_d flip_l bm sex year ## <fct> <fct> <dbl> <dbl> <int> <int> <fct> <int> ## 1 Adelie Torgersen 39.1 18.7 181 3750 male 2007 ## 2 Adelie Torgersen 39.5 17.4 186 3800 female 2007 ## # … with 342 more rows ``` --- # The Palmer Penguins dataset : overview ```r penguins %>% group_by(species, sex, year, island) %>% mutate(n = n()) %>% summarise_if(is.numeric, mean, na.rm = TRUE) %>% print(n=10) ``` ``` ## # A tibble: 35 x 9 ## # Groups: species, sex, year [22] ## species sex year island bill_l bill_d flip_l bm n ## <fct> <fct> <int> <fct> <dbl> <dbl> <dbl> <dbl> <dbl> ## 1 Adelie female 2007 Biscoe 37.5 18.6 182. 3470 5 ## 2 Adelie female 2007 Dream 37.9 17.8 185 3269. 9 ## 3 Adelie female 2007 Torgersen 38.3 18.2 188. 3475 8 ## 4 Adelie female 2008 Biscoe 36.6 17.2 187. 3244. 9 ## 5 Adelie female 2008 Dream 36.3 17.8 189 3412. 8 ## 6 Adelie female 2008 Torgersen 36.6 17.4 190 3519. 8 ## 7 Adelie female 2009 Biscoe 38.1 17.7 191. 3447. 8 ## 8 Adelie female 2009 Dream 36.6 17.3 190. 3358. 10 ## 9 Adelie female 2009 Torgersen 37.8 17.1 187. 3194. 8 ## 10 Adelie male 2007 Biscoe 39.2 18.3 182. 3770 5 ## # … with 25 more rows ``` --- name: simple # Des graphiques simples --- template: simple ## Un histogramme *pour visualiser la distribution des valeurs*. L'ordonnée porte sur les comptages dans chaque classe de valeurs. ```r penguins %>% ggplot() + aes(x = bill_l) + geom_histogram() ``` ``` ## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. ``` ``` ## Warning: Removed 2 rows containing non-finite values (stat_bin). ``` <img src="visu_files/figure-html/simple_hist-1.png" width="30%" style="display: block; margin: auto;" /> --- count: false .panel1-simple_hist-auto[ ```r *penguins ``` ] .panel2-simple_hist-auto[ ``` ## # A tibble: 344 x 8 ## species island bill_l bill_d flip_l bm sex year ## <fct> <fct> <dbl> <dbl> <int> <int> <fct> <int> ## 1 Adelie Torgersen 39.1 18.7 181 3750 male 2007 ## 2 Adelie Torgersen 39.5 17.4 186 3800 female 2007 ## 3 Adelie Torgersen 40.3 18 195 3250 female 2007 ## 4 Adelie Torgersen NA NA NA NA <NA> 2007 ## 5 Adelie Torgersen 36.7 19.3 193 3450 female 2007 ## 6 Adelie Torgersen 39.3 20.6 190 3650 male 2007 ## 7 Adelie Torgersen 38.9 17.8 181 3625 female 2007 ## 8 Adelie Torgersen 39.2 19.6 195 4675 male 2007 ## 9 Adelie Torgersen 34.1 18.1 193 3475 <NA> 2007 ## 10 Adelie Torgersen 42 20.2 190 4250 <NA> 2007 ## # … with 334 more rows ``` ] --- count: false .panel1-simple_hist-auto[ ```r penguins %>% * ggplot() ``` ] .panel2-simple_hist-auto[ <img src="visu_files/figure-html/simple_hist_auto_02_output-1.png" width="50%" style="display: block; margin: auto;" /> ] --- count: false .panel1-simple_hist-auto[ ```r penguins %>% ggplot() + * aes(x = bill_l) ``` ] .panel2-simple_hist-auto[ <img src="visu_files/figure-html/simple_hist_auto_03_output-1.png" width="50%" style="display: block; margin: auto;" /> ] --- count: false .panel1-simple_hist-auto[ ```r penguins %>% ggplot() + aes(x = bill_l) + * geom_histogram() ``` ] .panel2-simple_hist-auto[ ``` ## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. ``` ``` ## Warning: Removed 2 rows containing non-finite values (stat_bin). ``` <img src="visu_files/figure-html/simple_hist_auto_04_output-1.png" width="50%" style="display: block; margin: auto;" /> ] <style> .panel1-simple_hist-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-simple_hist-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-simple_hist-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- template: simple ## Un histogramme pour visualiser la distribution des valeurs. L'ordonnée porte sur la fréquence dans chaque classe de valeurs. --- count: false .panel1-simple_hist_dens-auto[ ```r *penguins ``` ] .panel2-simple_hist_dens-auto[ ``` ## # A tibble: 344 x 8 ## species island bill_l bill_d flip_l bm sex year ## <fct> <fct> <dbl> <dbl> <int> <int> <fct> <int> ## 1 Adelie Torgersen 39.1 18.7 181 3750 male 2007 ## 2 Adelie Torgersen 39.5 17.4 186 3800 female 2007 ## 3 Adelie Torgersen 40.3 18 195 3250 female 2007 ## 4 Adelie Torgersen NA NA NA NA <NA> 2007 ## 5 Adelie Torgersen 36.7 19.3 193 3450 female 2007 ## 6 Adelie Torgersen 39.3 20.6 190 3650 male 2007 ## 7 Adelie Torgersen 38.9 17.8 181 3625 female 2007 ## 8 Adelie Torgersen 39.2 19.6 195 4675 male 2007 ## 9 Adelie Torgersen 34.1 18.1 193 3475 <NA> 2007 ## 10 Adelie Torgersen 42 20.2 190 4250 <NA> 2007 ## # … with 334 more rows ``` ] --- count: false .panel1-simple_hist_dens-auto[ ```r penguins %>% * ggplot() ``` ] .panel2-simple_hist_dens-auto[ <img src="visu_files/figure-html/simple_hist_dens_auto_02_output-1.png" width="50%" style="display: block; margin: auto;" /> ] --- count: false .panel1-simple_hist_dens-auto[ ```r penguins %>% ggplot() + * aes(x = bill_l) ``` ] .panel2-simple_hist_dens-auto[ <img src="visu_files/figure-html/simple_hist_dens_auto_03_output-1.png" width="50%" style="display: block; margin: auto;" /> ] --- count: false .panel1-simple_hist_dens-auto[ ```r penguins %>% ggplot() + aes(x = bill_l) + * geom_histogram() ``` ] .panel2-simple_hist_dens-auto[ ``` ## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. ``` ``` ## Warning: Removed 2 rows containing non-finite values (stat_bin). ``` <img src="visu_files/figure-html/simple_hist_dens_auto_04_output-1.png" width="50%" style="display: block; margin: auto;" /> ] --- count: false .panel1-simple_hist_dens-auto[ ```r penguins %>% ggplot() + aes(x = bill_l) + geom_histogram() + * aes(y = ..density..) ``` ] .panel2-simple_hist_dens-auto[ ``` ## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. ``` ``` ## Warning: Removed 2 rows containing non-finite values (stat_bin). ``` <img src="visu_files/figure-html/simple_hist_dens_auto_05_output-1.png" width="50%" style="display: block; margin: auto;" /> ] <style> .panel1-simple_hist_dens-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-simple_hist_dens-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-simple_hist_dens-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- template: simple ## Un histogramme pour visualiser la distribution des valeurs. L'ordonnée porte sur la fréquence dans chaque classe de valeurs. ```r penguins %>% ggplot() + geom_histogram(aes(x= bill_l, y = ..density..)) ``` ``` ## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. ``` ``` ## Warning: Removed 2 rows containing non-finite values (stat_bin). ``` <img src="visu_files/figure-html/simple_hist_dens_simple-1.png" width="30%" style="display: block; margin: auto;" /> --- template: simple ## Un nuage de points (scatter plot) *pour visualiser le lien entre deux variables quantitatives*. ```r penguins %>% ggplot() + aes( x= bill_l, y=bill_d) + geom_point() ``` ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` <img src="visu_files/figure-html/simple_scatter-1.png" width="30%" style="display: block; margin: auto;" /> --- count: false .panel1-simple_scatter-auto[ ```r *penguins ``` ] .panel2-simple_scatter-auto[ ``` ## # A tibble: 344 x 8 ## species island bill_l bill_d flip_l bm sex year ## <fct> <fct> <dbl> <dbl> <int> <int> <fct> <int> ## 1 Adelie Torgersen 39.1 18.7 181 3750 male 2007 ## 2 Adelie Torgersen 39.5 17.4 186 3800 female 2007 ## 3 Adelie Torgersen 40.3 18 195 3250 female 2007 ## 4 Adelie Torgersen NA NA NA NA <NA> 2007 ## 5 Adelie Torgersen 36.7 19.3 193 3450 female 2007 ## 6 Adelie Torgersen 39.3 20.6 190 3650 male 2007 ## 7 Adelie Torgersen 38.9 17.8 181 3625 female 2007 ## 8 Adelie Torgersen 39.2 19.6 195 4675 male 2007 ## 9 Adelie Torgersen 34.1 18.1 193 3475 <NA> 2007 ## 10 Adelie Torgersen 42 20.2 190 4250 <NA> 2007 ## # … with 334 more rows ``` ] --- count: false .panel1-simple_scatter-auto[ ```r penguins %>% * ggplot() ``` ] .panel2-simple_scatter-auto[ <img src="visu_files/figure-html/simple_scatter_auto_02_output-1.png" width="50%" style="display: block; margin: auto;" /> ] --- count: false .panel1-simple_scatter-auto[ ```r penguins %>% ggplot() + * aes( x= bill_l, y=bill_d) ``` ] .panel2-simple_scatter-auto[ <img src="visu_files/figure-html/simple_scatter_auto_03_output-1.png" width="50%" style="display: block; margin: auto;" /> ] --- count: false .panel1-simple_scatter-auto[ ```r penguins %>% ggplot() + aes( x= bill_l, y=bill_d) + * geom_point() ``` ] .panel2-simple_scatter-auto[ ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` <img src="visu_files/figure-html/simple_scatter_auto_04_output-1.png" width="50%" style="display: block; margin: auto;" /> ] <style> .panel1-simple_scatter-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-simple_scatter-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-simple_scatter-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- template: simple ## Des boîtes à moustache pour visualiser le lien entre une variable qualitative et une variable quantitative ```r penguins %>% ggplot() + aes( x= species, y=bill_d) + geom_boxplot() ``` ``` ## Warning: Removed 2 rows containing non-finite values (stat_boxplot). ``` <img src="visu_files/figure-html/simple_boxplot-1.png" width="30%" style="display: block; margin: auto;" /> --- count: false .panel1-simple_boxplot-auto[ ```r *penguins ``` ] .panel2-simple_boxplot-auto[ ``` ## # A tibble: 344 x 8 ## species island bill_l bill_d flip_l bm sex year ## <fct> <fct> <dbl> <dbl> <int> <int> <fct> <int> ## 1 Adelie Torgersen 39.1 18.7 181 3750 male 2007 ## 2 Adelie Torgersen 39.5 17.4 186 3800 female 2007 ## 3 Adelie Torgersen 40.3 18 195 3250 female 2007 ## 4 Adelie Torgersen NA NA NA NA <NA> 2007 ## 5 Adelie Torgersen 36.7 19.3 193 3450 female 2007 ## 6 Adelie Torgersen 39.3 20.6 190 3650 male 2007 ## 7 Adelie Torgersen 38.9 17.8 181 3625 female 2007 ## 8 Adelie Torgersen 39.2 19.6 195 4675 male 2007 ## 9 Adelie Torgersen 34.1 18.1 193 3475 <NA> 2007 ## 10 Adelie Torgersen 42 20.2 190 4250 <NA> 2007 ## # … with 334 more rows ``` ] --- count: false .panel1-simple_boxplot-auto[ ```r penguins %>% * ggplot() ``` ] .panel2-simple_boxplot-auto[ <img src="visu_files/figure-html/simple_boxplot_auto_02_output-1.png" width="50%" style="display: block; margin: auto;" /> ] --- count: false .panel1-simple_boxplot-auto[ ```r penguins %>% ggplot() + * aes( x= species, y=bill_d) ``` ] .panel2-simple_boxplot-auto[ <img src="visu_files/figure-html/simple_boxplot_auto_03_output-1.png" width="50%" style="display: block; margin: auto;" /> ] --- count: false .panel1-simple_boxplot-auto[ ```r penguins %>% ggplot() + aes( x= species, y=bill_d) + * geom_boxplot() ``` ] .panel2-simple_boxplot-auto[ ``` ## Warning: Removed 2 rows containing non-finite values (stat_boxplot). ``` <img src="visu_files/figure-html/simple_boxplot_auto_04_output-1.png" width="50%" style="display: block; margin: auto;" /> ] <style> .panel1-simple_boxplot-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-simple_boxplot-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-simple_boxplot-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- template: simple ## Garder un dessin pour l'enrichir plus tard ```r gg <- penguins %>% ggplot() + aes(x = bill_l) + geom_histogram() gg + aes(y = ..density..) ``` --- count: false .panel1-gg_save-auto[ ```r *gg <- penguins ``` ] .panel2-gg_save-auto[ ] --- count: false .panel1-gg_save-auto[ ```r gg <- penguins %>% * ggplot() ``` ] .panel2-gg_save-auto[ ] --- count: false .panel1-gg_save-auto[ ```r gg <- penguins %>% ggplot() + * aes(x = bill_l) ``` ] .panel2-gg_save-auto[ ] --- count: false .panel1-gg_save-auto[ ```r gg <- penguins %>% ggplot() + aes(x = bill_l) + * geom_histogram() ``` ] .panel2-gg_save-auto[ ] --- count: false .panel1-gg_save-auto[ ```r gg <- penguins %>% ggplot() + aes(x = bill_l) + geom_histogram() *gg ``` ] .panel2-gg_save-auto[ ``` ## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. ``` ``` ## Warning: Removed 2 rows containing non-finite values (stat_bin). ``` <img src="visu_files/figure-html/gg_save_auto_05_output-1.png" width="50%" style="display: block; margin: auto;" /> ] --- count: false .panel1-gg_save-auto[ ```r gg <- penguins %>% ggplot() + aes(x = bill_l) + geom_histogram() gg + * aes(y = ..density..) ``` ] .panel2-gg_save-auto[ ``` ## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. ``` ``` ## Warning: Removed 2 rows containing non-finite values (stat_bin). ``` <img src="visu_files/figure-html/gg_save_auto_06_output-1.png" width="50%" style="display: block; margin: auto;" /> ] <style> .panel1-gg_save-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-gg_save-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-gg_save-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- template: simple ## Garder un dessin pour l'enrichir plus tard ```r gg <- penguins %>% ggplot() + aes( x= bill_l, y=bill_d) + geom_point() gg <- gg + aes(col = species) print(gg) ``` ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` <img src="visu_files/figure-html/gg1_save-1.png" width="50%" style="display: block; margin: auto;" /> --- count: false .panel1-gg1_save-auto[ ```r *gg <- penguins ``` ] .panel2-gg1_save-auto[ ] --- count: false .panel1-gg1_save-auto[ ```r gg <- penguins %>% * ggplot() ``` ] .panel2-gg1_save-auto[ ] --- count: false .panel1-gg1_save-auto[ ```r gg <- penguins %>% ggplot() + * aes( x= bill_l, y=bill_d) ``` ] .panel2-gg1_save-auto[ ] --- count: false .panel1-gg1_save-auto[ ```r gg <- penguins %>% ggplot() + aes( x= bill_l, y=bill_d) + * geom_point() ``` ] .panel2-gg1_save-auto[ ] --- count: false .panel1-gg1_save-auto[ ```r gg <- penguins %>% ggplot() + aes( x= bill_l, y=bill_d) + geom_point() *gg <- gg ``` ] .panel2-gg1_save-auto[ ] --- count: false .panel1-gg1_save-auto[ ```r gg <- penguins %>% ggplot() + aes( x= bill_l, y=bill_d) + geom_point() gg <- gg + * aes(col = species) ``` ] .panel2-gg1_save-auto[ ] --- count: false .panel1-gg1_save-auto[ ```r gg <- penguins %>% ggplot() + aes( x= bill_l, y=bill_d) + geom_point() gg <- gg + aes(col = species) *print(gg) ``` ] .panel2-gg1_save-auto[ ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` <img src="visu_files/figure-html/gg1_save_auto_07_output-1.png" width="50%" style="display: block; margin: auto;" /> ] <style> .panel1-gg1_save-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-gg1_save-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-gg1_save-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- name: color # Enrichir les graphiques avec la couleur --- template: color ## Ajouter une information de groupes sur un nuage de points ```r penguins %>% ggplot() + aes( x= bill_l, y=bill_d) + geom_point() + #BREAK aes(col = species) #BREAK ``` --- count: false .panel1-scatter_plot_species-user[ ```r *penguins %>% * ggplot() + * aes( x= bill_l, y=bill_d) + * geom_point() ``` ] .panel2-scatter_plot_species-user[ ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` <img src="visu_files/figure-html/scatter_plot_species_user_01_output-1.png" width="50%" style="display: block; margin: auto;" /> ] --- count: false .panel1-scatter_plot_species-user[ ```r penguins %>% ggplot() + aes( x= bill_l, y=bill_d) + geom_point() + * aes(col = species) ``` ] .panel2-scatter_plot_species-user[ ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` <img src="visu_files/figure-html/scatter_plot_species_user_02_output-1.png" width="50%" style="display: block; margin: auto;" /> ] <style> .panel1-scatter_plot_species-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-scatter_plot_species-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-scatter_plot_species-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- template: color ## Ajouter une information de groupes sur un histogramme ```r penguins %>% ggplot() + aes(x = bill_l, fill = species) + geom_histogram() ``` --- count: false .panel1-hist_color_stack-auto[ ```r *penguins ``` ] .panel2-hist_color_stack-auto[ ``` ## # A tibble: 344 x 8 ## species island bill_l bill_d flip_l bm sex year ## <fct> <fct> <dbl> <dbl> <int> <int> <fct> <int> ## 1 Adelie Torgersen 39.1 18.7 181 3750 male 2007 ## 2 Adelie Torgersen 39.5 17.4 186 3800 female 2007 ## 3 Adelie Torgersen 40.3 18 195 3250 female 2007 ## 4 Adelie Torgersen NA NA NA NA <NA> 2007 ## 5 Adelie Torgersen 36.7 19.3 193 3450 female 2007 ## 6 Adelie Torgersen 39.3 20.6 190 3650 male 2007 ## 7 Adelie Torgersen 38.9 17.8 181 3625 female 2007 ## 8 Adelie Torgersen 39.2 19.6 195 4675 male 2007 ## 9 Adelie Torgersen 34.1 18.1 193 3475 <NA> 2007 ## 10 Adelie Torgersen 42 20.2 190 4250 <NA> 2007 ## # … with 334 more rows ``` ] --- count: false .panel1-hist_color_stack-auto[ ```r penguins %>% * ggplot() + aes(x = bill_l, fill = species) ``` ] .panel2-hist_color_stack-auto[ <img src="visu_files/figure-html/hist_color_stack_auto_02_output-1.png" width="50%" style="display: block; margin: auto;" /> ] --- count: false .panel1-hist_color_stack-auto[ ```r penguins %>% ggplot() + aes(x = bill_l, fill = species) + * geom_histogram() ``` ] .panel2-hist_color_stack-auto[ ``` ## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. ``` ``` ## Warning: Removed 2 rows containing non-finite values (stat_bin). ``` <img src="visu_files/figure-html/hist_color_stack_auto_03_output-1.png" width="50%" style="display: block; margin: auto;" /> ] <style> .panel1-hist_color_stack-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-hist_color_stack-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-hist_color_stack-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- template: color ## Ajouter une information de groupes sur un histogramme Attention les couleurs représentent la part de chaque groupe dans la barre de l'histogramme. ```r penguins %>% ggplot() + aes(x = bill_l, fill = species) + geom_histogram() ``` <img src="visu_files/figure-html/hist_color_stack-1.png" width="20%" height="30%" style="display: block; margin: auto;" /> -- On peut vouloir faire 3 histogrammes différents ```r penguins %>% ggplot() + aes(x = bill_l, fill = species) + geom_histogram(position = 'identity') ``` --- count: false .panel1-hist_color_diff-auto[ ```r *penguins ``` ] .panel2-hist_color_diff-auto[ ``` ## # A tibble: 344 x 8 ## species island bill_l bill_d flip_l bm sex year ## <fct> <fct> <dbl> <dbl> <int> <int> <fct> <int> ## 1 Adelie Torgersen 39.1 18.7 181 3750 male 2007 ## 2 Adelie Torgersen 39.5 17.4 186 3800 female 2007 ## 3 Adelie Torgersen 40.3 18 195 3250 female 2007 ## 4 Adelie Torgersen NA NA NA NA <NA> 2007 ## 5 Adelie Torgersen 36.7 19.3 193 3450 female 2007 ## 6 Adelie Torgersen 39.3 20.6 190 3650 male 2007 ## 7 Adelie Torgersen 38.9 17.8 181 3625 female 2007 ## 8 Adelie Torgersen 39.2 19.6 195 4675 male 2007 ## 9 Adelie Torgersen 34.1 18.1 193 3475 <NA> 2007 ## 10 Adelie Torgersen 42 20.2 190 4250 <NA> 2007 ## # … with 334 more rows ``` ] --- count: false .panel1-hist_color_diff-auto[ ```r penguins %>% * ggplot() + aes(x = bill_l, fill = species) ``` ] .panel2-hist_color_diff-auto[ <img src="visu_files/figure-html/hist_color_diff_auto_02_output-1.png" width="50%" style="display: block; margin: auto;" /> ] --- count: false .panel1-hist_color_diff-auto[ ```r penguins %>% ggplot() + aes(x = bill_l, fill = species) + * geom_histogram(position = 'identity') ``` ] .panel2-hist_color_diff-auto[ ``` ## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. ``` ``` ## Warning: Removed 2 rows containing non-finite values (stat_bin). ``` <img src="visu_files/figure-html/hist_color_diff_auto_03_output-1.png" width="50%" style="display: block; margin: auto;" /> ] <style> .panel1-hist_color_diff-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-hist_color_diff-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-hist_color_diff-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- template: color ## une palette pour daltoniens --- count: false .panel1-scatter_viridis-auto[ ```r *gg ``` ] .panel2-scatter_viridis-auto[ ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` <img src="visu_files/figure-html/scatter_viridis_auto_01_output-1.png" width="50%" style="display: block; margin: auto;" /> ] --- count: false .panel1-scatter_viridis-auto[ ```r gg + * scale_color_viridis_d() ``` ] .panel2-scatter_viridis-auto[ ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` <img src="visu_files/figure-html/scatter_viridis_auto_02_output-1.png" width="50%" style="display: block; margin: auto;" /> ] <style> .panel1-scatter_viridis-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-scatter_viridis-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-scatter_viridis-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- template: color ## with a color tribute to [Wes Anderson](https://en.wikipedia.org/wiki/Wes_Anderson) Following a Tumblr blog [Wes Anderson Palettes](https://wesandersonpalettes.tumblr.com/), Karthik Ram proposes the [wesanderson palette](https://github.com/karthik/wesanderson) on Github. ```r ## remotes::install_github("wesanderson") color_darj <- wesanderson::wes_palette(name = "Darjeeling1") ``` ```r gg + scale_color_manual(values = color_darj) ``` --- count: false .panel1-scatter_wesanderson-auto[ ```r *gg ``` ] .panel2-scatter_wesanderson-auto[ ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` <img src="visu_files/figure-html/scatter_wesanderson_auto_01_output-1.png" width="50%" style="display: block; margin: auto;" /> ] --- count: false .panel1-scatter_wesanderson-auto[ ```r gg + * scale_color_manual(values = color_darj) ``` ] .panel2-scatter_wesanderson-auto[ ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` <img src="visu_files/figure-html/scatter_wesanderson_auto_02_output-1.png" width="50%" style="display: block; margin: auto;" /> ] <style> .panel1-scatter_wesanderson-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-scatter_wesanderson-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-scatter_wesanderson-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- template: color ## Exercice * Proposer un graphique pour visulaliser le relation entre longueur d'ailes et poids du corps. * Illustrer également un potentiel effet de l'année. -- ```r penguins %>% ggplot() + aes(x= bm, y = flip_l) + geom_point() + aes(col = as_factor(year)) + scale_color_manual(values = wesanderson::wes_palette(name = "Darjeeling1")) ``` --- count: false .panel1-exo_relation_cor-auto[ ```r *penguins %>% ggplot() ``` ] .panel2-exo_relation_cor-auto[ <img src="visu_files/figure-html/exo_relation_cor_auto_01_output-1.png" width="50%" style="display: block; margin: auto;" /> ] --- count: false .panel1-exo_relation_cor-auto[ ```r penguins %>% ggplot() + * aes(x= bm, y = flip_l) ``` ] .panel2-exo_relation_cor-auto[ <img src="visu_files/figure-html/exo_relation_cor_auto_02_output-1.png" width="50%" style="display: block; margin: auto;" /> ] --- count: false .panel1-exo_relation_cor-auto[ ```r penguins %>% ggplot() + aes(x= bm, y = flip_l) + * geom_point() ``` ] .panel2-exo_relation_cor-auto[ ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` <img src="visu_files/figure-html/exo_relation_cor_auto_03_output-1.png" width="50%" style="display: block; margin: auto;" /> ] --- count: false .panel1-exo_relation_cor-auto[ ```r penguins %>% ggplot() + aes(x= bm, y = flip_l) + geom_point() + * aes(col = as_factor(year)) ``` ] .panel2-exo_relation_cor-auto[ ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` <img src="visu_files/figure-html/exo_relation_cor_auto_04_output-1.png" width="50%" style="display: block; margin: auto;" /> ] --- count: false .panel1-exo_relation_cor-auto[ ```r penguins %>% ggplot() + aes(x= bm, y = flip_l) + geom_point() + aes(col = as_factor(year)) + * scale_color_manual(values = wesanderson::wes_palette(name = "Darjeeling1")) ``` ] .panel2-exo_relation_cor-auto[ ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` <img src="visu_files/figure-html/exo_relation_cor_auto_05_output-1.png" width="50%" style="display: block; margin: auto;" /> ] --- count: false .panel1-exo_relation_cor-auto[ ```r penguins %>% ggplot() + aes(x= bm, y = flip_l) + geom_point() + aes(col = as_factor(year)) + scale_color_manual(values = wesanderson::wes_palette(name = "Darjeeling1")) ``` ] .panel2-exo_relation_cor-auto[ ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` <img src="visu_files/figure-html/exo_relation_cor_auto_06_output-1.png" width="50%" style="display: block; margin: auto;" /> ] <style> .panel1-exo_relation_cor-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-exo_relation_cor-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-exo_relation_cor-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- name: pimp # Pimp my graph --- template: pimp ## Some nice color palette - [Inspired by Ghibli](https://github.com/ewenme/ghibli) - [The yarr pirate palette](https://cran.r-project.org/web/packages/yarrr/vignettes/piratepal.html) - [Harry Potter inspiration](https://github.com/aljrico/harrypotter)![](https://github.com/aljrico/harrypotter/blob/master/man/figures/logo.png) --- template: pimp ## Des annotations choisies ```r gg <- gg + scale_color_manual(values = color_darj) ``` --- count: false .panel1-scatter_labels-auto[ ```r *gg ``` ] .panel2-scatter_labels-auto[ ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` <img src="visu_files/figure-html/scatter_labels_auto_01_output-1.png" width="50%" style="display: block; margin: auto;" /> ] --- count: false .panel1-scatter_labels-auto[ ```r gg + * labs( x = 'Bill length in mm') ``` ] .panel2-scatter_labels-auto[ ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` <img src="visu_files/figure-html/scatter_labels_auto_02_output-1.png" width="50%" style="display: block; margin: auto;" /> ] --- count: false .panel1-scatter_labels-auto[ ```r gg + labs( x = 'Bill length in mm') + * labs(y = 'Bill depth in mm') ``` ] .panel2-scatter_labels-auto[ ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` <img src="visu_files/figure-html/scatter_labels_auto_03_output-1.png" width="50%" style="display: block; margin: auto;" /> ] --- count: false .panel1-scatter_labels-auto[ ```r gg + labs( x = 'Bill length in mm') + labs(y = 'Bill depth in mm') + * labs(color = "Species") ``` ] .panel2-scatter_labels-auto[ ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` <img src="visu_files/figure-html/scatter_labels_auto_04_output-1.png" width="50%" style="display: block; margin: auto;" /> ] <style> .panel1-scatter_labels-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-scatter_labels-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-scatter_labels-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- template: pimp ## Choisir le theme ```r gg + labs( x = 'Bill length in mm') + labs(y = 'Bill depth in mm') + labs(color = "Species") + #BREAK theme_light() #BREAK ``` --- count: false .panel1-scatter_themelight-user[ ```r *gg + * labs( x = 'Bill length in mm') + * labs(y = 'Bill depth in mm') + * labs(color = "Species") ``` ] .panel2-scatter_themelight-user[ ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` <img src="visu_files/figure-html/scatter_themelight_user_01_output-1.png" width="50%" style="display: block; margin: auto;" /> ] --- count: false .panel1-scatter_themelight-user[ ```r gg + labs( x = 'Bill length in mm') + labs(y = 'Bill depth in mm') + labs(color = "Species") + * theme_light() ``` ] .panel2-scatter_themelight-user[ ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` <img src="visu_files/figure-html/scatter_themelight_user_02_output-1.png" width="50%" style="display: block; margin: auto;" /> ] <style> .panel1-scatter_themelight-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-scatter_themelight-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-scatter_themelight-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- template: scatter_label count: false --- template: pimp ## Choisir le theme ```r gg + scale_color_manual(values = color_darj) + labs( x = 'Bill length in mm', y = 'Bill depth in mm', color = "Species") + theme_light() + #BREAK theme_minimal() #BREAK ``` --- count: false .panel1-scatter_thememinimal-user[ ```r *gg + scale_color_manual(values = color_darj) + * labs( x = 'Bill length in mm', * y = 'Bill depth in mm', color = "Species") + * theme_light() ``` ] .panel2-scatter_thememinimal-user[ ``` ## Scale for 'colour' is already present. Adding another scale for 'colour', ## which will replace the existing scale. ``` ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` <img src="visu_files/figure-html/scatter_thememinimal_user_01_output-1.png" width="50%" style="display: block; margin: auto;" /> ] --- count: false .panel1-scatter_thememinimal-user[ ```r gg + scale_color_manual(values = color_darj) + labs( x = 'Bill length in mm', y = 'Bill depth in mm', color = "Species") + theme_light() + * theme_minimal() ``` ] .panel2-scatter_thememinimal-user[ ``` ## Scale for 'colour' is already present. Adding another scale for 'colour', ## which will replace the existing scale. ``` ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` <img src="visu_files/figure-html/scatter_thememinimal_user_02_output-1.png" width="50%" style="display: block; margin: auto;" /> ] <style> .panel1-scatter_thememinimal-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-scatter_thememinimal-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-scatter_thememinimal-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- template: pimp ## Choisir le theme Si on est heureux de notre graphique, on le garde ```r gg <- gg + scale_color_manual(values = color_darj) + labs( x = 'Bill length in mm', y = 'Bill depth in mm', color = "Species") + theme_light() ``` ``` ## Scale for 'colour' is already present. Adding another scale for 'colour', ## which will replace the existing scale. ``` --- template: pimp ## Choisir le theme The default theme might not be the best option ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` <img src="visu_files/figure-html/preset_theme-1.png" width="30%" height="50%" style="display: block; margin: auto;" /> --- count: false .panel1-scatter_below-auto[ ```r *gg ``` ] .panel2-scatter_below-auto[ ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` <img src="visu_files/figure-html/scatter_below_auto_01_output-1.png" width="50%" style="display: block; margin: auto;" /> ] --- count: false .panel1-scatter_below-auto[ ```r gg + * theme(legend.position="bottom") ``` ] .panel2-scatter_below-auto[ ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` <img src="visu_files/figure-html/scatter_below_auto_02_output-1.png" width="50%" style="display: block; margin: auto;" /> ] <style> .panel1-scatter_below-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-scatter_below-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-scatter_below-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- template: pimp ## Choisir le theme The legend position can also be specified by a vector ` c(x,y)`. Their values should be between 0 and 1. c(0,0) corresponds to the “bottom left” and c(1,1) corresponds to the “top right” position. We may want to change the size. ```r gg + theme(legend.position=c(.9, .6)) ``` ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` <img src="visu_files/figure-html/scatter_within-1.png" width="30%" height="50%" style="display: block; margin: auto;" /> --- template: pimp ## Choisir le theme ```r gg + theme(legend.position=c(.9, .6), text = element_text(size = 10, face = "italic"), axis.text.x = element_text(angle=90, hjust=1), legend.text = element_text(size = 9, face = 'plain'), legend.title = element_text(size = 11, face = "bold") ) ``` ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` <img src="visu_files/figure-html/scatter_custom-1.png" width="50%" style="display: block; margin: auto;" /> --- template: pimp ## Choisir le theme par défaut --- count: false .panel1-theme_setup_2-user[ ```r *theme_set(theme_light()) *theme_update(legend.position="bottom") ``` ] .panel2-theme_setup_2-user[ ] --- count: false .panel1-theme_setup_2-user[ ```r theme_set(theme_light()) theme_update(legend.position="bottom") *penguins %>% * ggplot() + * aes( x= bill_l, y=bill_d, col = species) + * geom_point() ``` ] .panel2-theme_setup_2-user[ ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` <img src="visu_files/figure-html/theme_setup_2_user_02_output-1.png" width="50%" style="display: block; margin: auto;" /> ] <style> .panel1-theme_setup_2-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-theme_setup_2-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-theme_setup_2-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- name: stat # Ajouter des résumés statistiques --- template: stat ## pour illustrer la relation entre 2 variables --- --- count: false .panel1-stat_info_lm-auto[ ```r *gg ``` ] .panel2-stat_info_lm-auto[ ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` <img src="visu_files/figure-html/stat_info_lm_auto_01_output-1.png" width="50%" style="display: block; margin: auto;" /> ] --- count: false .panel1-stat_info_lm-auto[ ```r gg + * geom_smooth(method = 'lm', se = FALSE) ``` ] .panel2-stat_info_lm-auto[ ``` ## `geom_smooth()` using formula 'y ~ x' ``` ``` ## Warning: Removed 2 rows containing non-finite values (stat_smooth). ``` ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` <img src="visu_files/figure-html/stat_info_lm_auto_02_output-1.png" width="50%" style="display: block; margin: auto;" /> ] --- count: false .panel1-stat_info_lm-auto[ ```r gg + geom_smooth(method = 'lm', se = FALSE) + * geom_smooth(method = 'loess', se = TRUE) ``` ] .panel2-stat_info_lm-auto[ ``` ## `geom_smooth()` using formula 'y ~ x' ``` ``` ## Warning: Removed 2 rows containing non-finite values (stat_smooth). ``` ``` ## `geom_smooth()` using formula 'y ~ x' ``` ``` ## Warning: Removed 2 rows containing non-finite values (stat_smooth). ``` ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` <img src="visu_files/figure-html/stat_info_lm_auto_03_output-1.png" width="50%" style="display: block; margin: auto;" /> ] <style> .panel1-stat_info_lm-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-stat_info_lm-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-stat_info_lm-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- template: stat ## Exercice * Proposer un joli graphique pour visualiser la relation entre la longueur de l'aile et le poids. * Illustrer les changements potentiels au cours du temps ? * En utilisant de belles couleurs * en utilisant la palette `Zissou1` du package `wesanderson`. -- ```r penguins %>% ggplot() + aes(x= bm, y = flip_l, col = as_factor(year) ) + geom_point() + geom_smooth(method = 'lm') + scale_color_manual(values = wesanderson::wes_palette(name = "Darjeeling1")) ``` <img src="visu_files/figure-html/exo_relation_cor2-1.png" width="30%" height="50%" style="display: block; margin: auto;" /> --- name: autres # de nombreuses autres possibilités à explorer --- template: autres ## Comparing plots --- count: false .panel1-color_hist_facet-auto[ ```r *penguins ``` ] .panel2-color_hist_facet-auto[ ``` ## # A tibble: 344 x 8 ## species island bill_l bill_d flip_l bm sex year ## <fct> <fct> <dbl> <dbl> <int> <int> <fct> <int> ## 1 Adelie Torgersen 39.1 18.7 181 3750 male 2007 ## 2 Adelie Torgersen 39.5 17.4 186 3800 female 2007 ## 3 Adelie Torgersen 40.3 18 195 3250 female 2007 ## 4 Adelie Torgersen NA NA NA NA <NA> 2007 ## 5 Adelie Torgersen 36.7 19.3 193 3450 female 2007 ## 6 Adelie Torgersen 39.3 20.6 190 3650 male 2007 ## 7 Adelie Torgersen 38.9 17.8 181 3625 female 2007 ## 8 Adelie Torgersen 39.2 19.6 195 4675 male 2007 ## 9 Adelie Torgersen 34.1 18.1 193 3475 <NA> 2007 ## 10 Adelie Torgersen 42 20.2 190 4250 <NA> 2007 ## # … with 334 more rows ``` ] --- count: false .panel1-color_hist_facet-auto[ ```r penguins %>% * ggplot() + aes(x = bill_l, fill = species) ``` ] .panel2-color_hist_facet-auto[ <img src="visu_files/figure-html/color_hist_facet_auto_02_output-1.png" width="50%" style="display: block; margin: auto;" /> ] --- count: false .panel1-color_hist_facet-auto[ ```r penguins %>% ggplot() + aes(x = bill_l, fill = species) + * geom_histogram() ``` ] .panel2-color_hist_facet-auto[ ``` ## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. ``` ``` ## Warning: Removed 2 rows containing non-finite values (stat_bin). ``` <img src="visu_files/figure-html/color_hist_facet_auto_03_output-1.png" width="50%" style="display: block; margin: auto;" /> ] --- count: false .panel1-color_hist_facet-auto[ ```r penguins %>% ggplot() + aes(x = bill_l, fill = species) + geom_histogram() + * facet_wrap(~species) ``` ] .panel2-color_hist_facet-auto[ ``` ## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. ``` ``` ## Warning: Removed 2 rows containing non-finite values (stat_bin). ``` <img src="visu_files/figure-html/color_hist_facet_auto_04_output-1.png" width="50%" style="display: block; margin: auto;" /> ] --- count: false .panel1-color_hist_facet-auto[ ```r penguins %>% ggplot() + aes(x = bill_l, fill = species) + geom_histogram() + facet_wrap(~species) + * labs( x = 'Bill length in mm') ``` ] .panel2-color_hist_facet-auto[ ``` ## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. ``` ``` ## Warning: Removed 2 rows containing non-finite values (stat_bin). ``` <img src="visu_files/figure-html/color_hist_facet_auto_05_output-1.png" width="50%" style="display: block; margin: auto;" /> ] --- count: false .panel1-color_hist_facet-auto[ ```r penguins %>% ggplot() + aes(x = bill_l, fill = species) + geom_histogram() + facet_wrap(~species) + labs( x = 'Bill length in mm') + * scale_fill_manual(values = wesanderson::wes_palette('Darjeeling1')) ``` ] .panel2-color_hist_facet-auto[ ``` ## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. ``` ``` ## Warning: Removed 2 rows containing non-finite values (stat_bin). ``` <img src="visu_files/figure-html/color_hist_facet_auto_06_output-1.png" width="50%" style="display: block; margin: auto;" /> ] <style> .panel1-color_hist_facet-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-color_hist_facet-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-color_hist_facet-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- template: autres ## Comparing plots - exercise Change the previous graph to add a fitted density line --- count: false .panel1-color_hist_dens_cor-user[ ```r *penguins %>% ggplot() + aes(x = bill_l, y = ..density..) + * facet_wrap(~species) + * geom_histogram(alpha=0.5, aes( fill = species)) + * geom_density(aes(col = species)) + * labs( x = 'Bill length in mm') ``` ] .panel2-color_hist_dens_cor-user[ ``` ## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. ``` ``` ## Warning: Removed 2 rows containing non-finite values (stat_bin). ``` ``` ## Warning: Removed 2 rows containing non-finite values (stat_density). ``` <img src="visu_files/figure-html/color_hist_dens_cor_user_01_output-1.png" width="50%" style="display: block; margin: auto;" /> ] --- count: false .panel1-color_hist_dens_cor-user[ ```r penguins %>% ggplot() + aes(x = bill_l, y = ..density..) + facet_wrap(~species) + geom_histogram(alpha=0.5, aes( fill = species)) + geom_density(aes(col = species)) + labs( x = 'Bill length in mm') + * scale_fill_manual(values = wesanderson::wes_palette('Darjeeling1')) ``` ] .panel2-color_hist_dens_cor-user[ ``` ## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. ``` ``` ## Warning: Removed 2 rows containing non-finite values (stat_bin). ``` ``` ## Warning: Removed 2 rows containing non-finite values (stat_density). ``` <img src="visu_files/figure-html/color_hist_dens_cor_user_02_output-1.png" width="50%" style="display: block; margin: auto;" /> ] --- count: false .panel1-color_hist_dens_cor-user[ ```r penguins %>% ggplot() + aes(x = bill_l, y = ..density..) + facet_wrap(~species) + geom_histogram(alpha=0.5, aes( fill = species)) + geom_density(aes(col = species)) + labs( x = 'Bill length in mm') + scale_fill_manual(values = wesanderson::wes_palette('Darjeeling1')) + * scale_color_manual(values = wesanderson::wes_palette('Darjeeling1')) ``` ] .panel2-color_hist_dens_cor-user[ ``` ## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. ``` ``` ## Warning: Removed 2 rows containing non-finite values (stat_bin). ``` ``` ## Warning: Removed 2 rows containing non-finite values (stat_density). ``` <img src="visu_files/figure-html/color_hist_dens_cor_user_03_output-1.png" width="50%" style="display: block; margin: auto;" /> ] <style> .panel1-color_hist_dens_cor-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-color_hist_dens_cor-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-color_hist_dens_cor-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- template: autres ## des boxplots originaux ```r penguins %>% ggplot() + aes(x = species, y = bill_l) + geom_violin(alpha=0.5, aes( fill = species)) + labs( y = 'Bill length in mm') + #BREAK scale_fill_manual(values = wesanderson::wes_palette('Darjeeling1')) ``` --- count: false .panel1-color_boxplot_2-user[ ```r *penguins %>% ggplot() + aes(x = species, y = bill_l) + * geom_violin(alpha=0.5, aes( fill = species)) + * labs( y = 'Bill length in mm') ``` ] .panel2-color_boxplot_2-user[ ``` ## Warning: Removed 2 rows containing non-finite values (stat_ydensity). ``` <img src="visu_files/figure-html/color_boxplot_2_user_01_output-1.png" width="50%" style="display: block; margin: auto;" /> ] --- count: false .panel1-color_boxplot_2-user[ ```r penguins %>% ggplot() + aes(x = species, y = bill_l) + geom_violin(alpha=0.5, aes( fill = species)) + labs( y = 'Bill length in mm') + * scale_fill_manual(values = wesanderson::wes_palette('Darjeeling1')) ``` ] .panel2-color_boxplot_2-user[ ``` ## Warning: Removed 2 rows containing non-finite values (stat_ydensity). ``` <img src="visu_files/figure-html/color_boxplot_2_user_02_output-1.png" width="50%" style="display: block; margin: auto;" /> ] <style> .panel1-color_boxplot_2-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-color_boxplot_2-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-color_boxplot_2-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- --- template: autres ## des boxplots originaux, qui font apparaître les valeurs ```r penguins %>% ggplot() + aes(x = species, y = bill_l) + geom_boxplot(alpha=0.5, aes( fill = species)) + geom_jitter(color="black", size=0.4, alpha=0.8) + labs( y = 'Bill length in mm') + #BREAK scale_fill_manual(values = wesanderson::wes_palette('Darjeeling1')) ``` --- count: false .panel1-color_boxplot_3-user[ ```r *penguins %>% ggplot() + aes(x = species, y = bill_l) + * geom_boxplot(alpha=0.5, aes( fill = species)) + * geom_jitter(color="black", size=0.4, alpha=0.8) + * labs( y = 'Bill length in mm') ``` ] .panel2-color_boxplot_3-user[ ``` ## Warning: Removed 2 rows containing non-finite values (stat_boxplot). ``` ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` <img src="visu_files/figure-html/color_boxplot_3_user_01_output-1.png" width="50%" style="display: block; margin: auto;" /> ] --- count: false .panel1-color_boxplot_3-user[ ```r penguins %>% ggplot() + aes(x = species, y = bill_l) + geom_boxplot(alpha=0.5, aes( fill = species)) + geom_jitter(color="black", size=0.4, alpha=0.8) + labs( y = 'Bill length in mm') + * scale_fill_manual(values = wesanderson::wes_palette('Darjeeling1')) ``` ] .panel2-color_boxplot_3-user[ ``` ## Warning: Removed 2 rows containing non-finite values (stat_boxplot). ``` ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` <img src="visu_files/figure-html/color_boxplot_3_user_02_output-1.png" width="50%" style="display: block; margin: auto;" /> ] <style> .panel1-color_boxplot_3-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-color_boxplot_3-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-color_boxplot_3-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- template: autres ## Gérer les indices et les exposants ```r penguins %>% mutate(mu = bill_l * bill_d) %>% ggplot() + aes(y= mu ) + geom_boxplot(alpha=0.5, aes( fill = species)) + labs( y = bquote(mu~(mm^2))) + scale_fill_manual(values = wesanderson::wes_palette('Darjeeling1')) ``` ``` ## Warning: Removed 2 rows containing non-finite values (stat_boxplot). ``` <img src="visu_files/figure-html/color_boxplot_sup_sub-1.png" width="30%" height="50%" style="display: block; margin: auto;" /> --- name: useful_package # Useful packages for publication The `ggpubr` package is very helpful for publication. --- template: useful_package ```r gg_p1 <- gg gg_p1 ``` ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` <img src="visu_files/figure-html/ggpubr_p1-1.png" width="30%" height="50%" style="display: block; margin: auto;" /> --- template: useful_package ```r gg_p2 <- penguins %>% ggplot() + aes(x = bill_l, y = ..density..) + geom_histogram(alpha=0.5, aes( fill = species)) + geom_density(aes(col = species)) + labs( x = 'Bill length in mm') + scale_fill_manual(values = wesanderson::wes_palette('Darjeeling1')) + scale_color_manual(values = wesanderson::wes_palette('Darjeeling1')) gg_p2 ``` ``` ## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. ``` ``` ## Warning: Removed 2 rows containing non-finite values (stat_bin). ``` ``` ## Warning: Removed 2 rows containing non-finite values (stat_density). ``` <img src="visu_files/figure-html/ggpubr_p2-1.png" width="30%" height="50%" style="display: block; margin: auto;" /> --- template: useful_package ```r ##install.packages('ggpubr') ggpubr::ggarrange(gg_p1, gg_p2, nrow=2, ncol = 1) ``` ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` ``` ## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. ``` ``` ## Warning: Removed 2 rows containing non-finite values (stat_bin). ``` ``` ## Warning: Removed 2 rows containing non-finite values (stat_density). ``` <img src="visu_files/figure-html/ggpubr_p3-1.png" width="30%" height="50%" style="display: block; margin: auto;" /> --- template: useful_package ```r ggpubr::ggarrange(gg_p1, gg_p2, nrow=2, ncol = 1, common.legend = TRUE) ``` ``` ## Warning: Removed 2 rows containing missing values (geom_point). ## Warning: Removed 2 rows containing missing values (geom_point). ``` ``` ## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. ``` ``` ## Warning: Removed 2 rows containing non-finite values (stat_bin). ``` ``` ## Warning: Removed 2 rows containing non-finite values (stat_density). ``` <img src="visu_files/figure-html/ggpubr_p4-1.png" width="30%" height="50%" style="display: block; margin: auto;" /> --- name: bivariate_explore # Useful packages for publications/explorations The `GGally` package contains plenty of very useful and nice functions --- template: bivariate_explore count: false ```r ##install.packages('GGally') library(GGally) penguins %>% ggpairs(columns = c(1,3,4,5), mapping = aes(col = species)) + scale_color_manual(values = wesanderson::wes_palette('Darjeeling1'))+ scale_fill_manual(values = wesanderson::wes_palette('Darjeeling1')) + theme(text = element_text(size = 6)) ``` <img src="visu_files/figure-html/ggpairs-1.png" width="50%" height="30%" style="display: block; margin: auto;" /> --- template: bivariate_explore count: false ```r penguins %>% ggpairs(columns = c(1,3,4,5), mapping = aes(col = species), upper = list(continuous = wrap( "cor",size = 2)), lower = list(continuous = wrap('points', size = .5))) + scale_color_manual(values = wesanderson::wes_palette('Darjeeling1'))+ scale_fill_manual(values = wesanderson::wes_palette('Darjeeling1')) + theme(text = element_text(size = 6)) ``` <img src="visu_files/figure-html/ggpairs_fancy-1.png" width="50%" height="30%" style="display: block; margin: auto;" /> --- name: gganimate # Useful packages for presentation The `gganimate` package produces animated graph (html only). ```r library(gganimate) gg + transition_states(year) + geom_text(x = 56 , y = 15, aes(label = as.character(year)), size = 8, col = "grey50") + theme(legend.position="bottom") ``` --- name: plotly # Useful packages for online reporting The `plotly` package produces interactive plots (html only). ```r library(plotly) gg %>% ggplotly() ```
--- # Inspiring websites - [R Graph Gallery](https://www.r-graph-gallery.com/) - [Top50 ggplot Visualisation](http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html) --- # References Cornillon, P., A. Guyader, F. Husson, et al. (2018). _R pour la statistique et la science des données_. Presses universitaires de Rennes. Daudin, J. (2015). _Le modèle linéaire et ses extensions-Modèle linéaire général, modèle linéaire généralisé, modèle mixte, plans d'expériences (Niveau C)_. Edition Ellipses. Faraway, J. J. (2014). _Linear models with R_. CRC press. Hutcheon, J. M., J. A. W. Kirsch, and T. Garland (2002). "A Comparative Analysis of Brain Size in Relation to Foraging Ecology and Phylogeny in the Chiroptera". In: _Brain, Behavior and Evolution_ 60.3, pp. 165-180. ISSN: 1421-9743. DOI: [10.1159/000065938](https://doi.org/10.1159%2F000065938). URL: [http://dx.doi.org/10.1159/000065938](http://dx.doi.org/10.1159/000065938). R Core Team (2017). _R: A Language and Environment for Statistical Computing_. R Foundation for Statistical Computing. Vienna, Austria. URL: [https://www.R-project.org/](https://www.R-project.org/). Wickham, H. and G. Grolemund (2016). _R for data science: import, tidy, transform, visualize, and model data_. " O'Reilly Media, Inc.".