class: title-slide, center, middle <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.0/css/all.css" integrity="sha384-aOkxzJ5uQz7WBObEZcHvV5JvRW3TUc2rNPA7pe3AwnsUohiw1Vj2Rgx2KSOkF5+h" crossorigin="anonymous"> <style> .center2 { margin: 0; position: absolute; top: 50%; left: 50%; -ms-transform: translate(-50%, -50%); transform: translate(-50%, -50%); } .rcorners1 { margin: auto; border-radius: 25px; background: #ada500; padding: 10px; # width: 50%; } </style> <style type="text/css"> .right-column{ padding-top: 0; } .remark-code, .remark-inline-code { font-family: 'Source Code Pro', 'Lucida Console', Monaco, monospace; font-size: 90%; } </style> <div class="my-logo-left"> <img src="img/UA-eng-hor-1-RGB.jpg" width="110%"/> </div> <div class="my-logo-right"> <img src="img/ICO-logo.png" width="90%"/> </div> # ICO Workshop R & RStudio .font160[ .SW-greenD[Part 4] ] .font120[ .SW-greenD[*Powerful visualisations with*] .UA-red[*`ggplot2`*] ] Sven De Maeyer & Tine van Daal .font80[ .UA-red[ 2nd - 4th July, 2024 ] ] --- class: inverse-green, left # Overview .center2[ 1. Simple plots in R --- ([click here](#part1)) 2. Grammar of graphics --- ([click here](#part2)) 3. How `ggplot2` works (in a nutshell) --- ([click here](#part3)) 4. Visualising a categorical variable --- ([click here](#part4)) 5. Visualising a quantitative variable --- ([click here](#part5)) 6. Visualising more than one variable --- ([click here](#part6)) 7. More about visualisation? --- ([click here](#part7)) ] --- class: inverse-green, center, middle name: part1 # 1. Simple plots in R --- ## Plots in .UA-red[`base`] .panelset[ .panel[.panel-name[.SW-greenD[`plot()`]] .SW_greenD[The generic function .UA-red[`plot()`] "knows" what to do (plot) with the input it receives.] .pull-left[ ```r # one quantitatieve variable plot(Friends$fluency) ``` <img src="slides_part-4_files/figure-html/unnamed-chunk-2-1.png" width="252" style="display: block; margin: auto;" /> ] .pull-right[ ```r # one qualitative variables plot(table(Friends$condition)) ``` <img src="slides_part-4_files/figure-html/unnamed-chunk-3-1.png" width="252" style="display: block; margin: auto;" /> ] ] .panel[.panel-name[`boxplot()`] ```r boxplot( Friends$fluency, main = "Boxplot of the variable 'fluency'", col = "steelblue" ) ``` <img src="slides_part-4_files/figure-html/unnamed-chunk-4-1.png" width="360" style="display: block; margin: auto;" /> ] .panel[.panel-name[`hist()`] ```r hist( Friends$fluency, main = "Histogram of the variable 'fluency'", freq = FALSE, ylim = c(0, .08) ) ``` <img src="slides_part-4_files/figure-html/unnamed-chunk-5-1.png" width="288" style="display: block; margin: auto;" /> ] ] --- class: inverse-green, center, middle name: part2 # 2. Grammar of Graphics --- ## A more theoretical approach to visualisation .left-column[ <img src="img/Grammar_of_Graphics_cover.jpeg" width="80%" height="80%" /> ] .right-column[ - Theoretical 'breakdown' of a visualisation into components (called layers) - One system to create different visualisations - At the heart of several modern graphical applications: - ggplot2 - Tableau (Polaris) - Vega-Lite ] <br> .footnote[ _Slide taken from slide show by [Thomas Lin Pedersen](https://github.com/thomasp85/ggplot2_workshop/blob/master/presentation.pdf)_] --- ## Key idea behind the Grammar of Graphics .pull-left[ .font140[**Layers** of a visualisation:] .data-theme[.bold[data]] .aes-theme[.bold[aesthetics]] .geom-theme[.bold[geometries]] .facet-theme[.bold[facets]] .stat-theme[.bold[statistics]] .coord-theme[.bold[coordinates]] .theme-theme[.bold[themes]] ] .pull-right[  ] <br> <br> <br> .footnote[ _Animation by [Thomas de Beus](https://medium.com/tdebeus/think-about-the-grammar-of-graphics-when-improving-your-graphs-18e3744d8d18)_] --- ## Grammar of Graphics is a bit like cake .pull-left[ <br> <br> Start by setting up the *foundation* with **`ggplot()`** Specify *ingredients* (variables) with **`aes()`** and a *flavour* with **`scales`** Create *layers* to plot with **`geoms`** *Style* the cake with **`theme`** ] .pull-right[ <img src="img/like_cake.png" width="70%" height="70%" /> ] .footnote[ _Slide by [Tanya Shapiro](https://twitter.com/tanya_shapiro/status/1576935152575340544?s=20&t=IDPBrEMzKw8NNxA9TW2F4A)_] --- <div class="my-logo-right"> <img src="img/layers of ggplot.png" width="60%"/> </div> ## Grammar of Graphics: .data-theme[data] - Data is not only raw data, but can also be the results from an analysis |group |country |gender | mean_age| SD_age| CI_lower| CI_upper| |:---------|:-------|:------|--------:|------:|--------:|--------:| |BE Male |BE |Male | 39| 11.0| 17.44| 60.56| |BE Female |BE |Female | 41| 13.2| 15.13| 66.87| |BE Other |BE |Other | 36| 8.2| 19.93| 52.07| |NL Male |NL |Male | 37| 12.0| 13.48| 60.52| |NL Female |NL |Female | 36| 14.0| 8.56| 63.44| |NL Other |NL |Other | 31| 7.2| 16.89| 45.11| <br> - Data has to be 'tidy' --- <div class="my-logo-right"> <img src="img/layers of ggplot.png" width="60%"/> </div> ## Grammar of Graphics: .aes-theme[aesthetics] .pull-left[ - Describe how variables in data are *mapped* to visual properties. For example: - variables mapped on x- and y-axis - variable that defines color or size of points - ... - Change appearances of aesthetics using `scales` ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-9-1.png" width="720" style="display: block; margin: auto;" /> ] --- <div class="my-logo-right"> <img src="img/layers of ggplot.png" width="60%"/> </div> ## Grammar of Graphics: .geom-theme[geometries] .pull-left[ - Geometrical shapes at the heart of visualisation. For example: - boxplot - line - ... ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-10-1.png" width="720" style="display: block; margin: auto;" /> ] --- <div class="my-logo-right"> <img src="img/layers of ggplot.png" width="60%"/> </div> ## Grammar of Graphics: .facet-theme[facets] .pull-left[ - Also called 'small multiples' - Define how much panels are shown and how they are arranged ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-11-1.png" width="720" style="display: block; margin: auto;" /> ] --- <div class="my-logo-right"> <img src="img/layers of ggplot.png" width="60%"/> </div> ## Grammar of Graphics: .stat-theme[statistics] .pull-left[ - Data might be `tidy`, but still in need of some statistical calculations. For example: - Calculate descriptive statistics to create a boxplot - Estimate a linear (or other) model to draw a regression line in a scatter plot - Often implicit done by `ggplot2` ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-12-1.png" width="720" style="display: block; margin: auto;" /> ] --- class: inverse-green, center, middle name: part3 # 3. How `ggplot2` works (in a nutshell) --- <div class="my-logo-right"> <img src="img/palmerpenguins_hex.png" width="80%"/> </div> ## Time to get the penguins in... Nice data set that can be used within R .font60[(Source: https://allisonhorst.github.io/palmerpenguins/articles/intro.html)] ```r install.packages("palmerpenguins") library(palmerpenguins) data("penguins") ``` <img src="img/lter_penguins.png" width="50%" height="50%" /> <p align="right">.footnotesize[.SW-greenD[*Artwork by @allison_horst*]] </p> --- <div class="my-logo-right"> <img src="img/palmerpenguins_hex.png" width="80%"/> </div> ## Time to get the penguins in... Table: Table 1. Random sample of 10 observations from the Palmer Pinguins dataset |species |island | bill_length_mm| bill_depth_mm| flipper_length_mm| body_mass_g|sex | year| |:-------|:---------|--------------:|-------------:|-----------------:|-----------:|:------|----:| |Adelie |Dream | 39.6| 18.8| 190| 4600|male | 2007| |Gentoo |Biscoe | 51.1| 16.5| 225| 5250|male | 2009| |Gentoo |Biscoe | 45.2| 15.8| 215| 5300|male | 2008| |Gentoo |Biscoe | 47.5| 15.0| 218| 4950|female | 2009| |Adelie |Dream | 36.0| 17.1| 187| 3700|female | 2009| |Adelie |Dream | 36.0| 17.8| 195| 3450|female | 2009| |Gentoo |Biscoe | 43.8| 13.9| 208| 4300|female | 2008| |Adelie |Biscoe | 39.6| 20.7| 191| 3900|female | 2009| |Adelie |Torgersen | 46.0| 21.5| 194| 4200|male | 2007| |Adelie |Dream | 36.8| 18.5| 193| 3500|female | 2009| --- ## The basics of .UA-red[`ggplot2`]: *data & aesthetics* .pull-left[ ```r Plot <- ggplot( ## Step 1: data data = penguins, ## Step 2: specify aesthetics (mapping) aes( x = flipper_length_mm, y = body_mass_g) ) Plot ``` ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-17-1.png" width="360" style="display: block; margin: auto;" /> ] --- ## The basics of .UA-red[`ggplot2`]: *geometry* .pull-left[ ```r Plot <- ggplot( ## Step 1: data data = penguins, ## Step 2: specify aesthetics (mapping) aes( x = flipper_length_mm, y = body_mass_g) ) + ## Step 3: add geometry geom_point() Plot ``` ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-19-1.png" width="360" style="display: block; margin: auto;" /> ] Not every component of the *Grammar of Graphics* needs to be defined. The other components have *default* values that are automatically applied. --- ## The basics of .UA-red[`ggplot2`]: *facets* .pull-left[ ```r Plot <- ggplot( ## Step 1: data data = penguins, ## Step 2: specify aesthetics (mapping) aes( x = flipper_length_mm, y = body_mass_g) ) + ## Step 3: add geometry geom_point() + ## Step 4: define facets facet_wrap(~species) Plot ``` ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-21-1.png" width="360" style="display: block; margin: auto;" /> ] --- ## The basics of .UA-red[`ggplot2`]: *theme* .pull-left[ ```r Plot <- ggplot( ## Step 1: data data = penguins, ## Step 2: specify aesthetics (mapping) aes( x = flipper_length_mm, y = body_mass_g) ) + ## Step 3: add geometry geom_point() + ## Step 4: define facets facet_wrap(~species) + ## Step 5: set theme theme_minimal() Plot ``` ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-23-1.png" width="360" style="display: block; margin: auto;" /> ] --- ## Building a visualisation by adding layers ... <img src="slides_part-4_files/figure-html/unnamed-chunk-24-1.png" width="1152" style="display: block; margin: auto;" /> --- ## Several .UA-red[`geom_*`] options... <img src="img/ggplot2_cheatsheet.jpg" width="60%" height="60%" style="display: block; margin: auto;" /> .center[The *cheatsheet*: https://github.com/rstudio/cheatsheets/blob/main/data-visualization-2.1.pdf] --- class: inverse-green, center, middle name: part4 # 4. Visualising a categorical variable .white[*"The bar is open... Let's have a lollipop?" *] --- ## Visualising a categorical variable? .left-column[ There are many ways to visualise a categorical variable... Can you think of some?] -- .right-column[ <img src="img/data_to_viz.png" width="70%" height="70%" style="display: block; margin: auto;" /> <br> <br> .center[.font100[ *Have a look at [data to viz.com](https://www.data-to-viz.com)* ] ] ] --- ## Creating a barplot with .UA-red[`geom_bar()`] or .UA-red[`geom_col()`] .pull-left[ **`geom_bar()`** <img src="slides_part-4_files/figure-html/unnamed-chunk-27-1.png" width="216" style="display: block; margin: auto auto auto 0;" /> ] .pull-right[ **`geom_col()`** <img src="slides_part-4_files/figure-html/unnamed-chunk-28-1.png" width="216" style="display: block; margin: auto auto auto 0;" /> ] -- .pull-left[.footnotesize[ ```r ggplot( penguins, aes( x = species ) ) + geom_bar() # stat_count() does the counting automatically ``` ] ] .pull-right[.footnotesize[ ```r count_data <- penguins %>% count(species, name = 'count') ggplot( count_data, aes( x = species, y = count ) ) + geom_col() ``` ] ] --- ## Creating a barplot with .UA-red[`geom_bar()`] .pull-left[ - Add color to barplot by defining an additional *aesthetic*: **`fill`** .footnotesize[ ```r ggplot( penguins, aes( x = species ) ) + geom_bar( ## Additional aesthetic: "fill"-scale aes(fill = species) ) ``` ] ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-32-1.png" width="360" style="display: block; margin: auto;" /> ] --- ## Creating a barplot with .UA-red[`geom_bar()`] .pull-left[ - Determine `fill`-colors by adding **`scale_fill_manual()`** .footnotesize[ ```r ggplot( penguins, aes( x = species ) ) + geom_bar( aes(fill = species) ) + ## Specify fill-colors scale_fill_manual( values = c("darkorange", "purple", "cyan4") ) ``` ] ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-34-1.png" width="360" style="display: block; margin: auto;" /> ] --- ## Creating a barplot with .UA-red[`geom_bar()`] .pull-left[ - Add title, subtitle and change label of x-axis using **`labs()`** .footnotesize[ ```r ggplot( penguins, aes( x = species ) ) + geom_bar( aes(fill = species) ) + scale_fill_manual( values = c("darkorange","purple","cyan4") ) + ## Add title, subtitle and label x-axis labs( title = "Palmer penguins", subtitle = "n observations for species", x = "" ) ``` ] ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-36-1.png" width="360" style="display: block; margin: auto;" /> ] --- ## Creating a barplot with .UA-red[`geom_bar()`] .pull-left[ - Add another *theme* using **`theme_minimal()`** .footnotesize[ ```r ggplot( penguins, aes( x = species ) ) + geom_bar( aes(fill = species) ) + scale_fill_manual( values = c("darkorange","purple","cyan4") ) + labs( title = "Palmer penguins", subtitle = "n observations for species", x = "" ) + ## Choose theme theme_minimal() ``` ] ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-38-1.png" width="360" style="display: block; margin: auto;" /> ] --- ## Creating a barplot with .UA-red[`geom_bar()`] .pull-left[ - Flip x- and y-axis using **`coord_flip()`** - Remove legend using **`theme(legend.position = "none")`** .footnotesize[ ```r ggplot( penguins, aes( x = species ) ) + geom_bar( aes(fill = species) ) + scale_fill_manual( values = c("darkorange","purple","cyan4") ) + labs( title = "Palmer penguins", subtitle = "n observations for species", x = "" ) + ## Flip x- and y-axis coord_flip( ) + theme_minimal( ) + ## Remove legend theme( legend.position = "none" ) ``` ] ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-40-1.png" width="360" style="display: block; margin: auto;" /> ] --- ## Creating a lollipop plot .pull-left[ Use two *geoms*: .UA-red[**`geom_point()`**] and .UA-red[**`geom_segment()`**] .font50[ ```r penguins %>% count(species) %>% ggplot( aes( x = species, y = n) ) + geom_point( aes(col = species) ) + geom_segment( aes( x = species, xend = species, y = 0, yend = n, col = species ) ) + ``` ] Re-use remainder of code! .font50[ ```r scale_colour_manual( values = c("darkorange","purple","cyan4") ) + labs( title = "Palmer penguins", subtitle = "n observations for species", x = "" ) + coord_flip( ) + theme_minimal( ) + theme( legend.position = "none" ) ``` ] ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-43-1.png" width="360" style="display: block; margin: auto;" /> ] --- ## Creating a lollipop plot .pull-left[ - Reorder 'bars' by creating a new variable using **`fct_reorder()`** .font50[ ```r penguins %>% count(species) %>% ## Create an ordered factor mutate( species_ord = fct_reorder(species,n) ) %>% ggplot( aes(x = species_ord, y = n)) + geom_point( aes(col = species) ) + geom_segment( aes( x = species, xend = species, y = 0, yend = n, col = species ) ) + scale_colour_manual( values = c("darkorange","purple","cyan4") ) + labs( title = "Palmer penguins", subtitle = "n observations for species", x = "" ) + coord_flip( ) + theme_minimal( ) + theme( legend.position = "none" ) ``` ] ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-45-1.png" width="360" style="display: block; margin: auto;" /> ] --- ## Chosing colors ... <img src="img/colours_ggplot.jpg" width="70%" height="70%" style="display: block; margin: auto auto auto 0;" /> .footnote[http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/] --- class: inverse-blue # <i class="fas fa-laptop-code" style="color: #FF0035;"></i> Exercises `[ggplot2]` : part 1 .left-column[  ] .right-column[ - You can find the qmd-file .SW-greenD[ `Exercises_ggplot2.qmd`] at the course website. - Download the qmd-file .SW-greenD[ `Exercises_ggplot2.qmd`] to your laptop - Open the file in `RStudio` - The file contains a set of coding assignments with empty code blocks - Now, we focus on part 1 of the exercises - Write the code (and test it by running it) - Stuck? No Worries! - We are there - Help each other - There is a solution key (at the website) (.SW-greenD[`Exercises_ggplot2_solutions.qmd`]) ] --- class: inverse-green, center, middle name: part5 # 5. Visualising a quantitative variable --- ## Visualising a quantitative variable? .left-column[ There are many ways to visualise a quantitative variable... Can you think of some?] -- .right-column[ <img src="img/fundamentals_viz.png" width="45%" height="45%" style="display: block; margin: auto;" /> <br> .right[.font60[ *Taken from [Fundamentals of Data Visualization](https://clauswilke.com/dataviz/directory-of-visualizations.html) by Claus Wilke* ] ] ] --- ## Creating a histogram with .UA-red[`geom_histogram()`] .pull-left[ .footnotesize[ ```r ggplot( penguins, aes( x = flipper_length_mm ) ) + geom_histogram() ``` ] ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-49-1.png" width="360" style="display: block; margin: auto;" /> ] --- ## Creating a histogram with .UA-red[`geom_histogram()`] .pull-left[ - Change breaks on y-axis using **`scale_y_continuous()`** .footnotesize[ ```r ggplot( penguins, aes( x = flipper_length_mm ) ) + geom_histogram() + ## Specify breaks scale_y_continuous( breaks = c(seq(0, 25, 5), 29) ) ``` ] ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-51-1.png" width="360" style="display: block; margin: auto;" /> ] --- ## Creating a histogram with .UA-red[`geom_histogram()`] .pull-left[ - Change transparency using argument **`alpha`** - Remove minor grid lines using argument **`panel.grid.minor = element_blank()`** .font60[ ```r ggplot( penguins, aes( x = flipper_length_mm, fill = species ) ) + geom_histogram( ## Change transparency of points alpha = .7 ) + ## Specify breaks scale_y_continuous( breaks = c(seq(0, 25, 5), 29) ) + scale_fill_manual( values = c("darkorange","purple","cyan4") ) + labs( title = "Palmer penguins", subtitle = "Histogram of flipper length", x = "Flipper length" ) + theme_minimal() + theme( ## Remove minor grid lines panel.grid.minor = element_blank() ) ``` ] ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-53-1.png" width="360" style="display: block; margin: auto;" /> ] --- ## Creating a density plot<sup>°</sup> with .UA-red[`geom_density()`] .pull-left[ - Replace `geom_histogram()` with **`geom_density()`** plot .footnotesize[ ```r ggplot( penguins, aes( x = flipper_length_mm, fill = species ) ) + ## Use geom_density geom_density( alpha = .7 ) + scale_fill_manual( values = c("darkorange","purple","cyan4") ) + labs( title = "Palmer penguins", subtitle = "Density plot of flipper length", x = "Flipper length" ) + theme_minimal() ``` .left[.font70[ <sup>*°*</sup>*surface below curve = 100%* ]] ] ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-55-1.png" width="360" style="display: block; margin: auto;" /> ] --- ## Creating a density plot with .UA-red[`geom_density()`] .pull-left[ - Only colored lines by replacing argument **`fill`** with **`color`** .footnotesize[ ```r ggplot( penguins, aes( x = flipper_length_mm, color = species ) ) + geom_density() + ## Replace scale_color with scale_fill scale_color_manual( values = c("darkorange","purple","cyan4") ) + labs( title = "Palmer penguins", subtitle = "Density plot of flipper length", x = "Flipper length" ) + theme_minimal() + theme( panel.grid.minor = element_blank() ) ``` ] ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-57-1.png" width="360" style="display: block; margin: auto;" /> ] --- ## Or even better ... .UA-red[`geom_violin()` + `geom_jitter()`] .pull-left[ .font50[ ```r ggplot( penguins, aes( x = species, y = flipper_length_mm, ## Aesthetics fill and color applied to EVERY geom fill = species, color = species ) ) + ## Use geom_violin and geom_jitter geom_violin( alpha = .65 ) + geom_jitter( alpha = .7 ) + scale_fill_manual( values = c("darkorange","purple","cyan4") ) + scale_color_manual( values = c("darkorange","purple","cyan4") ) + labs( title = "Palmer penguins", subtitle = "Density plot voor flipper lengte", y = "Flipper length", x = "", ) + theme_minimal() + theme( legend.position = "none" ) ``` ] ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-59-1.png" width="360" /> ] --- ## Or even better .UA-red[`geom_violin()` + `geom_jitter()`] .pull-left[ - Put names of species on top using argument **`position="top"`** - Remove minor grid lines using argument **`panel.grid.major = element_blank()`** .font40[ ```r ggplot( penguins, aes( x = species, y = flipper_length_mm, fill = species, color = species ) ) + geom_violin( alpha = .65 ) + geom_jitter( alpha = .7 ) + scale_x_discrete( position = "top" ) + scale_fill_manual( values = c("darkorange","purple","cyan4") ) + scale_color_manual( values = c("darkorange","purple","cyan4") ) + labs( title = "Palmer penguins", subtitle = "Density plot of flipper length", y = "Flipper length", x = "", ) + theme_minimal() + theme( legend.position = "none", panel.grid.minor = element_blank(), ## Remove major grid lines panel.grid.major.x = element_blank() ) ``` ] ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-61-1.png" width="360" /> ] --- ## Or a *rain cloud plot* <img src="slides_part-4_files/figure-html/unnamed-chunk-62-1.png" width="432" style="display: block; margin: auto;" /> .center[.font60[*Based on the tutorial of Cédric Scherer: https://www.cedricscherer.com/*]] --- name: raincloud ## Or a *rain cloud plot* ([Appendix 1: How to grow a rain cloud plot?](#appendix1)) Additional package required: .UA-red[`ggdist`] .footnotesize[ ```r library(ggdist) ggplot(penguins, aes(x = species, y = flipper_length_mm)) + stat_halfeye( adjust = .5, width = .6, .width = 0, justification = -.2, point_colour = NA ) + geom_boxplot( width = .15, outlier.shape = NA ) + geom_point( size = 1.3, alpha = .3, position = position_jitter( seed = 1, width = .1 )) + labs( title = "Palmer penguins", subtitle = "Distribution of flipper length by species", x = "", y = "Flipper length" ) + coord_cartesian(xlim = c(1.2, NA), clip = "off") + coord_flip() + theme_minimal() ``` ] --- class: inverse-blue # <i class="fas fa-laptop-code" style="color: #FF0035;"></i> Exercises `[ggplot2]` : part 2 .left-column[  ] .right-column[ - You can find the qmd-file .SW-greenD[ `Exercises_ggplot2.qmd`] at the course website. - Download this file to your laptop - Open the file in `RStudio` - The file contains a set of coding assignments with empty code blocks - Now, we focus on part 2 of the exercises - Write the code (and test it by running it) - Stuck? No Worries! - We are there - Help each other - There is a solution key (.SW-greenD[`Exercises_ggplot2_solutions.qmd`]) ] --- class: inverse-green, center, middle name: part6 # 6. Visualising more than one variable --- ## Visualising more than one variable? .left-column[ There are many ways to visualise more than one variable... The choice depends (among other things) on the type of variables you want to plot: - only quantitative variables; - only qualitative variables; - or a combination of both Can you think of an example of each?] -- .right-column[ <img src="img/data_to_viz2.png" width="70%" height="70%" style="display: block; margin: auto;" /> <br> .center[.font100[ *Have a look at [data to viz.com](https://www.data-to-viz.com)* ] ] ] --- ## Visualising more than one variable <br> <br> We focus today upon: - **scatterplots** .font80[(two numeric variables, two numeric variables and one categorical variable)] - **grouped barplots** .font80[(two categorical variables)] --- ## Creating a scatterplot with .UA-red[`geom_point()`] .pull-left[ .footnotesize[ ```r ggplot( penguins, aes( x = body_mass_g, y = flipper_length_mm ) ) + geom_point() + labs( title = "Palmer penguins", subtitle = "Relation of flipper length with body mass", y = "Flipper length", x = "Body mass", ) + theme_minimal() + theme( legend.position = "none" ) ``` ] ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-66-1.png" width="360" /> ] --- ## Creating a scatterplot with .UA-red[`geom_point()`] and .UA-red[`geom_smooth()`] .pull-left[ .footnotesize[ - Add trend line using **`geom_smooth()`** ```r ggplot( penguins, aes( x = body_mass_g, y = flipper_length_mm ) ) + geom_point() + ## Add a trendline geom_smooth() + labs( title = "Palmer penguins", subtitle = "Relation of flipper length with body mass", y = "Flipper length", x = "Body mass" ) + theme_minimal() + theme( legend.position = "none" ) ``` ] ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-68-1.png" width="360" /> ] --- ## Creating a scatterplot with .UA-red[`geom_point()`] and .UA-red[`geom_smooth()`] .pull-left[ - Linear trend line using argument **`method = "lm"`** - Remove confidence intervals using argument **`se = FALSE`** .footnotesize[ ```r ggplot( penguins, aes( x = body_mass_g, y = flipper_length_mm ) ) + geom_point() + geom_smooth( ## Add linear trend line method = "lm", ## Remove confidence band se = FALSE ) + labs( title = "Palmer penguins", subtitle = "Correlation between flipper length and body mass", y = "Flipper length", x = "Body mass", ) + theme_minimal() + theme( legend.position = "none" ) ``` ] ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-70-1.png" width="360" style="display: block; margin: auto;" /> ] --- ## Creating a scatterplot with .UA-red[`geom_point()`] and .UA-red[`geom_smooth()`] .pull-left[ - Add shape and color by specifying *aesthetics*: **`shape`** and **`color`** - Change legend position and background, position of plot title, and layout of (sub)title .font40[ ```r ggplot( penguins, aes( x = flipper_length_mm, y = body_mass_g ) ) + geom_point( aes( color = species, shape = species ), size = 3, alpha = 0.8 ) + geom_smooth( aes(color = species), se = F, method = "lm" ) + theme_minimal() + scale_color_manual( values = c("darkorange","purple","cyan4")) + labs( title = "Palmer penguins", subtitle = "Correlation between flipper length and body mass", y = "Flipper length", x = "Body mass", color = "Species", shape = "Species") + theme( legend.position = c(0.2, 0.7), legend.background = element_rect(fill = "white", color = NA), plot.title.position = "plot", plot.title = element_text(hjust = 0, face= "bold"), plot.subtitle = element_text(hjust = 0, face= "italic") ) ``` ] ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-72-1.png" width="360" style="display: block; margin: auto;" /> ] --- ## Creating a scatterplot with .UA-red[`geom_point()`] and .UA-red[`geom_smooth()`] .pull-left[ - Adds facets using **`facet_wrap()`** .font60[ ```r ggplot( penguins, aes( x = body_mass_g, y = flipper_length_mm ) ) + geom_point( ) + geom_smooth( method = "lm", se = FALSE ) + facet_wrap(~species) + labs( title = "Palmer penguins", subtitle = "Correlation between flipper length and body mass", y = "Flipper length", x = "Body mass", ) + theme_minimal() + theme( legend.position = "none", plot.title = element_text(hjust = 0, face= "bold"), plot.subtitle = element_text(hjust = 0, face= "italic") ) ``` ] ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-74-1.png" width="360" style="display: block; margin: auto;" /> ] --- ## Creating a barplot of two variables (counts) .pull-left[ Grouped barplot of counts using **`position_dodge()`** <img src="slides_part-4_files/figure-html/unnamed-chunk-75-1.png" width="216" style="display: block; margin: auto auto auto 0;" /> ] .pull-right[ Stacked barplot of counts using argument **`position_stack()`** <img src="slides_part-4_files/figure-html/unnamed-chunk-76-1.png" width="216" style="display: block; margin: auto auto auto 0;" /> ] -- .pull-left[.footnotesize[ ```r penguins %>% ggplot( aes( x = island, fill = species ) ) + geom_bar( position = position_dodge() ) + theme_minimal() ``` ] ] .pull-right[.footnotesize[ ```r penguins %>% ggplot( aes( x = island, fill = species) ) + geom_bar( position = position_stack() ) + theme_minimal() ``` ] ] --- ## Creating a barplot of two variables (percentage) .pull-left[ - Switch to relative frequencies using **`position_fill()`** ```r penguins %>% ggplot( aes( x = island, fill = species ) ) + geom_bar( position = position_fill() ) + theme_minimal() ``` ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-80-1.png" width="360" style="display: block; margin: auto;" /> ] --- ## Creating a barplot of two variables (percentage) .pull-left[ - Add texts to bars using **`geom_text()`** - Add "fill" colors using **`scale_fill_brewer()`** - Remove additional space using **`coord_cartesian(expand=FALSE)`** .font40[ ```r penguins %>% ggplot( aes( x = island, fill = species ) ) + geom_bar( position = position_fill(), alpha = .9 ) + geom_text( aes(label = ..count..), stat = "count", colour = "white", position = position_fill(vjust = 0.5) ) + scale_fill_brewer( type = "qual", palette = 6 ) + scale_x_discrete(position = "top")+ scale_y_continuous(breaks = c(0.5, 1)) + labs( title = "Palmer penguins", subtitle = "Distribution of penguin species by island", fill = "Species" ) + coord_cartesian(expand = FALSE) + theme_minimal() + theme( plot.title = element_text(size = 20, face = "bold"), plot.subtitle = element_text(size = 16, face = "italic"), axis.title = element_blank(), axis.text.x = element_text(size = 14, face = "bold"), legend.title = element_text(face = "bold"), panel.grid.minor = element_blank(), panel.grid.major.x = element_blank() ) ``` ] ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-82-1.png" width="504" /> ] --- ## Chosing colors ... .pull-left[ - Colors are not meaningless and ... they grab attention - R has several built-in color palettes. The functions `scale_fill_brewer` and `scale_color_brewer` use palettes of [RColorBrewer](https://r-graph-gallery.com/38-rcolorbrewers-palettes.html). These palettes come in three 'flavours': - **sequential**: for ordered data .font80[((for example, scoring low/mediocre/high on a likert scale)] - **qualitative**: for nominal or categorical information .font80[(for example, different islands)] - **diverging**: for ordered data with meaningful mid values .font80[((for example, temperature or change in score)] ] .pull-right[ <img src="http://www.sthda.com/sthda/RDoc/figure/graphs/colors-in-r-rcolorbrewer-palettes.png" width="65%" style="display: block; margin: auto;" /> ] --- class: inverse-blue # <i class="fas fa-laptop-code" style="color: #FF0035;"></i> Exercises `[ggplot2]` : part 3 .left-column[  ] .right-column[ - You can find the qmd-file .SW-greenD[ `Exercises_ggplot2.qmd`] at the course website. - Download this file to your laptop - Open the file in `RStudio` - The file contains a set of coding assignments with empty code blocks - Now, we focus on part 3 of the exercises - Write the code (and test it by running it) - Stuck? No Worries! - We are there - Help each other - There is a solution key (.SW-greenD[`Exercises_ggplot2_solutions.qmd`]) ] --- class: inverse-green, center, middle name: part7 # 7. More about visualisation? --- ## The tip of the icebeRg... .pull-left[ .center[ [Gallery of different types of visualisations accompanied by ggplot-code](https://r-graph-gallery.com/index.html)] <img src="img/R_gallery.png" width="60%" height="60%" style="display: block; margin: auto;" /> ] .pull-right[ .center[ [Top50 ggplot visualisations...](http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html)] <img src="img/top50.png" width="60%" height="60%" style="display: block; margin: auto;" /> ] .pull-left[ .center[ [library of graphs with ggplot-code](https://r-charts.com/ggplot2/) ] <img src="img/rchart.png" width="60%" height="60%" style="display: block; margin: auto;" /> ] .pull-right[ .center[ [ggplot-extentions](https://exts.ggplot2.tidyverse.org/gallery/) ] <img src="img/extensions.png" width="65%" height="65%" style="display: block; margin: auto;" /> ] --- ## Which visualisation to use? .pull-left[ .center[ [Visual vocabulary](http://ft-interactive.github.io/visual-vocabulary/)] <img src="img/vocabulary.png" width="60%" height="60%" style="display: block; margin: auto;" /> ] .pull-right[ .center[ [The Data Visualisation Catalogue](https://datavizcatalogue.com/search.html)] <img src="img/catalogue.png" width="40%" height="40%" style="display: block; margin: auto;" /> ] <br> .pull-left[ .center[ [Research visuals: all the resources ...](https://baryon.be/visuals-resources/) ] <img src="img/visuals.png" width="60%" height="60%" style="display: block; margin: auto;" /> ] .pull-right[ .center[ [Website of Cedric Scherer](https://www.cedricscherer.com/)] <img src="img/cedric.png" width="60%" height="60%" style="display: block; margin: auto;" /> ] --- ## In need of help? .pull-left[**Don't forget RStudio's help-function!** <br> <img src="img/help.png" width="70%" height="70%" /> ] .pull-right[ **Google is your best fRiend...** Just google your question and you'll find code, examples, ... **Generative AI can help as well!** ] --- class: inverse-green, center, middle name: appendix1 # Appendix 1 *How to grow a rain cloud plot?* <br> <br><br> [Back to slide show...](#raincloud) --- ## Step 1 .pull-left[ .footnotesize[ ```r library(ggdist) P1 <- ggplot( penguins, aes( x = species, y = flipper_length_mm ) ) + stat_halfeye() P1 ``` ] ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-94-1.png" width="504" style="display: block; margin: auto;" /> ] --- ## Step 2 .pull-left[ .footnotesize[ ```r P1 <- ggplot( penguins, aes( x = species, y = flipper_length_mm ) ) + stat_halfeye( adjust = .5, width = .6, .width = 0, justification = -.2, point_colour = NA ) P1 ``` ] ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-96-1.png" width="504" style="display: block; margin: auto;" /> ] --- ## Step 3 .pull-left[ .footnotesize[ ```r P2 <- P1 + geom_boxplot( width = .15, outlier.shape = NA ) P2 ``` ] ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-98-1.png" width="504" style="display: block; margin: auto;" /> ] --- ## Step 4 .pull-left[ .footnotesize[ ```r P3 <- P2 + geom_point( size = 1.3, alpha = .3, position = position_jitter( seed = 1, width = .1 ) ) P3 ``` ] ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-100-1.png" width="504" style="display: block; margin: auto;" /> ] --- ## Step 5 .pull-left[ .footnotesize[ ```r P4 <- P3 + labs( title = "Palmer penguins", subtitle = "Distribution of flipper length by species", x = "", y = "Flipper length" ) P4 ``` ] ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-102-1.png" width="504" style="display: block; margin: auto;" /> ] --- ## Step 6 .pull-left[ .footnotesize[ ```r P5 <- P4 + coord_cartesian(xlim = c(1.2, NA), clip = "off") + coord_flip() + theme_minimal() + theme( plot.title.position = "plot", plot.title = element_text(face = "bold"), plot.subtitle = element_text(face = "italic") ) P5 ``` ] ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-104-1.png" width="504" style="display: block; margin: auto;" /> ] --- class: inverse-green, center, middle ## THE RAIN CLOUD PLOTS / [Back to slide show...](#raincloud) <img src="slides_part-4_files/figure-html/unnamed-chunk-105-1.png" width="504" style="display: block; margin: auto;" />