# INBO CODING CLUB 31 August 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 | *** Rhea Maesele | Dirk Maes | Nele Mullens | Adriaan Seynaeve | ## Challenge 1 ### Pieter ```r # 1 Vector data (continuous variable) natura2000 <- sf::st_read("./data/20230831/20230831_protected_areas.gpkg") pajottenland_municipalities <- st_read("./data/20230831/20230831_Pajottenland_municipalities.gpkg") natura2000 %>% ggplot(aes(fill = Shape_area)) + geom_sf() + scale_fill_viridis_c() pajottenland_municipalities %>% ggplot(aes(fill = Shape_Area)) + geom_sf() + scale_fill_viridis_b()` # 2. Raster data (continous variable) nitrogen <- terra::rast("./data/20230831/20230831_nitrogen.tif") nitrogen ggplot() + tidyterra::geom_spatraster(data = nitrogen, show.legend = TRUE) terra::plot(nitrogen, range = c(0,47), col = hcl.colors(n = 100, palette = "Blue-Red 3"), legend = TRUE) # 3. Raster data (categorical values) terra::plot(lu_nara_2016, col = legend_land_use$color, type = "classes", levels = legend_land_use$land_use, box = FALSE, main = "Land is used" ) ``` ## Challenge 2 ### Pieter ```r ## Challenge 2 - static maps natura2000 <- sf::st_read("./data/20230831/20230831_protected_areas.gpkg") natura2000_osm <- get_tiles(natura2000, crop = TRUE, zoom = 8) plot_tiles(natura2000_osm) plot(natura2000$geom, add = TRUE) mtext(text = get_credit("OpenStreetMap"), side = 1, line = -1, adj = 1, cex = .9, font = 3) svg("20230831_great_old_plot.svg") # save the plot we are about to create natura2000_esri <- get_tiles(natura2000, provider = "Esri.NatGeoWorldMap", crop = TRUE, zoom = 9) plot_tiles(natura2000_esri) plot(natura2000$geom, add = TRUE) mtext(text = get_credit("Esri.NatGeoWorldMap"), side = 1, line = -1, adj = 1, cex = .9, font = 3) dev.off() # close graphics device, (which we opened by `svg()`) ## see also png() bmp() pdf(), ggplot2::ggsave() is more fun. ``` ## INTERMEZZO ## Challenge 3 ### Pieter ```r # read data.frame with number of moths species for each UTM1km cell (`TAG` # identifier) n_moths <- readr::read_csv("./data/20230831/20230831_n_moths.txt") n_moths # read the geospatial file containing the UTM1km cells of Pajottenland utm1_pajottenland <- sf::st_read( "./data/20230831/20230831_utm1km_Pajottenland.gpkg" ) # Join data n_moths <- utm1_pajottenland %>% dplyr::left_join(n_moths, by = "TAG") n_moths # 1 + 2 mapview( n_moths, color = "red", alpha.regions = 0.8, col.regions = RColorBrewer::brewer.pal(9, name = "Spectral"), alpha = 0.2, map.types = "OpenStreetMap", legend = TRUE, zcol = "n", layer.name = "number of moth species" ) # 3 mapview( pajottenland_municipalities, layer.name = "municipality", map.types = "OpenStreetMap", alpha.regions = 0, lwd = 1, legend = FALSE, ) # 4 mapview( list(n_moths, pajottenland_municipalities), layer.name = list("number of moth species", "municipality"), legend = list(TRUE, FALSE), alpha.regions = list(0.2, 0), alpha = list(0.8, NULL), zcol = list("n", NULL), color = list("red","black"), map.types = "OpenStreetMap", lwd = list(2, 1) ) # 5 img <- "https://raw.githubusercontent.com/inbo/coding-club/master/docs/assets/images/coding_club_logo.svg" mapview( list(n_moths, pajottenland_municipalities), layer.name = list("number of moth species", "municipality"), legend = list(TRUE, FALSE), alpha.regions = list(0.2, 0), alpha = list(0.8, NULL), zcol = list("n", NULL), color = list("red","black"), map.types = "OpenStreetMap", lwd = list(2, 1) ) %>% leafem::addLogo(img, url = "https://inbo.github.io/coding-club/") ``` ### Dirk, example using your own colour palette ```r colpal <- colorRampPalette(c("white", "grey", "red", "darkgreen")) map_n_moths <- mapview(n_moths, zcol = "n", legend = TRUE, color = "pink", col.regions = colpal, map.types = "OpenStreetMap", alpha = 50) map_n_moths ``` ### How to save leaflet maps as html? ```r saveWidget(map, filename = "filename.html") ``` ## Bonus challenge 1 ## Bonus challenge 2