class: center middle main-title section-title-1 # Visualisation<br>avec `ggplot2` .class-info[ <figure> <img src="img/03/ggplot-logo.png" alt="ggplot" title="ggplot" width="15%"> </figure> ] <br> --- class: title title-1 # Voitures et cylindrée .box-inv-1.medium[Quelle est la relation entre la cylindrée d'une voiture <br>et son autonomie? ] --- class: title title-1 section-title-inv-1 # À votre tour #1 .box-1[Compilez le code du fichier Quarto pour créer un graphique] .box-1[Portez attention à l'orthographe, aux majuscules et aux parenthèses!] ```r library(ggplot2) ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) ```
−
+
02
:
00
--- ```r ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) ``` <img src="MATH60602-diapos2c-visualisation_files/figure-html/unnamed-chunk-3-1.png" width="60%" style="display: block; margin: auto;" /> --- layout: true class: title title-1 # La grammaire des graphiques .pull-left.center[ <figure> <img src="img/03/gg-book.jpg" alt="Grammar of Graphics book" title="Grammar of Graphics book" width="55%"> </figure> ] .pull-right[ .box-inv-1.medium[Composante esthétique] .box-1[Propriété visuelle d'un graphique] .box-1.sp-after[Position, forme, couleur, etc.] .box-inv-1.medium[Variables] .box-1[Une colonne d'une<br>base de données] ] # Les voitures avec une plus grande cylindrée parcourent plus de distance? <img src="MATH60602-diapos2c-visualisation_files/figure-html/unnamed-chunk-4-1.png" width="60%" style="display: block; margin: auto;" /> --- # Esthétique .pull-left-3[ .box-inv-1.small[`color` (discrète)] <img src="MATH60602-diapos2c-visualisation_files/figure-html/aes-color-discrete-1.png" width="100%" style="display: block; margin: auto;" /> .box-inv-1.small[`color` (continue)] <img src="MATH60602-diapos2c-visualisation_files/figure-html/aes-color-continuous-1.png" width="100%" style="display: block; margin: auto;" /> ] .pull-middle-3[ .box-inv-1.small[`size`] <img src="MATH60602-diapos2c-visualisation_files/figure-html/aes-size-1.png" width="100%" style="display: block; margin: auto;" /> .box-inv-1.small[`fill`] <img src="MATH60602-diapos2c-visualisation_files/figure-html/aes-fill-1.png" width="100%" style="display: block; margin: auto;" /> ] .pull-right-3[ .box-inv-1.small[`shape`] <img src="MATH60602-diapos2c-visualisation_files/figure-html/aes-shape-1.png" width="100%" style="display: block; margin: auto;" /> .box-inv-1.small[`alpha`] <img src="MATH60602-diapos2c-visualisation_files/figure-html/aes-alpha-1.png" width="100%" style="display: block; margin: auto;" /> ] --- # Associer colonnes et esthétiques .small[ ```r ggplot(mpg) + geom_point(aes(x = displ, y = hwy, color = class)) ggplot(mpg) + geom_point(aes(x = displ, y = hwy, size = class)) ggplot(mpg) + geom_point(aes(x = displ, y = hwy, shape = class)) ggplot(mpg) + geom_point(aes(x = displ, y = hwy, alpha = class)) ``` ] --- layout: false ```r ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, color = class)) ``` <img src="MATH60602-diapos2c-visualisation_files/figure-html/unnamed-chunk-6-1.png" width="60%" style="display: block; margin: auto;" /> --- class: title title-1 section-title-inv-1 # À votre tour #2 .box-1[Modifiez la couleur, la taille, la transparence et la forme des points de votre graphique.] .box-1[Expérimentez!] .box-1[Y a-t-il des différences selon qu'on associe une composante esthétique à des variables discrètes ou continues?] .box-1[Quid si on utilise plusieurs composantes esthétiques?] --- class: title title-1 # Comment créer ce graphique? <img src="MATH60602-diapos2c-visualisation_files/figure-html/unnamed-chunk-7-1.png" width="70%" style="display: block; margin: auto;" /> --- .left-code[ ```r ggplot(mpg) + geom_point(aes(x = displ, y = hwy, color = class)) ``` ] .right-plot[  ] --- .left-code[ ```r ggplot(mpg) + geom_point(aes(x = displ, y = hwy), color = "blue") ``` ] .right-plot[  ] --- .pull-left[ .small[ ```r ggplot(mpg) + geom_point(aes(x = displ, y = hwy, color = "blue")) ``` <img src="MATH60602-diapos2c-visualisation_files/figure-html/unnamed-chunk-8-1.png" width="100%" style="display: block; margin: auto;" /> ] ] .pull-right[ .small[ ```r ggplot(mpg) + geom_point(aes(x = displ, y = hwy), color = "blue") ``` <img src="MATH60602-diapos2c-visualisation_files/figure-html/unnamed-chunk-9-1.png" width="100%" style="display: block; margin: auto;" /> ] ] --- layout: true class: title title-1 --- # Similarités et différences? .pull-left[ <img src="MATH60602-diapos2c-visualisation_files/figure-html/unnamed-chunk-10-1.png" width="100%" style="display: block; margin: auto;" /> ] .pull-right[ <img src="MATH60602-diapos2c-visualisation_files/figure-html/unnamed-chunk-11-1.png" width="100%" style="display: block; margin: auto;" /> ] --- # Géométries <code class ='r hljs remark-code'>ggplot(data = <b><span style="background-color:#CBB5FF">DONNÉES</span></b>) +<br> <b><span style="background-color:#FFDFD1">FONCTION_GEOM</span></b>(mapping = aes(APPLICATION: VARIABLES > COMPOSANTESESTHÉTIQUE ))</code> --- # Géométries <table> <tr> <th class="cell-left"></th> <th class="cell-left">Exemple de géométrie</th> <th class="cell-left">Sortie</th> </tr> <tr> <td class="cell-left"><img src="img/03/geom_bar.png"></td> <td class="cell-left"><code class="remark-inline-code">geom_bar()</code></td> <td class="cell-left">Diagramme à bande</td> </tr> <tr> <td class="cell-left"><img src="img/03/geom_text.png"></td> <td class="cell-left"><code class="remark-inline-code">geom_text()</code></td> <td class="cell-left">Texte</td> </tr> <tr> <td class="cell-left"><img src="img/03/geom_point.png"></td> <td class="cell-left"><code class="remark-inline-code">geom_point()</code></td> <td class="cell-left">Nuage de points</td> </tr> <tr> <td class="cell-left"><img src="img/03/geom_boxplot.png"></td> <td class="cell-left"><code class="remark-inline-code">geom_boxplot()</code> </td> <td class="cell-left">Boîte à moustaches</td> </tr> </table> --- # Inventaire de géométries .box-inv-1[Beaucoup de géométries disponibles!] .box-1[Voir [la documentation `ggplot2`](https://ggplot2.tidyverse.org/reference/index.html#section-layer-geoms) pour<br>un exemple exhaustif avec toutes les géométries.] .box-1[Ou la feuille aide-mémoire `ggplot2`] --- layout: false class: title title-1 section-title-inv-1 # À votre tour #3 .box-1[Remplacez le nuage de point par des boîtes à moustache. Utilisez l'aide mémoire.] .pull-left[ <img src="MATH60602-diapos2c-visualisation_files/figure-html/unnamed-chunk-12-1.png" width="100%" style="display: block; margin: auto;" /> ] .pull-right[ <img src="MATH60602-diapos2c-visualisation_files/figure-html/unnamed-chunk-13-1.png" width="100%" style="display: block; margin: auto;" /> ]
−
+
01
:
30
--- class: title title-1 section-title-inv-1 # À votre tour #4 .box-1[Produisez un histogramme de `hwy`. Utilisez l'aide mémoire.<br>Indice: ne pas fournir de variable `y`.] <img src="MATH60602-diapos2c-visualisation_files/figure-html/unnamed-chunk-15-1.png" width="70%" style="display: block; margin: auto;" /> --- class: title title-1 section-title-inv-1 # À votre tour #5 .box-1[Colorez la densité de `hwy` par `class`.<br>Utilisez l'aide mémoire.<br> Indice: ne pas fournir de variable `y`.] <img src="MATH60602-diapos2c-visualisation_files/figure-html/unnamed-chunk-16-1.png" width="70%" style="display: block; margin: auto;" /> --- class: title title-1 # Graphiques complexes! <img src="MATH60602-diapos2c-visualisation_files/figure-html/unnamed-chunk-17-1.png" width="70%" style="display: block; margin: auto;" /> --- class: title title-1 section-title-inv-1 # À votre tour #6 .box-1[Prédisez ce que le code produit, puis compilez.] ```r ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) + geom_smooth(mapping = aes(x = displ, y = hwy)) ```
−
+
02
:
00
--- class: title title-1 # Global vs. local .box-inv-1[Toute composante esthétique dans `ggplot()`<br> est utilisée dans toutes les strates `geom_`] .small[ ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point() + geom_smooth() ``` <img src="MATH60602-diapos2c-visualisation_files/figure-html/unnamed-chunk-20-1.png" width="60%" style="display: block; margin: auto;" /> ] --- class: title title-1 # Global vs. local .box-inv-1[Toute composante esthétique fournie dans la strate `geom_`<br> s'applique uniquement à cette dernière] .small[ ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = drv)) + geom_smooth() ``` <img src="MATH60602-diapos2c-visualisation_files/figure-html/unnamed-chunk-21-1.png" width="60%" style="display: block; margin: auto;" /> ] --- layout: true class: title title-1 --- # Bien plus .pull-left[ .box-inv-1[Plusieurs strates pour améliorer l'aspect visuel!] .box-inv-1[On ajoute des strates séquentiellement pour créer des figures complexes] ] .pull-right[  ] --- # Mettre tout ensemble .box-inv-1.medium[On construit le graphique étape par étape<br>pour voir l'impact des strates<br>sur l'apparence.] --- layout: false .left-code[ .box-1[Données et composantes esthétiques globales] ```r *ggplot(data = mpg, * mapping = aes(x = displ, * y = hwy, * color = drv)) ``` ] .right-plot[  ] --- .left-code[ .box-1[Ajouter une strate géométrique (nuage de points)] ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + * geom_point() ``` ] .right-plot[  ] --- .left-code[ .box-1[Ajouter une composante de lissage] ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + geom_point() + * geom_smooth() ``` ] .right-plot[  ] --- .left-code[ .box-1[Changer la droite de lissage par une régression linéaire] ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + geom_point() + * geom_smooth(method = "lm") ``` ] .right-plot[  ] --- .left-code[ .box-1[Choisir la palette de couleur] ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + geom_point() + geom_smooth(method = "lm") + * scale_color_viridis_d() ``` ] .right-plot[  ] --- .left-code[ .box-1[Séparer en panneaux] ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + geom_point() + geom_smooth(method = "lm") + scale_color_viridis_d() + * facet_wrap(vars(drv), ncol = 1) ``` ] .right-plot[  ] --- .left-code[ .box-1[Ajouter des étiquettes] ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + geom_point() + geom_smooth(method = "lm") + scale_color_viridis_d() + facet_wrap(vars(drv), ncol = 1) + * labs(x = "cylindrée (litres)", * y = "", * subtitle = "autonomie sur l'autoroute (mile au gallon)", * color = "type", * title = "Cylindrée imposante rime avec moins d'autonomie.", * caption = "Je connais rien aux voitures") ``` ] .right-plot[  ] --- .left-code[ .box-1[Ajouter un thème] ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + geom_point() + geom_smooth(method = "lm") + scale_color_viridis_d() + facet_wrap(vars(drv), ncol = 1) + labs(x = "cylindrée (litres)", y = "", subtitle = "autonomie sur l'autoroute (mile au gallon)", color = "type", title = "Cylindrée imposante rime avec moins d'autonomie.", caption = "Je connais rien aux voitures") + * theme_bw() ``` ] .right-plot[  ] --- .left-code[ .box-1[Modifier le thème] ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + geom_point() + geom_smooth(method = "lm") + scale_color_viridis_d() + facet_wrap(vars(drv), ncol = 1) + labs(x = "cylindrée (litres)", y = "", subtitle = "autonomie sur l'autoroute (mile au gallon)", color = "type", title = "Cylindrée imposante rime avec moins d'autonomie.", caption = "Je connais rien aux voitures") + theme_bw() + * theme(legend.position = "bottom") ``` ] .right-plot[  ] --- .left-code[ .box-1[Terminé!] ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + geom_point() + geom_smooth(method = "lm") + scale_color_viridis_d() + facet_wrap(vars(drv), ncol = 1) + labs(x = "cylindrée (litres)", y = "", subtitle = "autonomie sur l'autoroute (mile au gallon)", color = "type", title = "Cylindrée imposante rime avec moins d'autonomie.", caption = "Je connais rien aux voitures") + theme_bw() + theme(legend.position = "bottom") ``` ] .right-plot[  ]