owned this note
owned this note
Published
Linked with GitHub
# INBO CODING CLUB
30 September 2021
Welcome!
## Share your code snippet
If you want to share your code snippet, copy paste your snippet within a section of three backticks (```):
As an **example**:
```
library(tidyverse)
```
(*you can copy paste this example and add your code further down*)
## Yellow sticky notes
Sticky notes are back! But still, some of us are remotely connected. Add you name here below and add a star for each challenge you could solve.
## Participants
Name | Challenges
--- | ---
Damiano Oldoni |
Emma Cartuyvels|**
Lynn Pallemaerts |**
Dirk Maes |
Tom De Dobbelaer |**
Jasmijn Hillaert |**
Cécile Herr |***
Adriaan Seynaeve |
Matthieu Chastel |
Marijke Thoonen |
Geert Spanoghe
Suzanna Lettens |**
Jolien Goossens |**
Maud Raman |
## Challenge 1
Cécile:
```
mypalette <- c("#391B15","#454961","#3F825A","#DE9250")
# Or from: https://sashamaps.net/docs/resources/20-colors/
mypalette <- c("black", "#4363d8", "#f58231", "#a9a9a9")
#' 2. How to use it for user case 1?
usercase1 <-
ggplot(area_biotopes_pararge,
aes(x = year,
y = meanArea,
color = biotope)) +
geom_point() +
geom_smooth() +
scale_color_manual(values = mypalette) +
labs(title = "Distribution of Pararge aegeria per biotope and region",
y = "Area (%)",
color = "BIOTOPE"
) +
facet_wrap(~ region)
usercase1
# 3. How to use it for user case 3?
usercase3 <-
ggplot(area_biotopes_2000,
aes(x = region, y = biotope)) +
geom_tile(aes(fill = meanArea)) +
scale_fill_gradientn(colours = mypalette) +
labs(title = "Average covered area (%) in 2000",
fill = "Area (%)") +
facet_wrap(~ species)
usercase3
# 4. How to invert the color scale of `usercase3`?
usercase3_inv <-
ggplot(area_biotopes_2000,
aes(x = region, y = biotope)) +
geom_tile(aes(fill = meanArea)) +
scale_fill_gradientn(colours = rev(mypalette)) +
labs(title = "Average covered area (%) in 2000",
fill = "Area (%)") +
facet_wrap(~ species)
usercase3_inv
```
Lynn
```
pal <- c("#554018","#7E5728","#AC6D3D","#DD8358")
usercase1 +
scale_color_manual(values=pal)
usercase3 +
scale_fill_continuous(type="viridis")
usercase3 +
scale_fill_continuous(type="viridis", direction=-1)
```
Emma:
```
#' 2. How to use it for user case 1?
usercase1 +
scale_color_manual(values = c("#56A16A","#7A9346","#998235","#AD6E3D"))
# 3. How to use it for user case 3?
usercase3 +
scale_fill_gradientn(colors = c("#56A16A","#7A9346","#998235","#AD6E3D"))
# 4. How to invert the color scale of `usercase3`?
usercase3 +
scale_fill_gradientn(colors = c("#AD6E3D","#998235","#7A9346","#56A16A"))
usercase3 +
scale_fill_gradientn(colors = rev(c("#56A16A","#7A9346","#998235","#AD6E3D")))
```
The package "ggsci" has a lot of predefined palettes, including colourblind-friendly ones
## Challenge 2
Cécile
```
#' 1. Display `usercase1` and `usercase2` next to each other
usercase1 + usercase2
#' 2. Display `usercase2` below `usercase1`
usercase1 / usercase2
#' 3. Display `usercase1` and `usercase2`in first row and `usercase3` and
#' `usercase4` in the second row creating a 2x2 plot grid
# this is the defaut behaviour for 4 plots, so this works:
usercase1 + usercase2 +usercase3 + usercase4
#' 4. `usercase1` and `usercase2` share same legend. How to avoid duplicating it in 1 and 2?
(usercase1 + usercase2 + plot_layout(guides = 'collect')) / (usercase3 + usercase4 + plot_layout(guides = 'keep'))
#' 5. What if you try to avoid duplicating the legend of `usercase3` and
#' `usercase4`?
# I will just take the maximum value and
# use it as max limit in both plots:
maxlim <- max(
area_biotopes_2000 %>% summarize(maxarea = max(meanArea)),
area_biotopes_2018 %>% summarize(maxarea = max(meanArea)))
usercase3 <- usercase3 +
scale_fill_continuous(limits = c(0, maxlim))
usercase4 <- usercase4 +
scale_fill_continuous(limits = c(0, maxlim))
(usercase1 + usercase2 ) / (usercase3 + usercase4) + plot_layout(guides = 'collect')
```
Matthieu
#' 1. Display `usercase1` and `usercase1` next to each other
usercase1 +
usercase2
#par(mfrow=c(1,2)) doesn't work?!
Lynn
```
#' 1. Display `usercase1` and `usercase2` next to each other
usercase1 + usercase2
#' 2. Display `usercase2` below `usercase1`
usercase1 / usercase2
#' 3. Display `usercase1` and `usercase2`in first row and `usercase3` and
#' `usercase4` in the second row creating a 2x2 plot grid
(usercase1 + usercase2) / (usercase3 + usercase4)
#' 4. `usercase1` and `usercase2` share same legend. How to avoid duplicating
#' it in 1 and 2?
(usercase1 + usercase2) +
plot_layout(guides="collect")
#' 5. What if you try to avoid duplicating the legend of `usercase3` and
#' `usercase4`? Can you understand why? How to solve it? Hint: _scale_ your
#' plots, check ggplot cheatsheet!
usercase3 + usercase4 +
plot_layout(guides="collect")
(usercase3 + scale_fill_continuous(limits=c(0,70))) +
(usercase4 + scale_fill_continuous(limits=c(0,70))) +
plot_layout(guides="collect")
```
Emma:
```
#' 1. Display `usercase1` and `usercase2` next to each other
usercase1 + usercase2
#' 2. Display `usercase2` below `usercase1`
usercase1/usercase2
#' 3. Display `usercase1` and `usercase2`in first row and `usercase3` and
#' `usercase4` in the second row creating a 2x2 plot grid
(usercase1|usercase2)/(usercase3|usercase4)
#' 4. `usercase1` and `usercase2` share same legend. How to avoid duplicating
#' it in 1 and 2?
usercase1/usercase2 +
plot_layout(guides = 'collect')
#' 5. What if you try to avoid duplicating the legend of `usercase3` and
#' `usercase4`? Can you understand why? How to solve it? Hint: _scale_ your
#' plots, check ggplot cheatsheet!
usercase3 + scale_fill_continuous(limits = c(0,75)) +
usercase4 + scale_fill_continuous(limits = c(0,75)) +
plot_layout(guides = 'collect')
```
## Challenge 3
Matthieu
#' 2. Remove the title and the legend of `usercase3`
usercase3 <-
ggplot(area_biotopes_2000,
aes(x = region, y = biotope)) +
geom_tile(aes(fill = meanArea)) +
#'' labs(title = "Average covered area (%) in 2000",
#'' fill = "Area (%)") +
theme(legend.position = "none")+
scale_fill_continuous(type="viridis", direction=-1)+
facet_wrap(~ species)
usercase3
## Bonus challenge