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/edubron-en-rgb.jpg" width="100%" /> </div> <div class="my-logo-right"> <img src="img/Logo Methods Hub.png" width="100%"/> </div> # A reProducible woRkflow with Quarto .font160[ .SW-greenD[Data visualisations] ] .font120[ .SW-greenD[*Powerful visualisations with*] .UA-red[*`ggplot2`*] ] Sven De Maeyer & Tine van Daal --- class: inverse-green, left # Overview .center2[ 1. The Grammar of Graphics --- ([click here](#part1)) 2. How `ggplot2` works (in a nutshell) --- ([click here](#part2)) 3. Visualising a categorical variable --- ([click here](#part3)) 4. Visualising a quantitative variable --- ([click here](#part4)) 5. Visualising more than one variable --- ([click here](#part5)) ] --- class: inverse-green, center, middle name: part1 # 1. The Grammar of Graphics --- ## A theoretical approach to visualisation .left-column[ <img src="img/Grammar_of_Graphics_cover.jpeg" alt="" width="80%" height="80%" /> ] .right-column[ Every visualisation is made up of the same components (called layers) One system (grammar) of building blocks to create (any) visualisation At the heart of several modern graphical applications: - **ggplot2** - Tableau - Vega-Lite - Observable Plot ] --- ## 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[  .footnote[ _Animation by [Thomas de Beus](https://medium.com/tdebeus/think-about-the-grammar-of-graphics-when-improving-your-graphs-18e3744d8d18)_] ] <br> <br> <br> --- <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-4-1.png" alt="" 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-5-1.png" alt="" 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-6-1.png" alt="" 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 is `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-7-1.png" alt="" width="720" style="display: block; margin: auto;" /> ] --- ## Grammar of Graphics 'works' a bit like *a cake* .pull-left[ <br> <br> 1. Start by setting up the *foundation* with **`ggplot()`** 2. Specify *ingredients* (variables) with **`aes()`** and a *flavour* with **`scales`** 3. Create *layers* to plot with **`geoms`** 4. *Style* the cake with **`theme`** ] .pull-right[ <img src="img/like_cake.png" alt="" width="70%" height="70%" /> ] .footnote[ _Slide by [Tanya Shapiro](https://www.tanyashapiro.com/)_ ] --- class: inverse-green, center, middle name: part2 # 2. 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 in 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" alt="" 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: 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| |:---------|:------|--------------:|-------------:|-----------------:|-----------:|:------|----:| |Gentoo |Biscoe | 47.5| 14.2| 209| 4600|female | 2008| |Chinstrap |Dream | 50.5| 19.6| 201| 4050|male | 2007| |Gentoo |Biscoe | 46.9| 14.6| 222| 4875|female | 2009| |Adelie |Biscoe | 42.2| 19.5| 197| 4275|male | 2009| |Adelie |Biscoe | 40.6| 18.6| 183| 3550|male | 2007| |Gentoo |Biscoe | 46.2| 14.4| 214| 4650|NA | 2008| |Gentoo |Biscoe | 45.5| 13.9| 210| 4200|female | 2008| |Adelie |Biscoe | 40.1| 18.9| 188| 4300|male | 2008| |Chinstrap |Dream | 52.7| 19.8| 197| 3725|male | 2007| |Adelie |Biscoe | 37.7| 18.7| 180| 3600|male | 2007| --- ## 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-13-1.png" alt="" 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-15-1.png" alt="" width="360" style="display: block; margin: auto;" /> ] Not every component of the *Grammar of Graphics* needs to be defined. Several components have *default* values that are applied automatically. --- ## 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-17-1.png" alt="" 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-19-1.png" alt="" width="360" style="display: block; margin: auto;" /> ] --- ## Building a visualisation by adding layers ... <img src="slides_part-4_files/figure-html/unnamed-chunk-20-1.png" alt="" width="1152" style="display: block; margin: auto;" /> --- ## Several .UA-red[`geom_*`] options... <img src="img/ggplot2_cheatsheet.jpg" alt="" 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: part3 # 3. 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" alt="" 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-23-1.png" alt="" width="216" style="display: block; margin: auto auto auto 0;" /> ] .pull-right[ **`geom_col()`** <img src="slides_part-4_files/figure-html/unnamed-chunk-24-1.png" alt="" width="216" style="display: block; margin: auto auto auto 0;" /> ] -- .pull-left[.footnotesize[ ``` r ggplot( data = penguins, aes( x = species ) ) + geom_bar() # stat_count() does the counting automatically ``` ] ] .pull-right[.footnotesize[ ``` r count_data <- penguins %>% count(species, name = 'count') ggplot( data = count_data, aes( x = species, y = count ) ) + geom_col() ``` ] ] --- ## Creating a barplot with .UA-red[`geom_bar()`] .pull-left[ Color to bars by the variable `species` by defining an additional *aesthetic*: **`fill`** .footnotesize[ ``` r ggplot( data = 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-28-1.png" alt="" 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( data = 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-30-1.png" alt="" width="360" style="display: block; margin: auto;" /> ] --- ## Creating a barplot with .UA-red[`geom_bar()`] .pull-left[ Add a title, subtitle and change the title of the x-axis using **`labs()`** .footnotesize[ ``` r ggplot( data = penguins, aes( x = species ) ) + geom_bar( aes(fill = species) ) + scale_fill_manual( values = c("darkorange","purple","cyan4") ) + ## Add title, subtitle and change title of x-axis labs( title = "Palmer penguins", subtitle = "n observations for species", x = "" ) ``` ] ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-32-1.png" alt="" 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( data = 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-34-1.png" alt="" width="360" style="display: block; margin: auto;" /> ] --- ## Creating a barplot with .UA-red[`geom_bar()`] .pull-left[ Flip the x- and y-axis using **`coord_flip()`** Remove the legend using **`theme(legend.position = "none")`** .font60[ ``` r ggplot( data = 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 the legend theme( legend.position = "none" ) ``` ] ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-36-1.png" alt="" width="360" style="display: block; margin: auto;" /> ] --- ## The magic of ggplot2... .pull-left[ Change a barplot into a lollipop plot by **using two other 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 ) ) + ``` ] and reuse the 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-39-1.png" alt="" width="360" style="display: block; margin: auto;" /> ] --- ## Chosing colors ... <img src="img/colours_ggplot.jpg" alt="" 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: part4 # 4. 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" alt="" 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( data = penguins, aes( x = flipper_length_mm ) ) + geom_histogram() ``` ] ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-43-1.png" alt="" 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( data = penguins, aes( x = flipper_length_mm ) ) + geom_histogram() + ## Specify breaks scale_y_continuous( breaks = seq(0, 35, 5) ) ``` ] ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-45-1.png" alt="" 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( data = penguins, aes( x = flipper_length_mm, fill = species ) ) + geom_histogram( ## Change transparency of points alpha = .7 ) + scale_y_continuous( breaks = seq(0, 35, 5), ) + 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-47-1.png" alt="" 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()`** to create a density plot .footnotesize[ ``` r ggplot( data = 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-49-1.png" alt="" width="360" style="display: block; margin: auto;" /> ] --- ## Creating a density plot with .UA-red[`geom_density()`] .pull-left[ Replace the argument **`fill`** with **`color`** for coloured lines only .footnotesize[ ``` r ggplot( data = 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() ``` ] ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-51-1.png" alt="" width="360" style="display: block; margin: auto;" /> ] --- ## Creating a density plot with .UA-red[`geom_density()`] .pull-left[ Add the arguments **`plot.title`** and **`plot.subtitle`** to `theme()` to change the lay-out of the (sub)title .footnotesize[ ``` r ggplot( data = penguins, aes( x = flipper_length_mm, color = species ) ) + geom_density() + scale_color_manual( values = c("darkorange","purple","cyan4") ) + labs( title = "Palmer penguins", subtitle = "Density plot of flipper length", x = "Flipper length" ) + theme_minimal() + theme( ## Print title in bold plot.title = element_text(face = "bold"), ## Print subtitle in italics plot.subtitle = element_text(face = "italic") ) ``` ] ] .pull-right[ <img src="slides_part-4_files/figure-html/unnamed-chunk-53-1.png" alt="" width="360" style="display: block; margin: auto;" /> ] --- 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: part5 # 5. 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" alt="" 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-56-1.png" alt="" 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-58-1.png" alt="" 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-60-1.png" alt="" 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-62-1.png" alt="" 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-64-1.png" alt="" 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-65-1.png" alt="" 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-66-1.png" alt="" 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-70-1.png" alt="" 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-72-1.png" alt="" 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" alt="" 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`]) ] ---