class: center, middle, inverse, title-slide # Grafiken mit ggplot2
The Grammar of Graphics Plot ## UDE Workshop 02.10.2019 ### Regina Weber
regina.weber@hochschule-rhein-waal.de
### Based on:
https://github.com/gadenbuie/gentle-ggplot2
--- # Ablaufplan Vorgeschlagener Ablaufplan | Zeit | Thema | | --- | --- | --- | --- | | 9.30h-10.00h | Einführung, Tagesablauf <br> Vorstellung aller TN undErwartungen | | 10.00-10.55h | Grundlagen der Grafikgestaltung <br>Einführung in ggplot2 – Logik und Grundlagen | | 11.00-12.30h | Praktische Übungen: Grafiken in ggplot2 | | *12.30-13.30h* | *Mittagspause* | | 13.30-14.25h | Komplexe Grafiken in ggplot2 – Grundlagen und Beispiele | | 14.30-16.00h | Praktische Übungen, komplexe Grundlagen | --- layout: false class: inverse center middle text-white .font200[Grundlagen der Grafikgestaltung ] --- layout: true # Grundlagen der Grafikgestaltung --- ## Warum nutzen wir Grafiken? .left-column[ ![](images/grafik.jpg) ] .right-column[ - Veranschaulichung/Darstellung eines Sachverhalts "auf einen Blick". - Hinweis/Betonung bestimmter Tendenzen (Regressionslinie) - Zusammenfassung komplexer Informationen ] -- .right-column[ **Grafiken haben tendenziösen Charakter und sollten daher überlegt gewählt werden.** Link: [Ein kleines Beispiel zu den Mitgliederzahlen der Piratenpartei](https://flaschenpost.piratenpartei.de/2012/11/05/zahlen-lugen-nicht-diagramme-konnen-tauschen/). ] --- ## Wie erstellen wir Grafiken? .left-column[ ![](images/glz049.jpg) ] .right-column[ Vier Variablen müssen berücksichtigt werden: 1. Absicht 2. Zielgruppe 3. Leseumstände (Paper? Buch? Präsentation?) 4. Typografie (Schriftführung), Formen und Farbgestaltung .hl[Während 1-3 i.d.R. gut beantwortbar sind, gibt es zu 4. ein paar (Wissenschaftler*innen oft unbekannte) fachliche Grundlagen.] ] --- ## Farben .left-column[<img src="images/goethe.jpg" style="height: 350px" /> .footnote[Goethes Farbenkreis] ] .right-column[Farben transportieren Emotionen und wecken Assoziationen. Wichtig für Grafiken sind aber besonders Farbkontraste und die Komplementärfarbe. - Gegenüberliegende Farben bilden starke Kontraste - Nebeneinanderliegende Farben eignen sich für Skalen ] -- .right-column[ ### Farben in R - Webseite .hl[ColorBrewer]: http://colorbrewer2.org/ - Package .hl[RColorBrewer] ] --- ## Schrift Drei unterschiedliche Schrifttypen: .font80[ <figure> <img src="images/Antiqua1.png" style="height: 110px"> <img src="images/Fraktur1.png" style="height: 110px"> <img src="images/Grotesk1.png" style="height: 110px"> <figcaption>Antiqua (Serif), Fraktur und Groteskschrift (Sans-Serif).</figcaption> </figure>] -- #### Positives Schriftbild - wenig Schriftarten (zwei, max. drei: Überschriften, Textteile, ggf. Beschriftungen), dies gilt auch für Grafikbeschriftungen! - Nutzung von *kursiv*, **fett**, und Schriftgröße um das Schriftbild zu strukturieren --- ## Literatur Gut zugängliche Literatur zum Thema <div align="center"> <img src="images/Runk.png" style="height: 380px"> <img src="images/Koschembar.png" style="height: 380px"> </div> --- layout: true --- layout: false class: inverse center middle text-white .font200[Warum *ggplot2*?] Adopted from [A Gentle Guide to the Grammar of Graphics with ggplot2](https://github.com/gadenbuie/gentle-ggplot2) by Garrick Aden-Buie --- layout: true # Warum *ggplot2*? --- .left-column[ ![](images/Wickley.jpg) ] .right-column[.font90[ "...because there is a simple set of core principles and very few special cases, ggplot2 is also easy to learn (although it may take a little time to forget your preconceptions from other graphics tools)."] ] .footnote[Wickham, S.1.] -- .right-column[ .hl[Idee]: Grafiken werden Stück für Stück zusammengesetzt .hl[Ziel]: Maximale Flexibilität. Zentrale Prinzipien: 1. Datenbasierte Herangehensweise (`tidy data` basiert) 2. Daten werden visuellen Repräsentationen zugeordnet 3. Manuelle Definition von Skalen, Farben, Formen und Gruppen. ] --- ## Installation Installieren des [tidyverse](http://tidyverse.org) ```r install.packages('tidyverse') ``` Tidyverse laden ```r library(tidyverse) ``` ``` ## -- Attaching packages ----------------------------------------------------------------------------- tidyverse 1.2.1 -- ``` ``` ## v ggplot2 3.2.1 v purrr 0.3.2 ## v tibble 2.1.3 v dplyr 0.8.3 ## v tidyr 0.8.3 v stringr 1.4.0 ## v readr 1.3.1 v forcats 0.4.0 ``` ``` ## -- Conflicts -------------------------------------------------------------------------------- tidyverse_conflicts() -- ## x dplyr::filter() masks stats::filter() ## x dplyr::lag() masks stats::lag() ``` --- ## Daten für den heutigen Tag [mpg](https://ggplot2.tidyverse.org/reference/mpg.html)-Datenset, Teil des R-Basissystems. [gapminder](http://www.gapminder.org/data/)-Datenset aus dem [`gapminder` package](https://github.com/jennybc/gapminder) von Jenny Bryan. ```r ## install.packages("gapminder") library(gapminder) ``` --- layout: false class: inverse center middle text-white .font200[gg heißt <br>Grammar of Graphics] --- layout: true # gg heißt Grammar of Graphics .left-code[ ### Welche Daten liegen hinter dem Plot? ] --- .right-plot[ <img src="index_files/figure-html/guess-data-from-plot-2-1.png" width="100%" /> ] --- .right-plot[ <img src="index_files/figure-html/guess-data-from-plot-3-1.png" width="100%" /> ] --- .right-plot[ <img src="index_files/figure-html/guess-data-from-plot-1-1.png" width="100%" /> ] --- .right-plot[ <img src="index_files/figure-html/guess-data-from-plot-4-1.png" width="100%" /> ] --- .right-plot[ <img src="index_files/figure-html/guess-data-from-plot-5-1.png" width="100%" /> ] --- .right-plot[ <img src="index_files/figure-html/guess-data-from-plot-6-1.png" width="100%" /> ] --- layout:false .left-code[ ### Welche Daten liegen hinter dem Plot? **Spritverbrauch von Autos** - Manufacturer - Car Type (Class) - City MPG - Highway MPG ] .right-plot[ <table> <thead> <tr> <th style="text-align:left;"> manufacturer </th> <th style="text-align:left;"> class </th> <th style="text-align:right;"> cty </th> <th style="text-align:right;"> hwy </th> <th style="text-align:left;"> model </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> audi </td> <td style="text-align:left;"> compact </td> <td style="text-align:right;"> 18 </td> <td style="text-align:right;"> 27 </td> <td style="text-align:left;"> a4 </td> </tr> <tr> <td style="text-align:left;"> audi </td> <td style="text-align:left;"> compact </td> <td style="text-align:right;"> 19 </td> <td style="text-align:right;"> 27 </td> <td style="text-align:left;"> a4 quattro </td> </tr> <tr> <td style="text-align:left;"> ford </td> <td style="text-align:left;"> suv </td> <td style="text-align:right;"> 12 </td> <td style="text-align:right;"> 18 </td> <td style="text-align:left;"> expedition 2wd </td> </tr> <tr> <td style="text-align:left;"> ford </td> <td style="text-align:left;"> suv </td> <td style="text-align:right;"> 13 </td> <td style="text-align:right;"> 19 </td> <td style="text-align:left;"> explorer 4wd </td> </tr> <tr> <td style="text-align:left;"> toyota </td> <td style="text-align:left;"> suv </td> <td style="text-align:right;"> 16 </td> <td style="text-align:right;"> 20 </td> <td style="text-align:left;"> 4runner 4wd </td> </tr> <tr> <td style="text-align:left;"> toyota </td> <td style="text-align:left;"> compact </td> <td style="text-align:right;"> 18 </td> <td style="text-align:right;"> 27 </td> <td style="text-align:left;"> camry solara </td> </tr> <tr> <td style="text-align:left;"> toyota </td> <td style="text-align:left;"> compact </td> <td style="text-align:right;"> 28 </td> <td style="text-align:right;"> 37 </td> <td style="text-align:left;"> corolla </td> </tr> <tr> <td style="text-align:left;"> toyota </td> <td style="text-align:left;"> suv </td> <td style="text-align:right;"> 13 </td> <td style="text-align:right;"> 18 </td> <td style="text-align:left;"> land cruiser wagon 4wd </td> </tr> </tbody> </table> ] --- layout: false # Visuelle Elemente einer Grafik .font120[ - **.hlb[Data]** -- Daten im tidy data-Format ] -- .font120[ - **.hlb[Geom]etric objects**, die Daten repräsentieren ] -- .font120[ - **.hlb[Aes]thetic mappings** von den Daten zu visuellen Elementen ] -- .font120[ - **.hlb[Stat]istics** transformieren Daten vor der Visualisierung ] -- .font120[ - **.hlb[Coord]inates** legen die Lokalisierung der Objekte fest ] -- .font120[ - **.hlb[Scale]s** definieren das Verhältnis der Daten zueinander ] -- .font120[ - **.hlb[Facet]s** gruppieren die Daten in Untergruppen ] --- layout: true # gg heißt Grammar of Graphics .left-column[ ### Data ```r ggplot(data) ``` ] --- .right-column[ #### Tidy Data 1. Jede Variable bildet eine .hl[Spalte] 2. Jede Beobachtung bildet eine .hl[Zeile] 3. Jede Beobachtungseinheit bildet eine .hl[Tabelle] ] -- .right-column[ #### Vor jeder Grafikerstellung 1. Welche Informationen möchte ich visualisieren? 1. Sind diese Daten in .hl[einer Zeile/Spalte] für jeden Datenpunkt? ] --- .right-column[ Messy data <table> <thead> <tr> <th style="text-align:left;"> country </th> <th style="text-align:right;"> 1997 </th> <th style="text-align:right;"> 2002 </th> <th style="text-align:right;"> 2007 </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Canada </td> <td style="text-align:right;"> 30.30584 </td> <td style="text-align:right;"> 31.90227 </td> <td style="text-align:right;"> 33.39014 </td> </tr> <tr> <td style="text-align:left;"> China </td> <td style="text-align:right;"> 1230.07500 </td> <td style="text-align:right;"> 1280.40000 </td> <td style="text-align:right;"> 1318.68310 </td> </tr> <tr> <td style="text-align:left;"> United States </td> <td style="text-align:right;"> 272.91176 </td> <td style="text-align:right;"> 287.67553 </td> <td style="text-align:right;"> 301.13995 </td> </tr> </tbody> </table> ] --- .right-column[ Tidy data <table> <thead> <tr> <th style="text-align:left;"> country </th> <th style="text-align:left;"> year </th> <th style="text-align:right;"> pop </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Canada </td> <td style="text-align:left;"> 1997 </td> <td style="text-align:right;"> 30.30584 </td> </tr> <tr> <td style="text-align:left;"> China </td> <td style="text-align:left;"> 1997 </td> <td style="text-align:right;"> 1230.07500 </td> </tr> <tr> <td style="text-align:left;"> United States </td> <td style="text-align:left;"> 1997 </td> <td style="text-align:right;"> 272.91176 </td> </tr> <tr> <td style="text-align:left;"> Canada </td> <td style="text-align:left;"> 2002 </td> <td style="text-align:right;"> 31.90227 </td> </tr> <tr> <td style="text-align:left;"> China </td> <td style="text-align:left;"> 2002 </td> <td style="text-align:right;"> 1280.40000 </td> </tr> <tr> <td style="text-align:left;"> United States </td> <td style="text-align:left;"> 2002 </td> <td style="text-align:right;"> 287.67553 </td> </tr> </tbody> </table> ] --- layout: true # gg heißt Grammar of Graphics .left-column[ ### Data ### Aesthetics ```r + aes() ``` ] --- .right-column[ Daten werden visuellen Elementen bzw. Parametern zugeordnet - year → **x** - pop → **y** - country → *shape*, *color*, etc. ] -- .right-column[ ```r aes( x = year, y = pop, color = country ) ``` ] --- layout: true # gg heißt Grammar of Graphics .left-column[ ### Data ### Aesthetics ### Geoms ```r + geom_*() ``` ] --- .right-column[ Das geometrische Objekt wird festgelegt. <img src="index_files/figure-html/geom_demo-1.png" width="650px" /> ] --- .right-column[ Einige der [wichtigsten bzw. meistgenutzten geometrischen Objekte](https://eric.netlify.com/2017/08/10/most-popular-ggplot2-geoms/) .font70.center[ | Type | Function | |:----:|:--------:| | Point | `geom_point()` | | Line | `geom_line()` | | Bar | `geom_bar()`, `geom_col()` | | Histogram | `geom_histogram()` | | Regression | `geom_smooth()` | | Boxplot | `geom_boxplot()` | | Text | `geom_text()` | | Vert./Horiz. Line | `geom_{vh}line()` | | Count | `geom_count()` | | Density | `geom_density()` | <https://eric.netlify.com/2017/08/10/most-popular-ggplot2-geoms/> ] ] --- .right-column[ Es gibt aber noch mehr: <http://ggplot2.tidyverse.org/reference/> .font70[ ``` ## [1] "geom_abline" "geom_area" "geom_bar" "geom_bin2d" ## [5] "geom_blank" "geom_boxplot" "geom_col" "geom_contour" ## [9] "geom_count" "geom_crossbar" "geom_curve" "geom_density" ## [13] "geom_density_2d" "geom_density2d" "geom_dotplot" "geom_errorbar" ## [17] "geom_errorbarh" "geom_freqpoly" "geom_hex" "geom_histogram" ## [21] "geom_hline" "geom_jitter" "geom_label" "geom_line" ## [25] "geom_linerange" "geom_map" "geom_path" "geom_point" ## [29] "geom_pointrange" "geom_polygon" "geom_qq" "geom_qq_line" ## [33] "geom_quantile" "geom_raster" "geom_rect" "geom_ribbon" ## [37] "geom_rug" "geom_segment" "geom_sf" "geom_sf_label" ## [41] "geom_sf_text" "geom_smooth" "geom_spoke" "geom_step" ## [45] "geom_text" "geom_tile" "geom_violin" "geom_vline" ``` ] ] -- .right-column[ <img src="images/geom.gif" width="200px" style="float: right; margin-right: 100px; margin-top: -25px;"> In RStudio `geom_` eintippen und: ] --- layout: true # Eine erste, einfache Grafik --- .left-code[ ```r ggplot(tidy_pop) ``` ] .right-plot[ <img src="index_files/figure-html/first-plot1a-out-1.png" width="100%" /> ] --- .left-code[ ```r ggplot(tidy_pop) + * aes(x = year, * y = pop) ``` ] .right-plot[ <img src="index_files/figure-html/first-plot1b-out-1.png" width="100%" /> ] --- .left-code[ ```r ggplot(tidy_pop) + aes(x = year, y = pop) + * geom_point() ``` ] .right-plot[ <img src="index_files/figure-html/first-plot1c-out-1.png" width="100%" /> ] --- .left-code[ ```r ggplot(tidy_pop) + aes(x = year, y = pop, * color = country) + geom_point() ``` ] .right-plot[ <img src="index_files/figure-html/first-plot1-out-1.png" width="100%" /> ] --- .left-code[ ```r ggplot(tidy_pop) + aes(x = year, y = pop, color = country) + geom_point() + * geom_line() ``` .font80[ ```r geom_path: Es gibt nur eine Beobachtung pro Gruppe (Land). Daher ändern wir die aesthetics für die Gruppe. ``` ] ] .right-plot[ <img src="index_files/figure-html/first-plot2-fake-out-1.png" width="100%" /> ] --- .left-code[ ```r ggplot(tidy_pop) + aes(x = year, y = pop, color = country) + geom_point() + geom_line( * aes(group = country)) ``` ] .right-plot[ <img src="index_files/figure-html/first-plot2-out-1.png" width="100%" /> ] --- .left-code[ ```r g <- ggplot(tidy_pop) + aes(x = year, y = pop, color = country) + geom_point() + geom_line( aes(group = country)) g ``` ] .right-plot[ <img src="index_files/figure-html/first-plot3-out-1.png" width="100%" /> ] --- layout: true # gg heißt Grammar of Graphics .left-column[ ### Data ### Aesthetics ### Geoms ```r + geom_*() ``` ] --- .right-column[ ```r geom_*(mapping, data, stat, position) ``` - `data` *Geoms* können eigene Daten zugewiesen werden - Muss zum globalen Koordinatorensystem passen - `map` *Geoms* können eigene *aesthetics* haben. - Globale *aesthetics* werden sonst übernommen - Spezifische *aesthetics* für einzelne *geoms*, z.B. - `geom_point` benötigt `x` und `y`, optional `shape`, `color`, `size`, etc. - `geom_ribbon`benötigt `x`, `ymin` und `ymax`, optional `fill` - `?geom_ribbon` ] --- .right-column[ ```r geom_*(mapping, data, stat, position) ``` - `stat` Manche *geoms* transformieren die Daten - Option: `stat = 'identity'` - Ex: `geom_histogram` benötigt `stat_bin()` um Beobachtungen zu gruppieren - `position` Manche ändern die Position im Koordinatensystem - `'dodge'`, `'stack'`, `'jitter'` ] --- layout: true # gg heißt Grammar of Graphics .left-column[ ### Data ### Aesthetics ### Geoms ### Facet ```r +facet_wrap() +facet_grid() ``` ] --- .right-column[ ```r g + facet_wrap(~ country) ``` <img src="index_files/figure-html/geom_facet-1.png" width="90%" /> ] --- .right-column[ ```r g + facet_grid(continent ~ country) ``` <img src="index_files/figure-html/geom_grid-1.png" width="90%" /> ] --- layout: true # gg heißt Grammar of Graphics .left-column[ ### Data ### Aesthetics ### Geoms ### Facet ### Labels ```r + labs() ``` ] --- .right-column[ ```r g + labs(x = "Year", y = "Population") ``` <img src="index_files/figure-html/labs-ex-1.png" width="90%" /> ] --- layout: true # gg heißt Grammar of Graphics .left-column[ ### Data ### Aesthetics ### Geoms ### Facet ### Labels ### Coords ```r + coord_*() ``` ] --- .right-column[ ```r g + coord_flip() ``` <img src="index_files/figure-html/coord-ex-1.png" width="90%" /> ] --- .right-column[ ```r g + coord_polar() ``` <img src="index_files/figure-html/coord-ex2-1.png" width="90%" /> ] --- layout: true # gg heißt Grammar of Graphics .left-column[ ### Data ### Aesthetics ### Geoms ### Facet ### Labels ### Coords ### Scales ```r + scale_*_*() ```] --- .right-column[ `scale` + `_` + `<aes>` + `_` + `<type>` + `()` Welche Parameter sollen geändert werden? → `<aes>` <br> Wie soll der Parameter geändert werden? → `<type>` Beispiele - Die diskrete X-Achse soll verändert werden <br> `scale_x_discrete()` - Die Y-Achse soll logarithmiert werden <br>`scale_y_log10()` - Die Farbpalette soll geändert werden <br>`scale_fill_discrete()`<br>`scale_color_manual()` ] --- .right-column[ ```r g + scale_color_manual(values = c("peru", "pink", "plum")) ``` <img src="index_files/figure-html/scale_ex1-1.png" width="90%" /> ] --- .right-column[ ```r g + scale_y_log10() ``` <img src="index_files/figure-html/scale_ex2-1.png" width="90%" /> ] --- .right-column[ ```r g + scale_x_discrete(labels = c("MCMXCVII", "MMII", "MMVII")) ``` <img src="index_files/figure-html/scale_ex4-1.png" width="90%" /> ] --- layout: true # gg heißt Grammar of Graphics .left-column[ ### Data ### Aesthetics ### Geoms ### Facet ### Labels ### Coords ### Scales ### Theme ```r + theme() ``` ] --- .right-column[ Das Aussehen der Grafik ändern<br>> d.h. Dinge, die keinen Daten zugeordnet sind. Einige *Themes* sind bereits enthalten - `g + theme_bw()` - `g + theme_dark()` - `g + theme_gray()` - `g + theme_light()` - `g + theme_minimal()`] --- .right-column[ Eine große Anzahl an Parametern kann individuell geändert werden: - Globale Parameter: `line`, `rect`, `text`, `title` - `axis`: x-, y-Achsentitel, -ticks, -linien - `legend`: Legenden - `panel`: Zeichenfläche - `plot`: Gesamte Grafik - `strip`: Facet labels] --- .right-column[ Die *Themes* haben sog. Hilfsfunktionen: - `element_blank()` löscht das jeweilige Element - `element_line()` - `element_rect()` - `element_text()` ] --- .right-column[ ```r g + theme_bw() ``` <img src="index_files/figure-html/unnamed-chunk-1-1.png" width="90%" /> ] --- .right-column[ .font80[ ```r g + theme_minimal() + theme(text = element_text(family = "Palatino")) ``` <img src="index_files/figure-html/unnamed-chunk-2-1.png" width="90%" /> ] ] --- .right-column[ Das *Theme* kann auch global definiert werden mit `theme_set()` ```r my_theme <- theme_bw() + theme( text = element_text(family = "Palatino", size = 12), panel.border = element_rect(colour = 'grey80'), panel.grid.minor = element_blank() ) theme_set(my_theme) ``` Alle Grafiken im jeweiligen R-File werden mit diesem *Theme* erstellt! ] --- .right-column[ ```r g ``` <img src="index_files/figure-html/unnamed-chunk-3-1.png" width="90%" /> ] --- .right-column[ ```r g + theme(legend.position = 'bottom') ``` <img src="index_files/figure-html/unnamed-chunk-4-1.png" width="90%" /> ] --- layout: false # Grafiken speichern Codebasierte Variante: `ggsave` ```r ggsave( filename = "my_plot.png", plot = my_plot, width = 10, height = 8, dpi = 100, device = "png" ) ``` Oder in RStudio manuell (nicht reproduzierbar!) --- # Übung: Einfache Grafiken in R Datei: `Companion-script_Uebungen.R` Übung 0: Reproduktion der ersten Grafik Übung 1: Schritt für Schritt-Aufbau einer einfachen Grafik --- class: inverse, center, middle # "Live" Coding: <br> Beispiele mit dem *gapminder*-Datensatz Adopted from [A Gentle Guide to the Grammar of Graphics with ggplot2](https://github.com/gadenbuie/gentle-ggplot2) by Garrick Aden-Buie --- layout: true # Einfache Grafiken mit *gapminder* Daten --- ```r library(gapminder) head(gapminder) ``` <div class="kable-table"> <table> <thead> <tr> <th style="text-align:left;"> country </th> <th style="text-align:left;"> continent </th> <th style="text-align:right;"> year </th> <th style="text-align:right;"> lifeExp </th> <th style="text-align:right;"> pop </th> <th style="text-align:right;"> gdpPercap </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> Asia </td> <td style="text-align:right;"> 1952 </td> <td style="text-align:right;"> 28.801 </td> <td style="text-align:right;"> 8425333 </td> <td style="text-align:right;"> 779.4453 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> Asia </td> <td style="text-align:right;"> 1957 </td> <td style="text-align:right;"> 30.332 </td> <td style="text-align:right;"> 9240934 </td> <td style="text-align:right;"> 820.8530 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> Asia </td> <td style="text-align:right;"> 1962 </td> <td style="text-align:right;"> 31.997 </td> <td style="text-align:right;"> 10267083 </td> <td style="text-align:right;"> 853.1007 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> Asia </td> <td style="text-align:right;"> 1967 </td> <td style="text-align:right;"> 34.020 </td> <td style="text-align:right;"> 11537966 </td> <td style="text-align:right;"> 836.1971 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> Asia </td> <td style="text-align:right;"> 1972 </td> <td style="text-align:right;"> 36.088 </td> <td style="text-align:right;"> 13079460 </td> <td style="text-align:right;"> 739.9811 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> Asia </td> <td style="text-align:right;"> 1977 </td> <td style="text-align:right;"> 38.438 </td> <td style="text-align:right;"> 14880372 </td> <td style="text-align:right;"> 786.1134 </td> </tr> </tbody> </table> </div> --- ```r glimpse(gapminder) ``` ``` Observations: 1,704 Variables: 6 $ country <fct> Afghanistan, Afghanistan, Afghanistan, Afghanistan, Afghanistan, Af... $ continent <fct> Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, A... $ year <int> 1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, 2002, 2... $ lifeExp <dbl> 28.801, 30.332, 31.997, 34.020, 36.088, 38.438, 39.854, 40.822, 41.... $ pop <int> 8425333, 9240934, 10267083, 11537966, 13079460, 14880372, 12881816,... $ gdpPercap <dbl> 779.4453, 820.8530, 853.1007, 836.1971, 739.9811, 786.1134, 978.011... ``` -- ### Erste Grafik: `lifeExp` vs. `gdpPercap` --- class: fullscreen layout: true --- .left-code[ ```r ggplot(gapminder) + aes(x = gdpPercap, y = lifeExp) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-le-gdp-1-1.png) ] -- Zuerst: Datenpunkte --- .left-code[ ```r ggplot(gapminder) + aes(x = gdpPercap, y = lifeExp) + * geom_point() ``` ] .right-plot[ ![](index_files/figure-html/gapminder-le-gdp-2-1.png) ] -- Next: Kontinente sichtbar machen. --- .left-code[ ```r ggplot(gapminder) + aes(x = gdpPercap, y = lifeExp, * color = continent) + geom_point() ``` ] .right-plot[ ![](index_files/figure-html/gapminder-le-gdp-3-1.png) ] -- Schlecht lesbar. Lösung:<br> Logarithmieren der X-Achse --- .left-code[ ```r ggplot(gapminder) + aes(x = gdpPercap, y = lifeExp, color = continent) + geom_point() + * scale_x_log10() ``` ] .right-plot[ ![](index_files/figure-html/gapminder-le-gdp-4-1.png) ] -- Immer noch sehr unübersichtlich... --- .left-code[ ```r ggplot(gapminder) + aes(x = gdpPercap, y = lifeExp, color = continent) + geom_point() + scale_x_log10() + * facet_wrap(~ continent) + * guides(color = FALSE) ``` Dank der Facets brauchen wir keine Farblegende mehr. ] .right-plot[ ![](index_files/figure-html/gapminder-le-gdp-5-1.png) ] -- Overplotting ... --- .left-code[ ```r ggplot(gapminder) + aes(x = gdpPercap, y = lifeExp, color = continent) + * geom_point(size = 0.25) + scale_x_log10() + facet_wrap(~ continent) + guides(color = FALSE) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-le-gdp-6-1.png) ] -- Gibt es einen Trend? --- .left-code[ ```r ggplot(gapminder) + aes(x = gdpPercap, y = lifeExp, color = continent) + * geom_line() + geom_point(size = 0.25) + scale_x_log10() + facet_wrap(~ continent) + guides(color = FALSE) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-le-gdp-7-1.png) ] -- `geom_line` verbindet alle Punkte miteinander - nicht was wir wollten! --- .left-code[ ```r ggplot(gapminder) + aes(x = gdpPercap, y = lifeExp, color = continent) + geom_line( * aes(group = country) ) + geom_point(size = 0.25) + scale_x_log10() + facet_wrap(~ continent) + guides(color = FALSE) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-le-gdp-8-1.png) ] -- Für einen Trend brauchen wir einen Zeitverlauf. --- .left-code[ ```r ggplot(gapminder) + * aes(x = year, * y = gdpPercap, color = continent) + geom_line( aes(group = country) ) + geom_point(size = 0.25) + * scale_y_log10() + facet_wrap(~ continent) + guides(color = FALSE) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-gdp-year-1-1.png) ] -- X-Achsenbeschriftung nicht lesbar. --- .left-code[ ```r ggplot(gapminder) + aes(x = year, y = gdpPercap, color = continent) + geom_line( aes(group = country) ) + geom_point(size = 0.25) + scale_y_log10() + * scale_x_continuous(breaks = * seq(1950, 2000, 25) * ) + facet_wrap(~ continent) + guides(color = FALSE) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-gdp-year-2-1.png) ] -- Schauen wir uns die Lebenserwartung an. --- .left-code[ ```r ggplot(gapminder) + aes(x = year, * y = lifeExp, color = continent) + geom_line( aes(group = country) ) + geom_point(size = 0.25) + * #scale_y_log10() + scale_x_continuous(breaks = seq(1950, 2000, 25) ) + facet_wrap(~ continent) + guides(color = FALSE) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-le-year-1-1.png) ] -- Die Trendlinie. --- .left-code[ ```r ggplot(gapminder) + aes(x = year, y = lifeExp, color = continent) + geom_line( aes(group = country) ) + geom_point(size = 0.25) + * geom_smooth() + scale_x_continuous(breaks = seq(1950, 2000, 25) ) + facet_wrap(~ continent) + guides(color = FALSE) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-le-year-2-1.png) ] -- Die Trendlinie ist kaum sichtbar. --- .left-code[ ```r ggplot(gapminder) + aes(x = year, y = lifeExp, color = continent) + geom_line( aes(group = country), * color = "grey75" ) + geom_point(size = 0.25) + geom_smooth() + scale_x_continuous(breaks = seq(1950, 2000, 25) ) + facet_wrap(~ continent) + guides(color = FALSE) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-le-year-3-1.png) ] -- Die Punkte stören noch. --- .left-code[ ```r ggplot(gapminder) + aes(x = year, y = lifeExp, color = continent) + geom_line( aes(group = country), color = "grey75" ) + * #geom_point(size = 0.25) + geom_smooth() + scale_x_continuous(breaks = seq(1950, 2000, 25) ) + facet_wrap(~ continent) + guides(color = FALSE) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-le-year-4-1.png) ] -- Vergleich der Kontinente in einem Plot. --- .left-code[ ```r ggplot(gapminder) + aes(x = year, y = lifeExp, color = continent) + geom_line( aes(group = country), color = "grey75" ) + geom_smooth() + # scale_x_continuous( # breaks = # seq(1950, 2000, 25) # ) + * # facet_wrap(~ continent) + guides(color = FALSE) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-le-year-5-1.png) ] -- Jetzt brauchen wir wieder eine Legende. --- .left-code[ ```r ggplot(gapminder) + aes(x = year, y = lifeExp, color = continent) + geom_line( aes(group = country), color = "grey75" ) + geom_smooth() + * theme( * legend.position = "bottom" * ) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-le-year-6-1.png) ] -- Optische Korrekturen mit dem *minimal theme*. --- .left-code[ ```r ggplot(gapminder) + aes(x = year, y = lifeExp, color = continent) + geom_line( aes(group = country), color = "grey75" ) + geom_smooth() + * theme_minimal() + theme( legend.position = "bottom" ) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-le-year-7-1.png) ] -- Schriftgröße ändern. --- .left-code[ ```r ggplot(gapminder) + aes(x = year, y = lifeExp, color = continent) + geom_line( aes(group = country), color = "grey75" ) + geom_smooth() + theme_minimal( * base_size = 8) + theme( legend.position = "bottom" ) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-le-year-8-1.png) ] --- .left-code[ ### Ein Blick auf einzelne Länder ```r firstworld <- gapminder %>% filter( country %in% c( "United States", "Canada", "Germany", "France" ) ) ``` ] .right-plot[ <div class="kable-table"> <table> <thead> <tr> <th style="text-align:left;"> country </th> <th style="text-align:left;"> continent </th> <th style="text-align:right;"> year </th> <th style="text-align:right;"> lifeExp </th> <th style="text-align:right;"> pop </th> <th style="text-align:right;"> gdpPercap </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Canada </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1952 </td> <td style="text-align:right;"> 68.750 </td> <td style="text-align:right;"> 14785584 </td> <td style="text-align:right;"> 11367.161 </td> </tr> <tr> <td style="text-align:left;"> Canada </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1957 </td> <td style="text-align:right;"> 69.960 </td> <td style="text-align:right;"> 17010154 </td> <td style="text-align:right;"> 12489.950 </td> </tr> <tr> <td style="text-align:left;"> Canada </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1962 </td> <td style="text-align:right;"> 71.300 </td> <td style="text-align:right;"> 18985849 </td> <td style="text-align:right;"> 13462.486 </td> </tr> <tr> <td style="text-align:left;"> Canada </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1967 </td> <td style="text-align:right;"> 72.130 </td> <td style="text-align:right;"> 20819767 </td> <td style="text-align:right;"> 16076.588 </td> </tr> <tr> <td style="text-align:left;"> Canada </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1972 </td> <td style="text-align:right;"> 72.880 </td> <td style="text-align:right;"> 22284500 </td> <td style="text-align:right;"> 18970.571 </td> </tr> <tr> <td style="text-align:left;"> Canada </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1977 </td> <td style="text-align:right;"> 74.210 </td> <td style="text-align:right;"> 23796400 </td> <td style="text-align:right;"> 22090.883 </td> </tr> <tr> <td style="text-align:left;"> Canada </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1982 </td> <td style="text-align:right;"> 75.760 </td> <td style="text-align:right;"> 25201900 </td> <td style="text-align:right;"> 22898.792 </td> </tr> <tr> <td style="text-align:left;"> Canada </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1987 </td> <td style="text-align:right;"> 76.860 </td> <td style="text-align:right;"> 26549700 </td> <td style="text-align:right;"> 26626.515 </td> </tr> <tr> <td style="text-align:left;"> Canada </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1992 </td> <td style="text-align:right;"> 77.950 </td> <td style="text-align:right;"> 28523502 </td> <td style="text-align:right;"> 26342.884 </td> </tr> <tr> <td style="text-align:left;"> Canada </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1997 </td> <td style="text-align:right;"> 78.610 </td> <td style="text-align:right;"> 30305843 </td> <td style="text-align:right;"> 28954.926 </td> </tr> <tr> <td style="text-align:left;"> Canada </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 2002 </td> <td style="text-align:right;"> 79.770 </td> <td style="text-align:right;"> 31902268 </td> <td style="text-align:right;"> 33328.965 </td> </tr> <tr> <td style="text-align:left;"> Canada </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 2007 </td> <td style="text-align:right;"> 80.653 </td> <td style="text-align:right;"> 33390141 </td> <td style="text-align:right;"> 36319.235 </td> </tr> <tr> <td style="text-align:left;"> France </td> <td style="text-align:left;"> Europe </td> <td style="text-align:right;"> 1952 </td> <td style="text-align:right;"> 67.410 </td> <td style="text-align:right;"> 42459667 </td> <td style="text-align:right;"> 7029.809 </td> </tr> <tr> <td style="text-align:left;"> France </td> <td style="text-align:left;"> Europe </td> <td style="text-align:right;"> 1957 </td> <td style="text-align:right;"> 68.930 </td> <td style="text-align:right;"> 44310863 </td> <td style="text-align:right;"> 8662.835 </td> </tr> <tr> <td style="text-align:left;"> France </td> <td style="text-align:left;"> Europe </td> <td style="text-align:right;"> 1962 </td> <td style="text-align:right;"> 70.510 </td> <td style="text-align:right;"> 47124000 </td> <td style="text-align:right;"> 10560.486 </td> </tr> <tr> <td style="text-align:left;"> France </td> <td style="text-align:left;"> Europe </td> <td style="text-align:right;"> 1967 </td> <td style="text-align:right;"> 71.550 </td> <td style="text-align:right;"> 49569000 </td> <td style="text-align:right;"> 12999.918 </td> </tr> <tr> <td style="text-align:left;"> France </td> <td style="text-align:left;"> Europe </td> <td style="text-align:right;"> 1972 </td> <td style="text-align:right;"> 72.380 </td> <td style="text-align:right;"> 51732000 </td> <td style="text-align:right;"> 16107.192 </td> </tr> <tr> <td style="text-align:left;"> France </td> <td style="text-align:left;"> Europe </td> <td style="text-align:right;"> 1977 </td> <td style="text-align:right;"> 73.830 </td> <td style="text-align:right;"> 53165019 </td> <td style="text-align:right;"> 18292.635 </td> </tr> <tr> <td style="text-align:left;"> France </td> <td style="text-align:left;"> Europe </td> <td style="text-align:right;"> 1982 </td> <td style="text-align:right;"> 74.890 </td> <td style="text-align:right;"> 54433565 </td> <td style="text-align:right;"> 20293.897 </td> </tr> <tr> <td style="text-align:left;"> France </td> <td style="text-align:left;"> Europe </td> <td style="text-align:right;"> 1987 </td> <td style="text-align:right;"> 76.340 </td> <td style="text-align:right;"> 55630100 </td> <td style="text-align:right;"> 22066.442 </td> </tr> <tr> <td style="text-align:left;"> France </td> <td style="text-align:left;"> Europe </td> <td style="text-align:right;"> 1992 </td> <td style="text-align:right;"> 77.460 </td> <td style="text-align:right;"> 57374179 </td> <td style="text-align:right;"> 24703.796 </td> </tr> <tr> <td style="text-align:left;"> France </td> <td style="text-align:left;"> Europe </td> <td style="text-align:right;"> 1997 </td> <td style="text-align:right;"> 78.640 </td> <td style="text-align:right;"> 58623428 </td> <td style="text-align:right;"> 25889.785 </td> </tr> <tr> <td style="text-align:left;"> France </td> <td style="text-align:left;"> Europe </td> <td style="text-align:right;"> 2002 </td> <td style="text-align:right;"> 79.590 </td> <td style="text-align:right;"> 59925035 </td> <td style="text-align:right;"> 28926.032 </td> </tr> <tr> <td style="text-align:left;"> France </td> <td style="text-align:left;"> Europe </td> <td style="text-align:right;"> 2007 </td> <td style="text-align:right;"> 80.657 </td> <td style="text-align:right;"> 61083916 </td> <td style="text-align:right;"> 30470.017 </td> </tr> <tr> <td style="text-align:left;"> Germany </td> <td style="text-align:left;"> Europe </td> <td style="text-align:right;"> 1952 </td> <td style="text-align:right;"> 67.500 </td> <td style="text-align:right;"> 69145952 </td> <td style="text-align:right;"> 7144.114 </td> </tr> <tr> <td style="text-align:left;"> Germany </td> <td style="text-align:left;"> Europe </td> <td style="text-align:right;"> 1957 </td> <td style="text-align:right;"> 69.100 </td> <td style="text-align:right;"> 71019069 </td> <td style="text-align:right;"> 10187.827 </td> </tr> <tr> <td style="text-align:left;"> Germany </td> <td style="text-align:left;"> Europe </td> <td style="text-align:right;"> 1962 </td> <td style="text-align:right;"> 70.300 </td> <td style="text-align:right;"> 73739117 </td> <td style="text-align:right;"> 12902.463 </td> </tr> <tr> <td style="text-align:left;"> Germany </td> <td style="text-align:left;"> Europe </td> <td style="text-align:right;"> 1967 </td> <td style="text-align:right;"> 70.800 </td> <td style="text-align:right;"> 76368453 </td> <td style="text-align:right;"> 14745.626 </td> </tr> <tr> <td style="text-align:left;"> Germany </td> <td style="text-align:left;"> Europe </td> <td style="text-align:right;"> 1972 </td> <td style="text-align:right;"> 71.000 </td> <td style="text-align:right;"> 78717088 </td> <td style="text-align:right;"> 18016.180 </td> </tr> <tr> <td style="text-align:left;"> Germany </td> <td style="text-align:left;"> Europe </td> <td style="text-align:right;"> 1977 </td> <td style="text-align:right;"> 72.500 </td> <td style="text-align:right;"> 78160773 </td> <td style="text-align:right;"> 20512.921 </td> </tr> <tr> <td style="text-align:left;"> Germany </td> <td style="text-align:left;"> Europe </td> <td style="text-align:right;"> 1982 </td> <td style="text-align:right;"> 73.800 </td> <td style="text-align:right;"> 78335266 </td> <td style="text-align:right;"> 22031.533 </td> </tr> <tr> <td style="text-align:left;"> Germany </td> <td style="text-align:left;"> Europe </td> <td style="text-align:right;"> 1987 </td> <td style="text-align:right;"> 74.847 </td> <td style="text-align:right;"> 77718298 </td> <td style="text-align:right;"> 24639.186 </td> </tr> <tr> <td style="text-align:left;"> Germany </td> <td style="text-align:left;"> Europe </td> <td style="text-align:right;"> 1992 </td> <td style="text-align:right;"> 76.070 </td> <td style="text-align:right;"> 80597764 </td> <td style="text-align:right;"> 26505.303 </td> </tr> <tr> <td style="text-align:left;"> Germany </td> <td style="text-align:left;"> Europe </td> <td style="text-align:right;"> 1997 </td> <td style="text-align:right;"> 77.340 </td> <td style="text-align:right;"> 82011073 </td> <td style="text-align:right;"> 27788.884 </td> </tr> <tr> <td style="text-align:left;"> Germany </td> <td style="text-align:left;"> Europe </td> <td style="text-align:right;"> 2002 </td> <td style="text-align:right;"> 78.670 </td> <td style="text-align:right;"> 82350671 </td> <td style="text-align:right;"> 30035.802 </td> </tr> <tr> <td style="text-align:left;"> Germany </td> <td style="text-align:left;"> Europe </td> <td style="text-align:right;"> 2007 </td> <td style="text-align:right;"> 79.406 </td> <td style="text-align:right;"> 82400996 </td> <td style="text-align:right;"> 32170.374 </td> </tr> <tr> <td style="text-align:left;"> United States </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1952 </td> <td style="text-align:right;"> 68.440 </td> <td style="text-align:right;"> 157553000 </td> <td style="text-align:right;"> 13990.482 </td> </tr> <tr> <td style="text-align:left;"> United States </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1957 </td> <td style="text-align:right;"> 69.490 </td> <td style="text-align:right;"> 171984000 </td> <td style="text-align:right;"> 14847.127 </td> </tr> <tr> <td style="text-align:left;"> United States </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1962 </td> <td style="text-align:right;"> 70.210 </td> <td style="text-align:right;"> 186538000 </td> <td style="text-align:right;"> 16173.146 </td> </tr> <tr> <td style="text-align:left;"> United States </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1967 </td> <td style="text-align:right;"> 70.760 </td> <td style="text-align:right;"> 198712000 </td> <td style="text-align:right;"> 19530.366 </td> </tr> <tr> <td style="text-align:left;"> United States </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1972 </td> <td style="text-align:right;"> 71.340 </td> <td style="text-align:right;"> 209896000 </td> <td style="text-align:right;"> 21806.036 </td> </tr> <tr> <td style="text-align:left;"> United States </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1977 </td> <td style="text-align:right;"> 73.380 </td> <td style="text-align:right;"> 220239000 </td> <td style="text-align:right;"> 24072.632 </td> </tr> <tr> <td style="text-align:left;"> United States </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1982 </td> <td style="text-align:right;"> 74.650 </td> <td style="text-align:right;"> 232187835 </td> <td style="text-align:right;"> 25009.559 </td> </tr> <tr> <td style="text-align:left;"> United States </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1987 </td> <td style="text-align:right;"> 75.020 </td> <td style="text-align:right;"> 242803533 </td> <td style="text-align:right;"> 29884.350 </td> </tr> <tr> <td style="text-align:left;"> United States </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1992 </td> <td style="text-align:right;"> 76.090 </td> <td style="text-align:right;"> 256894189 </td> <td style="text-align:right;"> 32003.932 </td> </tr> <tr> <td style="text-align:left;"> United States </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1997 </td> <td style="text-align:right;"> 76.810 </td> <td style="text-align:right;"> 272911760 </td> <td style="text-align:right;"> 35767.433 </td> </tr> <tr> <td style="text-align:left;"> United States </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 2002 </td> <td style="text-align:right;"> 77.310 </td> <td style="text-align:right;"> 287675526 </td> <td style="text-align:right;"> 39097.100 </td> </tr> <tr> <td style="text-align:left;"> United States </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 2007 </td> <td style="text-align:right;"> 78.242 </td> <td style="text-align:right;"> 301139947 </td> <td style="text-align:right;"> 42951.653 </td> </tr> </tbody> </table> </div> ] --- .left-code[ ```r ggplot(firstworld) + aes( x = year, y = pop ) + geom_col() ``` ] .right-plot[ ![](index_files/figure-html/gapminder-1stworld-1-1.png) ] -- Wie teilt sich die Bevölkerung auf die Länder auf? --- .left-code[ ```r ggplot(firstworld) + aes( x = year, y = pop, * fill = country ) + geom_col() ``` ] .right-plot[ ![](index_files/figure-html/gapminder-1stworld-2-1.png) ] -- Einfaches statt gestapeltes Balkendiagramm. --- .left-code[ ```r ggplot(firstworld) + aes( x = year, y = pop, fill = country ) + geom_col( * position = "dodge" ) ``` `position = "dodge"` platziert die Balken nebeneinander. ] .right-plot[ ![](index_files/figure-html/gapminder-1stworld-3-1.png) ] -- Zahlen auf der Y-Achse transformieren? --- .left-code[ ```r ggplot(firstworld) + aes( x = year, * y = pop / 10^6, fill = country ) + geom_col( position = "dodge" ) ``` *aesthetics* können rechnen! ] .right-plot[ ![](index_files/figure-html/gapminder-1stworld-4-1.png) ] -- Die Länder im Einzelnen. --- .left-code[ ```r ggplot(firstworld) + aes( x = year, y = pop / 10^6, fill = country ) + geom_col( position = "dodge" ) + * facet_wrap(~ country) + * guides(fill = FALSE) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-1stworld-5-1.png) ] -- Die Y-Achse kann individuell angepasst werden. (.hl[Achtung!]) --- .left-code[ ```r ggplot(firstworld) + aes( x = year, y = pop / 10^6, fill = country ) + geom_col( position = "dodge" ) + facet_wrap(~ country, * scales = "free_y") + guides(fill = FALSE) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-1stworld-6-1.png) ] -- Schauen wir uns die Lebenserwartung an. --- .left-code[ ```r ggplot(firstworld) + aes( x = year, * y = lifeExp, fill = country ) + geom_col( position = "dodge" ) + facet_wrap(~ country, scales = "free_y") + guides(fill = FALSE) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-1stworld-7-1.png) ] -- Hier wäre 📈 passender als 📊. --- .left-code[ ```r ggplot(firstworld) + aes( x = year, y = lifeExp, fill = country ) + * geom_line() + facet_wrap(~ country, scales = "free_y") + guides(fill = FALSE) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-1stworld-8-1.png) ] -- 📊 brauchen **fill**, 📈 brauchen **color**! --- .left-code[ ```r ggplot(firstworld) + aes( x = year, y = lifeExp, * color = country ) + geom_line() + facet_wrap(~ country, scales = "free_y") + * guides(color = FALSE) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-1stworld-9-1.png) ] -- Dies ist in einer Grafik übersichtlicher! --- .left-code[ ```r ggplot(firstworld) + aes( x = year, y = lifeExp, color = country ) + geom_line() ``` ] .right-plot[ ![](index_files/figure-html/gapminder-1stworld-10-1.png) ] --- ### Wie sieht es mit der Lebenserwartung aus? - auf einem Kontinent - global --- .left-code[ ```r gapminder %>% filter( continent == "Americas" * ) %>% * ggplot() + aes( x = year, y = lifeExp ) ``` pipe funktioniert mit `ggplot()`! .hl[Achtung]: `%>%` vs. `+` ] .right-plot[ ![](index_files/figure-html/gapminder-all-americas-1-1.png) ] -- Boxplot mit dem Range der Lebenserwartung. --- .left-code[ ```r gapminder %>% filter( continent == "Americas" ) %>% ggplot() + aes( x = year, y = lifeExp ) + * geom_boxplot() ``` ] .right-plot[ ![](index_files/figure-html/gapminder-all-americas-2-1.png) ] -- Ein Boxplot pro Jahr. --- .left-code[ ```r gapminder %>% filter( continent == "Americas" ) %>% * mutate( * year = factor(year) * ) %>% ggplot() + aes( x = year, y = lifeExp ) + geom_boxplot() ``` ] .right-plot[ ![](index_files/figure-html/gapminder-all-americas-3-1.png) ] -- Globale Lebenserwartung: Auskommentieren des Filters. --- .left-code[ ```r gapminder %>% # filter( # continent == "Americas" # ) %>% mutate( year = factor(year) ) %>% ggplot() + aes( x = year, y = lifeExp ) + geom_boxplot() ``` ] .right-plot[ ![](index_files/figure-html/gapminder-all-americas-4-1.png) ] -- Einen Boxplot pro Kontinent? --- .left-code[ ```r gapminder %>% mutate( year = factor(year) ) %>% ggplot() + aes( x = year, y = lifeExp, * fill = continent ) + geom_boxplot() ``` ] .right-plot[ ![](index_files/figure-html/gapminder-all-americas-5-1.png) ] -- Die Jahre sind kaum zu lesen, also: 🔄 --- .left-code[ ```r gapminder %>% mutate( year = factor(year) ) %>% ggplot() + aes( x = year, y = lifeExp, fill = continent ) + geom_boxplot() + * coord_flip() ``` ] .right-plot[ ![](index_files/figure-html/gapminder-all-americas-6-1.png) ] -- `dplyr` funktiniert auch im *ggplot2*-Code (Lebenserwartung pro Dekade) --- .left-code[ ```r gapminder %>% mutate( * decade = floor(year / 10), * decade = decade * 10, * decade = factor(decade) ) %>% ggplot() + aes( * x = decade, y = lifeExp, fill = continent ) + geom_boxplot() + coord_flip() ``` ] .right-plot[ ![](index_files/figure-html/gapminder-all-americas-7-1.png) ] -- Ohne Ozeanien: `filter`-Befehl. --- .left-code[ ```r g <- gapminder %>% * filter( * continent != "Oceania" * ) %>% mutate( decade = floor(year / 10) * 10, decade = factor(decade) ) %>% ggplot() + aes( x = decade, y = lifeExp, fill = continent ) + geom_boxplot() + coord_flip() ``` ] .right-plot[ ![](index_files/figure-html/gapminder-all-americas-8-1.png) ] --- .left-code[ ```r g + theme_minimal(8) + labs( y = "Life Expectancy", x = "Decade", fill = NULL, title = "Life Expectancy by Continent and Decade", caption = "gapminder.org" ) ``` .hl[Achtung]: `x` und `y` bleiben auch bei `coord_flip()` erhalten. `fill = NULL` entfernt den Titel der Label. ] .right-plot[ ![](index_files/figure-html/gapminder-all-americas-9-1.png) ] --- layout: false class: inverse, center, middle # Komplexe Grafiken Adopted from [A Gentle Guide to the Grammar of Graphics with ggplot2](https://github.com/gadenbuie/gentle-ggplot2) by Garrick Aden-Buie who was inspired by the [TED-Talk "The Best Stats You've Ever Seen"](http://www.ted.com/talks/hans_rosling_shows_the_best_stats_you_ve_ever_seen) by Hans Rosling --- # Grundlayout ```r g_hr <- ggplot(gapminder) + aes(x = gdpPercap, y = lifeExp, size = pop, color = country) + geom_point() + facet_wrap(~year) ``` .plot-callout[ ![](index_files/figure-html/hans-rosling-1-1.png) ] --- # Entfernen der Legende ```r g_hr <- ggplot(gapminder) + aes(x = gdpPercap, y = lifeExp, size = pop, color = country) + geom_point() + facet_wrap(~year) + guides(color = FALSE, size = FALSE) ``` .plot-callout[ ![](index_files/figure-html/hans-rosling-1a-1.png) ] --- # X-Achse, Farbe und Größe der Punkte anpassen ```r g_hr <- g_hr + scale_x_log10(breaks = c(10^3, 10^4, 10^5), labels = c("1k", "10k", "100k")) + scale_color_manual(values = gapminder::country_colors) + scale_size(range = c(0.5, 12)) ``` .plot-callout[ ![](index_files/figure-html/hans-rosling-2-1.png) ] --- # Optimieren der Beschriftung <br><br> ```r g_hr <- g_hr + labs( x = "GDP per capita", y = "Life Expectancy" ) + theme_minimal(base_family = "Fira Sans") + theme( strip.text = element_text(size = 16, face = "bold"), panel.border = element_rect(fill = NA, color = "grey40"), panel.grid.minor = element_blank() ) ``` .plot-callout.top-right[ ![](index_files/figure-html/hans-rosling-3-1.png) ] --- # Der gesamte Code und die Grafik .font70[ ```r ggplot(gapminder) + aes(x = gdpPercap, y = lifeExp, size = pop, color = country) + geom_point() + facet_wrap(~year) + guides(color = FALSE, size = FALSE) + scale_x_log10( breaks = c(10^3, 10^4, 10^5), labels = c("1k", "10k", "100k")) + scale_color_manual(values = gapminder::country_colors) + scale_size(range = c(0.5, 12)) + labs( x = "GDP per capita", y = "Life Expectancy") + theme_minimal(14, base_family = "Fira Sans") + theme( strip.text = element_text(size = 16, face = "bold"), panel.border = element_rect(fill = NA, color = "grey40"), panel.grid.minor = element_blank()) ``` ] --- class: fullscreen background-image: url(index_files/figure-html/hans-rosling-final-1.png) background-size: cover --- # Bonus: Animierte Grafiken! .left-code[ .font70[ ```r # library(devtools) # install_github("thomasp85/gganimate") library(gganimate) # Same plot without facet_wrap() g_hra + transition_states(year, 1, 0) + ggtitle("{closest_state}") ``` ] ] -- .right-plot[ ![](images/hans-rosling-esque.gif) ] --- # Übung: Komplexe Grafiken in R Datei: `Companion-script_Uebungen.R` Übung 2: Grafiken mit gapminder Daten aufbauen Übung 3: Americas Grafik mit Gapminder Daten Übung 4: Alle amerikanischen Länder Übung 5: Hans Rosling Grafik --- layout: false class: inverse, middle, center # Zum Abschluss ... --- # Stack Exchange ist _die_ Adresse ![](images/stack-exchange-search.png) --- <img src="images/stack-exchange-answer.png" style="max-height: 100%"> --- layout: false # ggplot2 Extensions: [ggplot2-exts.org](https://www.ggplot2-exts.org/gallery/) <img src="images/ggplot2-exts-gallery.png" style="max-height: 100%"> --- # ggplot2-Tools ### Nützliche Links - **ggplot2 Dokumentation:** <http://ggplot2.tidyverse.org/> - **R4DS - Datenvisualisierung (E-Book online):** <http://r4ds.had.co.nz/data-visualisation.html> - **Hadley Wickhams ggplot2 Buch:** <https://www.springer.com/gp/book/9780387981413> ### Nützliche RStudio Add-Ins (nicht getestet) - [esquisse](https://github.com/dreamRs/esquisse): Interaktive ggplot2-Plots erstellen - [ggplotThemeAssist](https://github.com/calligross/ggthemeassist): ggplot-Themes interaktiv bearbeiten - [ggedit](https://github.com/metrumresearchgroup/ggedit): Layer, Achsen und Themes bearbeiten --- # Übungen ### #TidyTuesday - <https://github.com/rfordatascience/tidytuesday> ### Datensets für Übungen - `fivethirtyeight` - `nycflights` - `ggplot2movies` --- class: inverse, center, middle # Danke schön! .font150.text-white[ Regina Weber <br> [www.reginaweber.de](https://www.reginaweber.de) <br> regina.weber@hochschule-rhein-waal.de ]