# INBO CODING CLUB 30 March 2023 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 --- | --- Damiano Oldoni | Pieter Huybrechts |** Dirk Maes | *** Adriaan Seynaeve | Rhea Maesele | *** Nele Mullens | * Ward Langeraert | *** Joppe Massant | Margot Vanhellemont | *** Marijke Thoonen | * ## Challenge 1 ### Pieter's solution ``` r ### 1.1 ggplot(data = ias_be) + aes(x = first_observed) + geom_bar(fill = "blue", color = "red", ) + labs(x = "year of introduction", y = "number of taxa") ### 1.3 ias_be %>% filter(first_observed > 1979) %>% ggplot() + aes(x = first_observed) + geom_bar( fill = "blue", color = "red", ) + labs( x = "year of introduction", y = "number of taxa" ) ### 1.5 ias_be %>% filter(first_observed > 1979) %>% ggplot() + aes(x = first_observed) + geom_histogram( fill = "blue", color = "red", binwidth = 5 ) + labs( x = "year of introduction", y = "number of taxa" ) ``` ### Solution Dirk ``` r p <- ggplot(ias_be, aes(x = first_observed)) + geom_bar(colour = "red", fill = "blue") + xlab("year of introduction") + ylab("number of taxa") + xlim(1980, 2020) + scale_x_binned(n.breaks = 15) + theme(axis.text.x = element_text(angle = 45, hjust = 0.5, vjust = 0.5), axis.text.y = element_text(angle = 0, hjust = 0), axis.text = element_text(size = 10), axis.title = element_text(size = 15, face = "bold"), legend.text = element_text(size = 10), legend.position = "none", legend.title = element_text(size = 15), panel.background = element_rect(fill = "white", colour = "grey", size = 1, linetype = "solid"), panel.grid.major = element_line(size = 0.5, linetype = "solid", colour = "grey")) p ``` ### Solution Ward ``` r # geom_bar() makes the height of the bar proportional to the number of cases # in each group # geom_col() makes the height of the bar represent values in the data ias_be %>% filter(first_observed >= 1980) %>% ggplot() + geom_bar(aes(x = cut(first_observed, length(unique(first_observed)) / 5)), fill = "cornflowerblue", colour = "firebrick") + labs(x = "year of introduction", y = "number of taxa") ``` ### Nele's solution ``` # 1.1 ggplot(data = ias_be, mapping = aes(x = first_observed)) + geom_bar() # 1.2 ggplot(data = ias_be, mapping = aes(x = first_observed)) + geom_bar() + labs(x="year of introduction", y= "number of taxa") # 1.3 filtered_ias_be <- dplyr::filter(ias_be, first_observed > 1979) #ggplot(data = filtered_ias_be, # mapping = aes(x = first_observed)) + geom_bar() ggplot(data = filtered_ias_be, mapping = aes(x = first_observed)) + geom_histogram(binwidth = 5) # 1.4 ggplot(data = filtered_ias_be, mapping = aes(x = first_observed)) + geom_histogram(fill = "blue", colour = "red") # 1.5 ggplot(data = filtered_ias_be, mapping = aes(x = first_observed)) + geom_histogram(binwidth = 5, fill = "blue", colour = "red") ``` ### Solution Joppe ``` value_counts <- count(ias_be, first_observed) value_counts <- value_counts %>% filter(first_observed >= 1980) ggplot(data = value_counts, mapping = aes( x = first_observed, y = n)) + scale_x_binned(n.breaks=8) + geom_col(fill = "blue", color = "red") + labs(x="Year of introduction", y="Number of taxa") ``` ## Challenge 2 ### Solution Ward ``` r # 1 ias_regions %>% ggplot() + geom_bar(aes(x = first_observed, fill = locationId), colour = "black") + labs(x = "year of introduction", y = "number of taxa", fill = "location") # 2 ias_regions %>% ggplot() + geom_bar(aes(x = first_observed, fill = locationId), position = "dodge") + labs(x = "year of introduction", y = "number of taxa", fill = "location") # 3 ias_regions %>% ggplot() + geom_bar(aes(x = first_observed, fill = locationId), position = "dodge") + facet_wrap(~kingdom, scales = "free_y") + labs(x = "year of introduction", y = "number of taxa", fill = "location") # 4 ias_regions %>% mutate( kingdom = factor(kingdom, levels = c("Animalia", "Plantae", "Fungi", "Chromista"), ordered = TRUE) ) %>% ggplot() + geom_bar(aes(x = first_observed, fill = locationId), position = "dodge") + facet_wrap(~kingdom, scales = "free_y") + labs(x = "year of introduction", y = "number of taxa", fill = "location") # alternative solution ias_regions %>% group_by(kingdom) %>% mutate(n = n()) %>% ungroup() %>% mutate(kingdom = reorder(kingdom, n, decreasing = TRUE)) %>% ggplot() + geom_bar(aes(x = first_observed, fill = locationId), position = "dodge") + facet_wrap(~kingdom, scales = "free_y") + labs(x = "year of introduction", y = "number of taxa", fill = "location") # 5 ias_regions %>% group_by(kingdom) %>% mutate(n = n()) %>% ungroup() %>% mutate(kingdom = reorder(kingdom, n, decreasing = TRUE)) %>% ggplot() + geom_bar(aes(x = first_observed, fill = locationId), position = "dodge") + facet_grid(kingdom~locationId, scales = "free_y") + labs(x = "year of introduction", y = "number of taxa", fill = "location") ``` ### Solution Dirk ``` r ias_regions <- ias_regions %>% mutate(region = case_when(locationId == "ISO_3166:BE-VLG" ~ "Flanders", locationId == "ISO_3166:BE-BRU" ~ "Brussels", locationId == "ISO_3166:BE-WAL" ~ "Wallonia")) head(ias_regions) ias_regions$kingdom <- factor(ias_regions$kingdom, levels = c("Animalia", "Plantae", "Fungi", "Chromista")) p <- ggplot(ias_regions, aes(x = first_observed)) + geom_bar(aes(fill = region), position = "dodge") + xlab("year of introduction") + ylab("number of taxa") + facet_wrap(~kingdom, scales = "free", ncol = 2) p ## part2 p <- ggplot(ias_regions, aes(x = first_observed)) + geom_bar(aes(fill = region), position = "dodge") + xlab("year of introduction") + ylab("number of taxa") + facet_grid(kingdom~region, scales = "free_y") p ``` ### Solution Marijke ```r ia`_regions$kingdom <- as_factor(ias_regions$kingdom) ias_regions %>% ggplot() + geom_bar(aes(x = first_observed, fill = locationId)) + xlab("year of introduction") + ylab("number of taxa") + facet_grid(rows = vars(kingdom), cols = vars(locationId), scales = "free_y") ``` ### Pieter's solution ```r ggplot(ias_regions) + aes(x = first_observed, fill = locationId) + geom_histogram(bins = 30L) + scale_fill_hue(direction = -1) + theme_dark() + facet_wrap(vars(kingdom)) ggplot(ias_regions) + aes(x = first_observed, fill = locationId) + geom_histogram(bins = 5L) + scale_fill_viridis_d(option = "inferno", direction = -1) + theme_dark() + facet_wrap(scales = "free_y", ~factor(kingdom, levels = c("Animalia", "Plantae", "Fungi", "Chromista"))) + labs( x = "year of introduction", y = "number of taxa" ) ``` ### Nele's solution ``` # 2.1 - 2.2 ggplot(data = ias_regions, mapping = aes(x = first_observed)) + geom_histogram() + facet_wrap(vars(locationId)) # 2.3 ggplot( data = ias_regions, mapping = aes(x = first_observed, fill = locationId) ) + geom_bar() + facet_wrap(vars(kingdom), scales = "free_y") # 2.5 ggplot(data = ias_regions, mapping = aes(x = first_observed, fill = locationId)) + geom_bar() + facet_wrap(vars(kingdom, locationId)) ``` ### Joppe's solution ``` ### 2.2 ggplot(data = ias_regions, mapping = aes(x = first_observed, fill = locationId)) + geom_bar(position="dodge") ### 2.3 ggplot(data = ias_regions, mapping = aes(x = first_observed, fill = locationId)) + geom_bar() + facet_wrap(vars(locationId)) ### 2.3 ggplot(data = ias_regions, mapping = aes(x = first_observed, fill = locationId)) + geom_bar(position = 'dodge') + facet_wrap(vars(kingdom)) ### 2.4 ggplot(data = ias_regions, mapping = aes(x = first_observed, fill = locationId)) + geom_bar(position = 'dodge') + facet_grid(rows = vars(kingdom), cols = vars(locationId)) ``` ### Margot's solution ``` gg`lot(data = ias_regions, mapping = aes(x = first_observed, fill = factor(locationId,levels = c("ISO_3166:BE-VLG", "ISO_3166:BE-WAL", "ISO_3166:BE-BRU"), labels = c("FLANDERS", "WALLONIA", "BRUSSELS")))) + geom_bar() + labs(x = "year of introduction", y = "number of taxa") + facet_grid(factor(kingdom, levels = c('Animalia', 'Plantae', 'Fungi', 'Chromista')) ~ factor(locationId, levels = c("ISO_3166:BE-VLG", "ISO_3166:BE-WAL", "ISO_3166:BE-BRU"), labels = c("FLANDERS", "WALLONIA", "BRUSSELS")), scales = 'free_y') + scale_fill_discrete("location", labels = c("FLANDERS", "WALLONIA", "BRUSSELS")) ``` ## Challenge 3 ### Solution Ward ```r # part 1 library(INBOtheme) ias_path %>% ggplot() + geom_bar(aes(x = pathway, fill = kingdom), position = "dodge") + labs(y = "number of taxa") + coord_flip() # part 2 bin <- 10 ias_path %>% mutate(bins_first_observed_label = floor(.data$first_observed / bin) * bin ) %>% mutate(bins_first_observed_label = paste( as.character(.data$bins_first_observed_label), "-", as.character(.data$bins_first_observed_label + bin - 1) )) %>% group_by(pathway, bins_first_observed_label) %>% mutate(n = n()) %>% ggplot(aes(x = bins_first_observed_label, y = n, colour = pathway)) + geom_point(shape = 1, size = 3) + geom_line(aes(group = pathway)) + labs(x = "time period", y = "number of taxa") ``` ### Pieter's solution ```r ggplot(ias_path) + aes(x = pathway, fill = kingdom) + geom_bar(position = "dodge") + scale_fill_hue(direction = 1) + labs(y = "Number of introduced taxa") + coord_flip() + theme( legend.position = "top", axis.title.y = element_text(size = 19L, face = "bold"), axis.title.x = element_text(size = 19L, face = "bold") ) ``` ## Bonus challenge ### Solution Ward ```r ggplot(ToothGrowth, aes(x = factor(dose), y = len, color = supp)) + geom_boxplot() + geom_point(aes(group = supp), position = position_jitterdodge(jitter.width = 0.5)) + labs(x = "dose", y = "length") ```