class: center, middle ![:scale 30%](/assets/images/coding_club_logo_1.png) # 26 MARCH 2024 ## INBO coding club Herman Teirlinck Building 01.69 - Paul Janssen --- class: left, top ## ROOMIE: room reservation ``` > if (isFALSE(roomie)) { + warning("Please confirm asap the room reservation on the roomie") + } Warning message: Please confirm asap the room reservation on the roomie ``` --- class: left, top ## Record the session Kind reminder to... myself. --- class: left, top ## New Hexagon Stickers New INBO coding club stickers have been ordered (thanks to Tessa and her team). Be sure to join us for any of the next sessions to get one! ![:scale 40%](/assets/images/20240326/20240326_coding_club_logo.png) --- class: center, middle ![:scale 90%](/assets/images/20240326/20240326_badge_ggplot.png) --- class: center, top ![:scale 85%](/assets/images/20240326/20240326_cheat_sheet_ggplot.png)
Download [cheatsheet](https://github.com/inbo/coding-club/blob/master/cheat_sheets/20240229_cheat_sheet_ggplot2.pdf) We go "beyond ggplot", but the ggplot cheatsheet can still be useful. --- class: left, top ## How to get started? Check the [Each session setup](https://inbo.github.io/coding-club/gettingstarted.html#each-session-setup) to get started. ## First time coding club? Check the [First time setup](https://inbo.github.io/coding-club/gettingstarted.html#first-time-setup) section to setup. --- class: left, top ![:scale 100%](/assets/images/coding_club_sticky_concept.png) --- class: center, top # Share your code during the coding session Go to https://hackmd.io/t6cbOs9iSCqBiUbYLyfkBQ?both and start by adding your name in section "Participants".
--- class: left, top # Download data and code You can download the material of today: - automatically via `inborutils::setup_codingclub_session()`* - manually** from GitHub foders [coding-club/data/20240229](https://github.com/inbo/coding-club/tree/master/data/20240229) and [coding-club/src/20240229](https://github.com/inbo/coding-club/tree/master/src/20240229)
__\* Note__: you can use the date in "YYYYMMDD" format to download the coding club material of a specific day, e.g. run `setup_codingclub_session("20230228")` to download the coding club material of February, 28 2023. If date is omitted, i.e. `setup_codingclub_session()`, the date of today is used. For all options, check the [tutorial online](https://inbo.github.io/tutorials/tutorials/r_setup_codingclub_session/).
__\*\* Note__: check the getting started instructions on [how to download a single file](https://inbo.github.io/coding-club/gettingstarted.html#each-session-setup)
--- class: left, top # Data and scripts description Today we will work with: - `20240326_rodents.txt`, a tab separated file with observations of 5 rodents species in Belgium up to 2020. - `20240326_challenges.R`: R script with code to start with --- class: left, top # Load libraries ```r library(tidyverse) library(plotly) library(scales) library(patchwork) library(ggforce) library(gganimate) library(magick) ``` --- class: left, top # RECAP: ggplot recipe: data - mapping - geometry ![:scale 80%](/assets/images/20240326/20240326_ggplot_recipe.png) --- class: left, top ## Recap: mapping Mapping can be defined in either `ggplot()` or the geometry `geom_*()`. ```r # mapping in ggplot() ggplot(ToothGrowth, mapping = aes(x = dose, y = len)) + geom_point() # mapping in geometry geom_point() ggplot(ToothGrowth) + geom_point(mapping = aes(x = dose, y = len)) ``` What to do with multiple plots (geometries)? ```r # set all args of aes() in each geom_*() ggplot(ToothGrowth) + geom_violin(mapping = aes(x = dose, y = len, group = dose)) + geom_point(mapping = aes(x = dose, y = len)) # put common args of aes() in ggplot() ggplot(ToothGrowth, mapping = aes(x = dose, y = len)) + geom_violin(mapping = aes(group = dose)) + geom_point() ``` --- class: left, top ## Recap: mapping - aesthetics The most important mapping argument? **aes()**thetics! Put all arguments which require variables (= columns) in `aes()`, leave all other arguments out. ```r # you want color represents the supplement (supp) ggplot(ToothGrowth, mapping = aes(x = dose, y = len)) + geom_point(aes(color = supp)) # you want all points are blue hollow triangles ggplot(ToothGrowth, mapping = aes(x = dose, y = len)) + geom_point(color = "blue", shape = 2) ``` --- background-image: url(/assets/images/background_challenge_1.png) class: left, top # Challenge 1. Interactivity and scales 1. Make `plot1` interactive with plotly. Hint: check the plotly [Getting Started](https://plotly.com/ggplot2/getting-started/). 2. Months are integers. How to improve the x breaks in plot2 to avoid decimal values? Hint: use `pretty_breaks()` from scales package within the right ggplot `scale_*` function. 3. Starting from plot3, use 'yy notation for x-labels (e.g. '12 instead of 2012). For y-labels, use "K" suffix for thousands (e.g. 2K for 2000). Hint: example code in ["Break and labels"](https://scales.r-lib.org/#breaks-and-labels) section of scales homepage. --- class: left, top # Intermezzo. How to choose a color palette: the viridis palette Today we won't speak about colors. Still, take in mind this criteria when choosing a color palette: 1. printer-friendly (gray scale) 2. perceptually uniform 3. easy to read by those with colorblindness. All of this can be found in the [viridis palette](https://www.r-bloggers.com/2018/07/ggplot2-welcome-viridis/) which avoids the red and goes from blue to yellow, passing through green (_viridis_: green in Latin). --- class: left, top # Intermezzo. The INBO official colors Do you know that INBO has its own (color) theme? It's called [INBOtheme](https://inbo.github.io/INBOtheme/) and it's a package. Install it, load it and all figures will have authomatically an INBO touch. Read the nice [tutorials](https://inbo.github.io/INBOtheme/articles/index.html) for more. ```r library(INBOtheme) ggplot(data = rodentia, mapping = aes(x = year, fill = institutionCode)) + geom_bar() + labs(y = "observations") ``` ![:scale 40%](/assets/images/20240326/20240326_INBOtheme.png) --- background-image: url(/assets/images/background_challenge_2.png) class: left, top # Challenge 2. Compositions 1. Try to make a nice composition of the 4 plots. You can use the improved versions of plot2 and plot3 created in challenge 1 of course. Tip: check the very well written [patchwork documentation](https://patchwork.data-imaginist.com/articles/patchwork.html). 2. Using [ggforce](https://ggforce.data-imaginist.com/index.html), add a "facet zoom" to `plot1` focussing on the last 5 years. --- background-image: url(/assets/images/background_challenge_3.png) class: left, top # Challenge 3. Animations Let's animate plot4 with [gganimate](https://gganimate.com/articles/gganimate.html). Check also the [Getting Started](https://gganimate.com/articles/gganimate.html) page. In particular, pay attention to the [Object Permanence](https://gganimate.com/articles/gganimate.html#object-permanence) concept. Also add a title that dynamically shows the species and time as it passes. Test some `enter_*` and `exit_*` functions to animate the appearance and disappearance of data. Save the animations. See the two exercises in the next slide. --- background-image: url(/assets/images/background_challenge_3.png) class: left, top # Challenge 3. Animations 1. Create an animation showing the observations species by species. See example below. 2. Create an animation showing the observations year by year. Hint: improve the aesthetics of `geom_point` of plot4. By default the animation has 100 frames and 10 frame per seconds (fps), i.e. a duration of 10s. How can you change these values? ![:scale 60%](/assets/images/20240326/20240326_animation_plot4_by_species.png) --- class: left, top # Bonus challenge. Add image to plot The R package [magick](https://docs.ropensci.org/magick/index.html) is the R interface for ImageMagick, the most comprehensive open-source image processing library available. Using magick, add the INBO coding club logo to one of our plots. ![:scale 60%](/assets/images/20240326/20240326_logo.png) --- class: left, top # Resources - Extended and commented [solutions](https://github.com/inbo/coding-club/blob/main/src/20240326/20240326_challenges_solutions.R) are online available. - The edited [video recording](https://vimeo.com/928549430?share=copy) is available on our [vimeo channel](https://vimeo.com/user/8605285/folder/1978815). - `ggplot2` website: https://ggplot2.tidyverse.org/ - [`ggplot2` extension packages gallery](https://exts.ggplot2.tidyverse.org/gallery/) - Make interactive plots using [plotly](https://plotly-r.com/). In particular, [apply plotly to ggplot](https://plotly.com/ggplot2/getting-started/) - Adapt breaks and labels of your plots with [scales](https://scales.r-lib.org/) package - Arrange intuitively multiple plots with [patchwork](https://patchwork.data-imaginist.com/) - Explore [ggforce](https://ggforce.data-imaginist.com/): a kind of repository of geoms, stats, etc. that are as well documented and implemented as the official ones found in ggplot2 - Animate your plots with [gganimate](https://gganimate.com/) - Process images as a pro in R via [magick](https://docs.ropensci.org/magick/index.html) - [INBOtheme](https://inbo.github.io/INBOtheme/) homepage - [viridis palette](https://www.r-bloggers.com/2018/07/ggplot2-welcome-viridis/): what is it? --- class: center, middle ![:scale 30%](/assets/images/coding_club_logo_1.png) Room: 01.71 - Frans Breziers
Date: __25/04/2024__, van 10:00 tot 12:30
Subject: to be decided
(registration announced via DG_useR@inbo.be)