# INBO CODING CLUB 28 November 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 | *** Emma Cartuyvels| ** Pieter Huybrechts | ** Dirk Maes | ** Falk Mielke | *** Rhea Maesele | * Sanne Govaert | *** Anja Leyman | Jonas Bertels | Britt Lonneville | ** Cyrielle Delvenne | Adriaan Seynaeve | Arc'hantael Labriere | Bert Van Hecke | ** ## Challenge 1 ### Damiano's solution (example) Copy paste this section to show your solutions. ```r # dummy code print("This is how to insert code.") ``` ### Pieter's solution I used a different palette, I hear it's more colour blind friendly: https://doi.org/10.1371/journal.pone.0199239 ```r library(ggplot2) library(ggrepel) ias_in_pa <- sf::st_read( dsn = "./data/20241128/20241128_ias_union_concern_Natura2000_B.gpkg" ) ias_in_pa |> ggplot() + geom_sf(colour = "black") + aes(fill = n_ias) + # geom_sf_label_(aes(label = SITECODE), size = 2) + geom_label_repel( aes(label = SITECODE, geometry = geom), stat = "sf_coordinates", size = 2, max.time = 9, max.iter = 50000, colour = "snow" ) + # scale_fill_viridis_c() + paletteer::scale_fill_paletteer_c("viridis::cividis") + theme_minimal() + theme(legend.position = "bottom") + labs( title = "Invasive Alien Species of union concern in Natura2000 protected areas", fill = "Number of IAS" ) ``` ![image](https://hackmd.io/_uploads/rypp22H71l.png) ### Falk's attempt ```r # https://ggplot2.tidyverse.org/reference/ggsf.html ias_in_pa %>% ggplot(aes(fill = n_ias)) + geom_sf(color = "black") + # geom_sf_label(aes(label = SITECODE), size = 8, fontface = "bold") + # geom_sf_text(aes(label = SITECODE), size = 4, fontface = "bold") + geom_text_repel(aes(label = SITECODE, geometry = geom), stat = "sf_coordinates", size = 8, fontface = "bold") + scale_fill_viridis_c() + theme(legend.position = "none") ``` ### Dirk's attempt (categorising the number of species), needs some more cleaning! ``` ias_in_pa <- ias_in_pa %>% mutate(n_ias2 = case_when( n_ias <10 ~ "<10", n_ias >= 10 & n_ias < 15 ~ "10 - 15", n_ias >= 15 & n_ias < 20 ~ "15 - 20", n_ias >= 20 & n_ias < 25 ~ "20 - 25", n_ias >= 25 ~ ">25" )) head(ias_in_pa) ias_in_pa$n_ias2 <- factor(ias_in_pa$n_ias2, levels = c("<10", "10 - 15", "15 - 20", "20 - 25", ">25")) p <- ggplot(ias_in_pa, aes(fill = n_ias2), colour = "black") + scale_fill_manual(values = c(viridis::inferno(5))) + geom_sf() + geom_label_repel(aes(label = SITECODE, geometry = geom), colour = "lightgreen", size = 5, fontface = "bold", stat = "sf_coordinates", max.overlaps = 38) + theme_minimal() + theme(legend.position = "bottom") + labs( title = "Invasive Alien Species of union concern in Natura2000 protected areas", fill = "Number of IAS" ) p ``` ### Emma's solution ``` ggplot(ias_in_pa) + geom_sf(aes(fill = n_ias), color = "black") + scale_fill_viridis_c() + geom_label_repel(aes(label = SITECODE, geometry = geom), stat = "sf_coordinates", size = 2, fontface = "bold", max.overlaps = 15) ``` ### Sanne's solution ```r ias_in_pa %>% ggplot() + geom_sf(aes(fill = n_ias), color = "black") + # geom_sf_label(aes(label = SITECODE, size = 2), color = "black") + geom_text_repel( aes(label = SITECODE, geometry = geom), stat = "sf_coordinates", size = 2 ) + scale_fill_viridis_c() ``` ### Rhea's solution ```r #1. How to plot the Natura2000 areas with ggplot? #Fill the areas based on the number of alien species of Union concern, n_ias. #This kind of map is known as choropleth map. #Hint: see documentation of the geom_sf function or the general hint below. ggplot(ias_in_pa) + geom_sf(aes(fill = n_ias)) #2. Add the site code (SITECODE column) as labels. #Show them in black, with fontsize 2 and bold style. ggplot(ias_in_pa) + geom_sf(aes(fill = n_ias)) + geom_sf_label(aes(label = SITECODE), color = "black", size = 2, fontface = "bold") #3. A pure ggplot question: how to set the color of the borders of the Natura2000 areas #to black? ggplot(ias_in_pa) + geom_sf(aes(fill = n_ias), color = "black") + geom_sf_label(aes(label = SITECODE), color = "black", size = 2, fontface = "bold") #within geom_sf, but outside 'aes()', set color = "black" to set the color of the borders as another color than the 'fill' #4. A pure ggplot question: Scale the color using the continuous viridis palette. #Hint: see documentation of the scale_fill_viridis family of functions. ggplot(ias_in_pa) + geom_sf(aes(fill = n_ias)) + geom_sf_label(aes(label = SITECODE), color = "black", size = 2, fontface = "bold") + scale_fill_viridis_c() #5. How to avoid overlapping labels? Hint: see the general hint below. #General hint: Follow the first part of this good tutorial from R CHARTS! ggplot(ias_in_pa) + geom_sf(aes(fill = n_ias)) + scale_fill_viridis_c() + geom_text_repel( aes(label = SITECODE, geometry = geom), stat = "sf_coordinates", size = 2, fontface = "bold") ``` ### Arc'hantael's attempt: ias_in_pa %>% ggplot() + geom_sf(color = "black") + aes(fill = n_ias) + geom_sf_text(aes(label = SITECODE), size = 2, fontface = "bold") + scale_fill_viridis_c() + labs(fill = "Number of IAS") ## Challenge 2 ### Emma's solution ``` ias_in_pa_sample |> ggplot() + annotation_map_tile(type = "cartolight", zoomin = 0, cachedir = "./data/20241128") + geom_sf(aes(fill = n_ias), color = "black") + scale_fill_viridis_c() + geom_label_repel(aes(label = SITECODE, geometry = geom), stat = "sf_coordinates", size = 2, fontface = "bold", max.overlaps = 15) + annotation_north_arrow(location = "bl", which_north = "grid") + labs(x = NULL, y = NULL) ``` ### Pieter's solution ```r ias_in_pa_sample <- ias_in_pa[1:10,] ias_in_pa_sample %>% ggplot() + scale_fill_viridis_c() + ggspatial::annotation_map_tile( type = "cartolight", zoom = 10, cachedir = tempdir() ) + geom_sf(aes(fill = n_ias)) + ggspatial::annotation_north_arrow( location = "tr", which_north = "true", style = ggspatial::north_arrow_fancy_orienteering()) + ggspatial::annotation_scale(location = "br") ``` ![image](https://hackmd.io/_uploads/Hk2V4pHQyg.png) ### Falk's Attempt ```r # more map types: https://paleolimbot.github.io/rosm/ ias_in_pa %>% ggplot(aes(fill = n_ias)) + annotation_map_tile( type = "hotstyle", zoomin = 0, cachedir = "./.tiles/" ) + geom_sf(color = "black") + geom_text_repel(aes(label = SITECODE, geometry = geom), stat = "sf_coordinates", size = 4, fontface = "bold") + scale_fill_viridis_c() + annotation_north_arrow(location = "tr", which_north = "true") + annotation_scale(location = "bl") + theme_bw() + theme(legend.position = "none") ``` ### Sanne's solution ```r ias_in_pa_sample %>% ggplot() + annotation_map_tile( type = "thunderforestlandscape", zoomin = 0, cachedir = "./data/20241128" ) + geom_sf(color = "black") + aes(fill = n_ias) + scale_fill_viridis_c() + geom_label_repel( aes(label = SITECODE, geometry = geom), stat = "sf_coordinates", size = 2 ) + annotation_north_arrow(location = "tl", which_north = "grid") ``` ## Challenge 3 ### Falk's attempt ```r # https://r-spatial.github.io/mapview/articles/mapview_02-advanced.html # c("SITECODE", "SITENAME", "n_ias") m1 <- mapview( ias_in_pa, zcol = "n_ias", popup = popupTable(ias_in_pa), # does not work layer.name = "Alien species of Union concern", color = "gray90", alpha.regions = 0.8, alpha.lines = 0.1, map.types = c("OpenStreetMap", "CartoDB.DarkMatter"), legend = TRUE ) # m1 m2 <- vl_mun %>% mapview( layer.name = "municipalities", color = "black", alpha.regions = 0.0, alpha.lines = 1.0, map.types = c("OpenStreetMap", "CartoDB.DarkMatter"), legend = TRUE ) m2 + m1 # ~~adding mouse coords did not work for me :`(~~ # https://r-spatial.github.io/mapview/articles/mapview_06-add.html # actually it did work by `library("leafem")` ``` ### Pieter's solution ```r mv_ias <- ias_in_pa %>% mapview::mapview( zcol = "n_ias", color = "grey20", map.types = "OpenStreetMap", alpha.regions = 0.8, alpha = 0.1, layer.name = "Alien species of Union concern", popup = leafpop::popupTable( ias_in_pa, zcol = c("SITECODE", "SITENAME", "n_ias") ) ) mapview::mapview(flanders, # map = mv_ias, layer.name = "municipalities", alpha.regions = 0, legend = FALSE, lwd = 1, map.types = "OpenStreetMap") + mv_ias ``` ### Emma's solution ``` mapview(flanders, layer.name = "municipality", map.types = "OpenStreetMap", color = "black", legend = FALSE, alpha.regions = 0, lwd = 1) + mapview(ias_in_pa, zcol = "n_ias", legend = TRUE, color = "grey30", map.types = "OpenStreetMap", alpha.regions = 0.8, alpha = 0.1, layer.name = "Alien species of Union concern", popup = popupTable(ias_in_pa, zcol = c("SITECODE", "SITENAME", "n_ias"))) ``` ### Robin's solution ```r map1 <- mapview(ias_in_pa_sample, zcol = "n_ias", color.palette = viridis, legend = TRUE, alpha = 0.7, color.palette = viridis::viridis, lwd = 1.5, contour = TRUE, contour.color = "gray30", layer.name = "Alien species of Union concern", popup = popupTable(ias_in_pa_sample, zcol = c("SITECODE", "SITENAME", "n_ias")), label = TRUE) map2 <- mapview(flanders, legend = TRUE, alpha = 0.7, alpha.regions = 0, color.palette = viridis::viridis, lwd = 1, contour = TRUE, contour.color = "black", layer.name = "Municipalities", label = TRUE) combined_map <- map2 + map1 combined_map ``` ### Sanne's solution ```r mapview( flanders, layer.name = "Municipalities", color = "black", matypes = "OpenStreetMap", legend = FALSE, alpha = 1, alpha.regions = 0, label = FALSE ) + mapview( ias_in_pa, zcol = "n_ias", legend = "TRUE", alpha.regions = 0.8, layer.name = "Alien species of Union concern", maptypes = "OpenStreetMap", color = "gray30", alpha = 0.1, popup = popupTable( ias_in_pa, zcol = c("SITECODE", "SITENAME", "n_ias") ) ) ```