# INBO CODING CLUB 29 February 2024 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 | *** Anja Leyman | Emma Cartuyvels |** Dirk Maes | ** Margot Vanhellemont | ** Pieter Huybrechts| ** Rhea Maesele | * Nele Mullens | ** Jolien Van Malderen |** Charlie Plasman | ** Heleen Deroo | * Tina Tuerlings | Adriaan Seynaeve | ** Ward Standaert | ** Leen Govaere | ## Challenge 1 ### Damiano's solution (example) Copy paste this section to show your solutions. ```r # dummy code print("This is how to insert code.") cat("ᓚᘏᗢ") ``` ### Emma's solution ``` rodentia %>% filter(year > 1999) %>% ggplot(aes(x = year)) + stat_bin(binwidth = 5, fill = "blue", color = "red") + labs(x = "Year", y = "Number of observations", title = "Evolution of rodents in Belgium") ``` ### Pieter's solution ```R # install ggthemes for more ggplot themes if(!require("ggthemes", quietly = TRUE)){install.packages("ggthemes")} # You could also parse eventDate, but year is provided :) dplyr::filter(rodentia, year >= 2000) %>% ggplot() + aes(x = year) + geom_histogram(bins = 5L, fill = "powderblue", color = "plum2") + labs(x = "Year", y = "Number of observations", title = "Evolution of rodents in Belgium",) + ggthemes::theme_economist_white() ``` ### Tina's solution ``` rodentia2000<-rodentia[rodentia$year>="2000",] ggplot(data=rodentia2000,aes(x=year))+ geom_bar(fill='blue',col="red")+ labs(x="Year",y="number of observations", title="Evolution of rodents in Belgium")+ scale_x_binned(n.breaks=5) ``` ### Nele's solution 👍 ``` # 1.1 Make a bar chart with the number of observations of rodents in Belgium per # year. Tip: check the difference between geom_bar() and geom_col(), e.g. via # help ?geom_bar. ggplot(data=rodentia, mapping=aes(x=year))+ #y = indivindualCount geom_bar() # 1.2 Change x and y labels to "Year" and "number of observations" respectively. ggplot(data=rodentia, mapping=aes(x=year))+ labs(x = "Year", y = "Number of observations")+ geom_bar() # 1.3 Add the title "Evolution of rodents in Belgium" to the plot. ggplot(data=rodentia, mapping=aes(x=year))+ labs(x = "Year", y = "Number of observations", title = "Evolution of rodents in Belgium")+ geom_bar() # 1.4 Show only data from 2000. #filter out lower years filter(rodentia, year >= 2000) %>% ggplot(mapping=aes(x=year))+ labs(x = "Year", y = "Number of observations", title = "Evolution of rodents in Belgium")+ geom_bar() # 1.5 Fill all bars with color blue and set contour color to red. filter(rodentia, year >= 2000) %>% ggplot(mapping=aes(x=year))+ labs(x = "Year", y = "Number of observations", title = "Evolution of rodents in Belgium")+ geom_bar(color = "red", fill = "blue") # 1.6 Sometimes, it is adviced to bin data to deal better with outliers and # show trends. In this case, group years in bins of 5 years. filter(rodentia, year >= 2000) %>% ggplot(mapping=aes(x=year))+ labs(x = "Year", y = "Number of observations", title = "Evolution of rodents in Belgium")+ stat_bin(binwidth = 5, color = "red", fill = "blue") ``` ### Charlie's solution 👍 ``` # Make a bar chart with the number of observations of rodents in Belgium per year. # Change x and y labels to "Year" and "number of observations" respectively. # Add the title "Evolution of rodents in Belgium" to the plot. rodentia |> filter(year > 1999) |> ggplot(mapping = aes(x = year)) + geom_bar() + labs(title = "Evolution of rodents in Belgium", x = "Year", y = "Number of observations") # Show only data from 2000. # Fill all bars with color blue and set contour color to red. # Bin data to deal better with outliers and show trends. In this case, group years in bins of 5 years. rodentia |> filter(year == 2000) |> ggplot(mapping = aes(x = month)) + stat_bin(binwidth = 5, fill = "blue", color = "red") + labs(title = "Evolution of rodents in Belgium", x = "Year", y = "Number of observations") ``` ### Ward's solution 👍 ``` ggplot(data = rodentia %>% filter(year >= 2000), aes(x = year)) + stat_bin(binwidth = 5, fill = "blue", color = "red") + labs(x = "Year") + labs(y = "number of observations") + ggtitle("Evolution of rodents in Belgium") ``` ## Challenge 2 ### Emma's solution 👍 ``` ## Challenge 2A rodentia %>% filter(year > 1999) %>% ggplot(aes(x = year)) + stat_bin(binwidth = 5, position = "dodge") + labs(x = "Year", y = "Number of observations", title = "Evolution of rodents in Belgium") + facet_wrap(~ genus, scales = "free") ## Challenge 2B rodentia_selection_n %>% ggplot(aes(x = year, y = n, color = species)) + geom_point() + geom_line() rodentia_selection_n %>% ggplot(aes(x = year, y = n, color = species)) + geom_point() + geom_smooth(method = "gam") ``` ### Pieter's Solution ```R ## 2A ----------------------------------------------------------- filter(rodentia, year >= 2015) |> ggplot(mapping = aes(x = year, fill = genus)) + geom_bar(position = position_dodge2(preserve = "single")) # different option with colours count(rodentia, genus, year) |> ggplot() + aes(x = genus, y = n, fill = year) + geom_col(position = "stack") + scale_fill_viridis_c(option = "inferno", direction = 1) + theme_classic() ### Faceting #### x is genus, subplot per year dplyr::filter(rodentia, year >= 2009) |> ggplot() + aes(x = genus, fill = family) + geom_bar() + scale_fill_hue(direction = 1) + facet_wrap(vars(year), scales = "free_x") + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) #### subplot per family, x is year filter(rodentia, year > 1999) |> ggplot(mapping = aes(x = year)) + geom_histogram(bins = 5L, fill = "wheat3") + facet_wrap(vars(family), scales = "free_x") ## 2B ----------------------------------------------------------- rodentia_selection <- rodentia %>% dplyr::filter(species %in% c("Ondatra zibethicus", "Rattus norvegicus")) rodentia_selection_n <- rodentia_selection %>% group_by(species,year) %>% count() ggplot(rodentia_selection_n, mapping = aes(y = n, x = year, colour = species)) + geom_point() + geom_smooth(method = "gam") ``` ### Charlie's solution ``` ## Challenge 2A # Make a bar chart plot similar to the ones in challenge 1.1 at genus level. rodentia |> filter(year > 2000) |> ggplot(mapping = aes(x = year, fill = genus, color = genus)) + geom_bar(position = "fill") + labs(title = "Evolution of rodents in Belgium", x = "Year", y = "Number of observations") # How to split the bar chart in subplots based on family? Set x scale "free". rodentia |> filter(year > 2000) |> ggplot(mapping = aes(x = year, fill = genus, color = genus)) + geom_bar(position = "fill") + labs(title = "Evolution of rodents in Belgium", x = "Genus", y = "Number of observations") + facet_wrap(~family) ## Challenge 2B ggplot(data = rodentia_selection_n, mapping = aes(x = year,y = n, color = species)) + geom_line() + geom_smooth() + labs(title = "Evolution of 2 rodents sp. in Belgium", x = "Year", y = "Number of observations", color = "Rodent species") ``` ### Nele's solution 👍 ``` # 2A.1 Make a bar chart plot similar to the ones in challenge 1.1 at genus level. # How are the bars displayed per genus, by default? ggplot(data=rodentia, mapping=aes(x = year, fill = genus))+ labs(x = "Genera", y = "Number of observations")+ stat_bin(binwidth = 5, color = "black") # 2A.2 Try other ways to "adjust" the bars. Tip: check the position adjustment # section section in ggplot2 reference website. ggplot(data=rodentia, mapping=aes(x = year, fill = genus))+ labs(x = "Genera", y = "Number of observations")+ geom_bar(position = "jitter", color = "black") # 2A.3 How to split the bar chart in subplots based on family? Set x scale "free". ggplot(data=rodentia, mapping=aes(x = year, fill = genus))+ labs(x = "Genera", y = "Number of observations")+ stat_bin(binwidth = 5, color = "black") + facet_grid(cols = vars(family)) # 2B.1 Create a plot showing the number of observations of Ondatra zibeticus and # Rattus norvegicus. Use both points and lines and distinguish the two species # via color. ggplot(rodentia_selection_n, mapping=aes(x = year, y = n, color = species))+ #y = species, labs(x = "Years", y = "Number of observations")+ geom_line() # 2B.2 Typically, points are combined with a line showing the trend, calculated # via a smoother function. ggplot helps us enormously to model and smooth our # data. Try different smoother methods. ggplot(rodentia_selection_n, mapping=aes(x = year, y = n, color = species))+ #y = species, labs(x = "Years", y = "Number of observations")+ geom_point() + geom_line() + geom_smooth() ``` ### Dirk's solution 👍 ```r p <- ggplot(rodentia, aes(x = year, group = genus, fill = genus)) + geom_bar(position = position_dodge2(preserve = "single")) + # to keep all bars in the same width xlab("Year") + ylab("number of observations") + ggtitle("Evolution of rodents in Belgium") + xlim(2000,2021) + theme_bw() + facet_wrap(~ family, scales = "free") p ``` ## Challenge 3 ### Tina's solution ``` ## showing the distribution of the observed species per year library(viridis) library(ggridges) ggplot( rodentia, aes(x=month, y=species,col=family)) + geom_boxplot() + scale_fill_viridis(discrete = TRUE, alpha=0.6) ggplot(rodentia, aes(x = month, y =species, fill = family)) + geom_density_ridges() + theme_ridges() + theme(legend.position = "none") ##pie charts # Create a new column to categorize genera rodentia$genus_category <- ifelse(rodentia$genus %in% c("Rattus", "Ondatra"), rodentia$genus, "other") # Plot the pie chart ggplot(rodentia, aes(x = "", fill = genus_category)) + geom_bar(width = 1, stat = "count") + coord_polar("y", start = 0) + theme_void() ```