owned this note
owned this note
Published
Linked with GitHub
# INBO CODING CLUB
17 December 2020
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
No yellow sticky notes online. Put your name + " | " and add a "*" each time you solve a challenge (see below).
## Participants
Name | Challenges
--- | ---
Hans Van Calster |
Emma Cartuyvels |**
Dirk Maes | **
Frieda Van Roy |**
An Leyssen |**
Arno Thomaes|**
Patrik Oosterlynck|**
Tom De Dobbelaer |**
Karen Cox |**
Anja Leyman | **
Leen Govaere |**
Stien Heremans |
Cécile Herr |**
Lynn Pallemaerts | *
Luc De Bruyn | **
## Challenge 1
Hans:
```
code
```
Emma:
```
#' 2. How to use it for user case 1?
usercase1 + scale_fill_manual(values = c("#0A831D","#59A648",
"#9DC77C","#E9E3BA"),
aesthetics = "color")
# 2. How to use it for user case 2?
usercase2 + scale_fill_gradientn(colors = c("#0A831D","#59A648",
"#9DC77C","#E9E3BA"),
aesthetics = "fill")
```
Dirk:
```
# usercase 1
library(ggsci)
usercase1 <-
ggplot(area_biotopes_pararge,
aes(x = year,
y = meanArea,
color = as.factor(biotope))) +
geom_point() +
geom_smooth() +
theme(axis.text.x = element_text(angle = 45,
hjust = 1),
axis.text = element_text(size = 20),
axis.title = element_text(size = 25, face = "bold"),
legend.text = element_text(size = 20),
legend.title = element_text(size = 20),
panel.background = element_rect(fill = "white", colour = "grey",
size = 1,
linetype = "solid"),
panel.grid.major = element_line(size = 0.5,
linetype = "solid",
colour = "grey"),
strip.text = element_text(size = 15)) +
labs(title = "Distribution of Pararge aegeria per biotope and region",
y = "Area (%)",
x = "Year") +
scale_fill_manual("Biotope") +
scale_colour_jco() +
facet_wrap(~ region)
usercase1
```
## Challenge 2
Emma:
```
#' `ggplot2` includes some functions to use viridis palette directly.
#' 1. How to apply viridis discrete color scale to user case 1?
usercase1 + scale_color_viridis_d()
#' 2. How to apply viridis continuous color scale to user case 2?
usercase2 + scale_fill_viridis_c()
#' 3. Invert the color direction. Note: typically inverted color directions are
#' less intuitive
usercase1 + scale_color_viridis_d(direction = -1)
usercase2 + scale_fill_viridis_c(direction = -1)
# 4. Use colormap "cividis" instead of default ("viridis")
usercase1 + scale_color_viridis_d(option = "E")
usercase2 + scale_fill_viridis_c(option = "E")
```
Cécile
```
#' 1. How to apply viridis discrete color scale to user case 1?
usercase1_col <- usercase1 +
scale_colour_manual(values = viridis(4)))
usercase1_col
#' 2. How to apply viridis continuous color scale to user case 2?
usercase2_col <- usercase2 +
scale_fill_gradientn(colors = viridis(4))
usercase2_col
#' 3. Invert the color direction.
usercase2_col <- usercase2 +
scale_fill_gradientn(colors = rev(viridis(4)))
usercase2_col
# 4. Use colormap "cividis" instead of default ("viridis")
usercase2_col <- usercase2 +
scale_fill_gradientn(colors = cividis(4))
usercase2_col
```
Anja
```
pal <- viridisLite::viridis(4)
pal
usercase1 + scale_color_manual(values = pal)
sercase2 + scale_fill_gradientn(colours = viridis(4))
#' 3. Invert the color direction. Note: typically inverted color directions are less intuitive
usercase1 + scale_color_manual(values = rev(pal))
usercase2 + scale_fill_gradientn(colours = rev(viridis(4)))
# 4. Use colormap "cividis" instead of default ("viridis")
usercase1 + scale_color_manual(values = cividis(4))
usercase2 + scale_fill_gradientn(colours = cividis(4))
```
Arno
```
usercase1 + scale_color_viridis(discrete = TRUE)
usercase2 + scale_fill_viridis()
usercase1 + scale_color_viridis(discrete = TRUE, direction = -1)
usercase2 + scale_fill_viridis(direction = -1)
usercase1 + scale_color_viridis(discrete = TRUE, option = "cividis")
```
Frieda
```
#' 1. How to apply viridis discrete color scale to user case 1?
usercase1 + scale_color_viridis(discrete=TRUE)
#' 2. How to apply viridis continuous color scale to user case 2?
usercase2 + scale_fill_viridis()
#' 3. Invert the color direction. Note: typically inverted color directions are less intuitive
usercase1 + scale_color_viridis_d(direction = -1)
usercase2 + scale_fill_viridis_c(direction = -1)
# 4. Use colormap "cividis" instead of default ("viridis")
usercase1 + scale_color_viridis_d(option = "E")
usercase2 + scale_fill_viridis_c(option = "E")
```
Lynn
```
#' 1. How to apply viridis discrete color scale to user case 1?
usercase1 +
scale_color_viridis_d()
#' 2. How to apply viridis continuous color scale to user case 2?
usercase2 +
scale_fill_viridis_c()
#' 3. Invert the color direction. Note: typically inverted color directions are less intuitive
usercase1 +
scale_color_viridis_d(direction=-1)
usercase2 +
scale_fill_viridis_c(direction=-1)
# 4. Use colormap "cividis" instead of default ("viridis")
usercase1 +
scale_color_viridis_d(option="E")
usercase2 +
scale_fill_viridis_c(option="E")
```
Tom
```
#' 1. How to apply viridis discrete color scale to user case 1?
usercase1+ scale_color_viridis_d()
#' 2. How to apply viridis continuous color scale to user case 2?
usercase2+ scale_fill_viridis_c()
#' 3. Invert the color direction. Note: typically inverted color directions are less intuitive
usercase1+ scale_color_viridis_c(direction = -1)
usercase2+ scale_fill_viridis_c(direction = -1)
# 4. Use colormap "cividis" instead of default ("viridis")
usercase2+ scale_fill_gradientn(colours = "cividis")
```
## INTERMEZZO
## Challenge 3
Cécile:
```
usercase2 <-
ggplot(area_biotopes_2010,
aes(x = region, y = biotope, group = 1,
text = paste("My own text is ",
"<br>Mean area: ", meanArea,
"<br>Region: ", region,
"<br>Biotope: ", biotope))) +
geom_tile(aes(fill = meanArea)) +
labs(title = "Average covered area (%) in 2010",
fill = "Area (%)") +
facet_wrap(~ species)
ggplotly(usercase2, tooltip = "text")
```
Anja
```
ggplotly(usercase1)
ggplotly(usercase2)
#' 2. How to personalize the text displayed while hovering on the tiles of usercase2? See example below
# region, biotope, value ipv MeanArea, region en Biotope
usercase2 <-
ggplot(area_biotopes_2010,
aes(x = region, y = biotope,
text = paste("region: ", region,
"<br>biotope: ", biotope,
"<br>value: ", round(meanArea, 1)
))) +
geom_tile(aes(fill = meanArea)) +
labs(title = "Average covered area (%) in 2010",
fill = "Area (%)") +
facet_wrap(~ species)
ggplotly(usercase2, tooltip ="text")
usercase1 <-
ggplot(area_biotopes_pararge,
aes(x = year,
y = meanArea,
color = as.factor(biotope))) +
geom_point(aes(text = paste("year: ", year,
"<br>biotope ", biotope,
"<br>value: ",round(meanArea, 1)
))) +
geom_smooth() +
labs(title = "Distribution of Pararge aegeria per biotope and region",
y = "Area (%)",
color = "BIOTOPE"
) +
facet_wrap(~ region)
ggplotly(usercase1, tooltip = "text")
```
Emma:
```
#' 1. Make interactive plots for usercase1 and usercase2 using plotly.
ggplotly(usercase1)
ggplotly(usercase2)
#' 2. How to personalize the text displayed while hovering on the tiles of usercase2?
#' See example below
usercase2 <-
ggplot(area_biotopes_2010,
aes(x = region, y = biotope,
text = paste("region:", region,
"<br>biotope:", biotope,
"<br>value:", round(meanArea, digits = 1)))) +
geom_tile(aes(fill = meanArea)) +
labs(title = "Average covered area (%) in 2010",
fill = "Area (%)") +
facet_wrap(~ species)
ggplotly(usercase2,
tooltip = "text")
# apply the same to usercase1:
# hint make sure the "unofficial" aes(text = ...) is only passed on to geom_text
usercase1 <-
ggplot(area_biotopes_pararge,
aes(x = year,
y = meanArea,
color = as.factor(biotope)
)) +
geom_point(aes(text = paste("region:", region,
"<br>biotope:", biotope,
"<br>value:", round(meanArea, digits = 1)))) +
geom_smooth() +
labs(title = "Distribution of Pararge aegeria per biotope and region",
y = "Area (%)",
color = "BIOTOPE"
) +
facet_wrap(~ region)
ggplotly(usercase1,
tooltip = "text")
```
Lynn
```
#' 1. Make interactive plots for usercase1 and usercase2 using plotly.
ggplotly(usercase1)
ggplotly(usercase2)
#' 2. How to personalize the text displayed while hovering on the tiles of usercase2? See example below
usercase2 <-
ggplot(area_biotopes_2010,
aes(x = region,
y = biotope,
group=1,
text=paste("region: ", region,
"<br>biotope: ", biotope,
"<br>value: ", round(meanArea,2)))) +
geom_tile(aes(fill = meanArea)) +
labs(title = "Average covered area (%) in 2010",
fill = "Area (%)") +
facet_wrap(~ species)
ggplotly(usercase2, tooltip = "text")
# apply the same to usercase1:
# hint make sure the "unofficial" aes(text = ...) is only passed on to geom_point
usercase1 <-
ggplot(area_biotopes_pararge,
aes(x = year,
y = meanArea,
color = as.factor(biotope))) +
geom_point(aes(text=paste("region: ", region,
"<br>biotope: ", biotope,
"<br>value: ", round(meanArea,2)))) +
geom_smooth() +
labs(title = "Distribution of Pararge aegeria per biotope and region",
y = "Area (%)",
color = "BIOTOPE"
) +
facet_wrap(~ region)
ggplotly(usercase1, tooltip = "text")
```
Hans
```
#' 1. Make a basic animation plot and set year in the title
# step 1 make standard faceted heatmap
heatmap_area_biotopes <-
ggplot(area_biotopes,
aes(x = region, y = biotope)) +
geom_tile(aes(fill = meanArea)) +
labs(fill = "Area (%)") +
facet_wrap(~ species)
# step 2 add animation by year
heatmap_area_biotopes_animation <-
heatmap_area_biotopes +
transition_time(time = year)
# step 3 set year in the title
heatmap_area_biotopes_animation <-
heatmap_area_biotopes_animation +
ggtitle("Average covered area (%) in {frame_time}",
subtitle = "Showing frame {frame} of {nframes}")
#' 2. Improve entering and exiting among states
heatmap_area_biotopes_animation <-
heatmap_area_biotopes_animation +
# extra options for improving visualization
enter_fade() +
exit_shrink()
animate(heatmap_area_biotopes_animation,
nframes = length(unique(area_biotopes$year)),
fps = 1)
#' 3. Save animation as gif (default) and set width to 800 pixels and height to 550 pixels
anim_save("20201217_animation_heatmap_biotopes.gif", animation = heatmap_area_biotopes_animation, path = "./docs/assets/images/20201217", width = 800, height = 500)
#' 4. Do you hate gifs? Well, save the animation as a mp4 video
anim_save(filename = "20201217_animation_heatmap_biotopes.mp4",
animation = heatmap_area_biotopes_animation,
path = "./docs/assets/images/20201217",
width = 800,
height = 500,
renderer = ffmpeg_renderer(format = "mp4"))
```
## Bonus challenge