owned this note
owned this note
Published
Linked with GitHub
# INBO CODING CLUB
26 August 2025
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 star ("*") each time you solve a challenge (see below).
## Participants
Name | Challenges
--- | ---
Damiano Oldoni | ***
Emma Cartuyvels |
Pieter Huybrechts | **
Rhea Maesele |
Florian Van Hecke |
Anne-Lie Van Praet |
Hanna Jaspaert |
Tosca Vanroy |
Stien Mertens |**
Falk Mielke |
Toon Westra |*
## General
### Mapview and the Pipe
```{r}
library("sf")
library("mapview")
mapview(franconia) # does work -> reference
franconia %>%
mapview() # does not work
franconia |>
mapview() # DOES work
franconia %>%
mapView() # does not work
franconia %>%
mapView(x = .) # does not work
franconia %>%
mapview(x = .) # does not work
view_map <- function (.data, ...) {
mapview(x = .data, ...)
}
franconia %>%
view_map # does not work
franconia |>
view_map() # does not work
```
### Cleanup Suggestion *(Falk)*
FYI you can clean up all the character columns at once:
```{r}
n_giant_hogweed %>%
mutate(
across(
where(is.character),
function(col) stringr::str_extract(col, "(?<=\\[').*(?='\\])")
)
)
# modified from https://forum.posit.co/t/extract-text-between-brakets/43448/6
```
## 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
```r
## 1.1 ####
mapview(n_giant_hogweed,
legend = TRUE,
zcol = "n",
color = "white"
)
## 1.2 ####
mapview(n_giant_hogweed,
legend = TRUE,
zcol = "n",
color = "white",
layer.name = "Number of giant hogweed occurrences",
alpha = 0.5,
alpha.regions = 0.8,
col.regions = heat.colors,
popup = popupTable(n_giant_hogweed,
zcol = c("n",
"mun_name_nl",
"mun_name_fr")
),
)
## 1.3 ####
hogweed_map <- mapview(n_giant_hogweed,
legend = TRUE,
zcol = "n",
color = "white",
layer.name = "Number of giant hogweed occurrences",
alpha = 0.5,
alpha.regions = 0.8,
col.regions = heat.colors,
popup = popupTable(n_giant_hogweed,
zcol = c("n",
"mun_name_nl",
"mun_name_fr")
),
map.types = "OpenStreetMap.Mapnik"
)
hogweed_map
## 1.4 ####
giant_hogweed <- readr::read_tsv(
"data/20250826/20250826_giant_hogweed_fl_bxl.tsv",
na = ""
) %>%
sf::st_as_sf(
coords = c("decimalLongitude", "decimalLatitude"),
crs = 4326
)
hogweed_occ_map <-
hogweed_map + mapview(giant_hogweed,
col.regions = "gray80",
cex = 5,
layer.name = "Giant hogweed occurrences"
)
hogweed_occ_map
## 1.5 ####
hogweed_occ_map %>% leafem::addMouseCoordinates()
## 1.6 ####
hogweed_occ_map %>%
leafem::addMouseCoordinates() %>%
mapshot(url = file.path("data", "20250826", "hogweed.html"))
```
### Emma's solution
```
## 1.1 ####
m <- mapview(n_giant_hogweed,
zcol = "n",
legend = TRUE,
color = "white")
m
## 1.2 ####
m <- mapview(n_giant_hogweed,
zcol = "n",
layer.name = "Number of giant hogweed occurrences",
legend = TRUE,
color = "white",
alpha.regions = 0.8,
alpha = 0.5,
popup = popupTable(n_giant_hogweed,
zcol = c("mun_name_nl", "mun_name_fr", "n")))
m
## 1.3 ####
m <- mapview(n_giant_hogweed,
zcol = "n",
layer.name = "Number of giant hogweed occurrences",
legend = TRUE,
color = "white",
alpha.regions = 0.8,
alpha = 0.5,
map.types = "OpenStreetMap",
popup = popupTable(n_giant_hogweed,
zcol = c("mun_name_nl", "mun_name_fr", "n")))
m
## 1.4 ####
giant_hogweed <- readr::read_tsv(
"data/20250826/20250826_giant_hogweed_fl_bxl.tsv",
na = ""
) %>%
sf::st_as_sf(
coords = c("decimalLongitude", "decimalLatitude"),
crs = 4326
)
m + mapview(giant_hogweed,
layer.name = "Giant hogweed occurrences",
color = "grey80",
col.regions = "grey80",
cex = 5)
## 1.5 ####
leafem::addMouseCoordinates(m + mapview(giant_hogweed,
layer.name = "Giant hogweed occurrences",
color = "grey80",
col.regions = "grey80",
cex = 5))
mapshot(leafem::addMouseCoordinates(m + mapview(giant_hogweed,
layer.name = "Giant hogweed occurrences",
color = "grey80",
col.regions = "grey80",
cex = 5)),
url = "map.html")
```
### Hanna's solution
```
## 1.1 ####
mapview(n_giant_hogweed, zcol = "n", legend = TRUE, color = "white", alpha = 0.8, lwd = 0.5, layer.name = "Number of giant hogweed occurrences", popup = leafpop::popupTable(n_giant_hogweed, c("mun_name_nl", "n", "mun_name_fr")))
## 1.2 ####
mapview(n_giant_hogweed, zcol = "n", legend = TRUE, color = "white", alpha = 0.8, lwd = 0.5, layer.name = "Number of giant hogweed occurrences", popup = leafpop::popupTable(n_giant_hogweed, c("mun_name_nl", "n", "mun_name_fr")))
## 1.3 ####
mapview(n_giant_hogweed, zcol = "n", legend = TRUE, color = "white", alpha = 0.8, lwd = 0.5, layer.name = "Number of giant hogweed occurrences", popup = leafpop::popupTable(n_giant_hogweed, c("mun_name_nl", "n", "mun_name_fr")), map.types = "OpenStreetMap")
## 1.4 ####
giant_hogweed <- readr::read_tsv(
"data/20250826/20250826_giant_hogweed_fl_bxl.tsv",
na = ""
) %>%
sf::st_as_sf(
coords = c("decimalLongitude", "decimalLatitude"),
crs = 4326
)
mapview(n_giant_hogweed, zcol = "n", legend = TRUE, color = "white", alpha = 0.8, lwd = 0.5, layer.name = "Number of giant hogweed occurrences", popup = leafpop::popupTable(n_giant_hogweed, c("mun_name_nl", "n", "mun_name_fr")), map.types = "OpenStreetMap")+
mapview(giant_hogweed, color = "grey80", cex = 5, layer.name = "Giant hogweed occurrences")
## 1.5 ####
m <- mapview(n_giant_hogweed, zcol = "n", legend = TRUE, color = "white", alpha = 0.8, lwd = 0.5, layer.name = "Number of giant hogweed occurrences", popup = leafpop::popupTable(n_giant_hogweed, c("mun_name_nl", "n", "mun_name_fr")), map.types = "OpenStreetMap")+
mapview(giant_hogweed, color = "grey80", cex = 5, layer.name = "Giant hogweed occurrences")
map <- leafem::addMouseCoordinates(m)
## 1.6 ####
# save as HTML
mapshot(m, url = "output/20250826_giant_hogweed_map.html")
```
### Falk's mapview pipe issue odyssee
Thinking too complicated: there is a `year` column.
- I should be able to aggregate over `year` / sum up `n` because otherwise I do not know how the number in the map is produced.
```{r overview_map}
# glimpse(n_giant_hogweed)
# n_giant_hogweed %>%
# count(mun_code) %>%
# arrange(desc(n))
aggregated_hogweed <- n_giant_hogweed %>%
summarise(
n = sum(n),
across(geom, st_union),
.by = mun_code
) %>%
sf::st_as_sf() %>%
sf::st_cast("MULTIPOLYGON")
# does NOT work
aggregated_hogweed %>%
mapview(
color = "white", lwd = 1,
zcol = "n"
)
```
I initially got the error:
```{}
[...]
Caused by error in `dyn.load()`:
! unable to load shared object '/data/R/library/s2/libs/s2.so':
libabsl_flags_internal.so.2501.0.0: cannot open shared object file: No such file or directory
```
found [this](https://github.com/r-spatial/sf/issues/1977) sf issue
- would rather not use `sf_use_s2(FALSE)` because I do not know which other features it disables
- `install.packages("s2")` and reload did the job.
now I can aggregate; seems there is only one row per municipality
- but: cannot plot map any more.
- Geometry type changed. Casting the geometry yields visually identical objects:
```{}
> n_giant_hogweed
Simple feature collection with 319 features and 28 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: 2.541 ymin: 50.69 xmax: 5.911 ymax: 51.51
Geodetic CRS: WGS 84
> aggregated_hogweed
Simple feature collection with 319 features and 2 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: 2.541 ymin: 50.69 xmax: 5.911 ymax: 51.51
Geodetic CRS: WGS 84
```
```{r}
identical(
n_giant_hogweed %>% select(mun_code, n),
aggregated_hogweed
) # NOT identical
# waldo::compare(
# n_giant_hogweed %>% select(mun_code, n),
# aggregated_hogweed
# )
# -> nothing of relevance
```
Solution:
the objects were fine; turned out it was the PIPE!
```{r}
# does NOT work
aggregated_hogweed %>%
mapview(
color = "white", lwd = 1,
zcol = "n"
)
# DOES work
mapview(
aggregated_hogweed
color = "white", lwd = 1,
zcol = "n"
)
# # (by the way, this was already wrong:)
# n_giant_hogweed %>%
# mapview()
```
Thank you, Emma!
Post coding club addition: the problem actually is a bug in mapview while dealing with the pipe: see https://github.com/r-spatial/mapview/issues/348
Still to be fixed by package authors.
## Challenge 2
### Solution Stijn
n_giant_hogweed_bxl <- n_giant_hogweed %>%
dplyr::filter(reg_name_nl == "['Brussels Hoofdstedelijk Gewest']")
g2 <- ggplot(data = n_giant_hogweed_bxl) +
geom_sf(aes(fill=n),colour="red") +
scale_fill_viridis_c(option = "C") +
geom_sf_text(
aes(label = mun_name_nl),
size = 2,
fontface = "bold",
color = "gray50",
check_overlap = TRUE
)
g2
### Pieter's solution
```r
# Get n_giant_hogweed for Brussels only
n_giant_hogweed_bxl <- n_giant_hogweed %>%
dplyr::filter(reg_name_nl == "['Brussels Hoofdstedelijk Gewest']")
## 2.1 ####
n_giant_hogweed_bxl %>%
ggplot() +
geom_sf()
## 2.2 ####
n_giant_hogweed_bxl %>%
ggplot(aes(fill = n,
color = "red4")) +
geom_sf()
## 2.3 ####
n_giant_hogweed_bxl %>%
ggplot() +
geom_sf(aes(fill = n,
color = "red4")) +
geom_sf_label(aes(label = stringr::str_extract(mun_name_nl, "[a-zA-Z\\-]+")),
size = 2,
colour = "grey50",
fontface = "bold",
fill = "white"
)
## 2.4 ####
n_giant_hogweed_bxl %>%
ggplot(aes(fill = n)) +
scale_fill_viridis_c() +
geom_sf()
## 2.5 ####
n_giant_hogweed_bxl %>%
ggplot() +
geom_sf(aes(fill = n,
color = "red4")) +
geom_label_repel(
aes(label = stringr::str_extract(mun_name_nl, "[a-zA-Z\\-]+"),
geometry = geom,
size = 2,
fontface = "bold"),
stat = "sf_coordinates"
)
```
### Emma's solution
```
## 2.1 ####
ggplot(data = n_giant_hogweed_bxl) +
geom_sf()
## 2.2 ####
ggplot(data = n_giant_hogweed_bxl) +
geom_sf(color = "red", aes(fill = n))
## 2.3 ####
ggplot(data = n_giant_hogweed_bxl) +
geom_sf(color = "red", aes(fill = n)) +
geom_sf_text(aes(label = mun_name_nl),
color = "gray50",
size = 2,
fontface = "bold")
## 2.4 ####
ggplot(data = n_giant_hogweed_bxl) +
geom_sf(color = "red", aes(fill = n)) +
geom_sf_text(aes(label = mun_name_nl),
color = "gray50",
size = 2,
fontface = "bold") +
scale_colour_viridis_c()
## 2.5 ####
ggplot(data = n_giant_hogweed_bxl) +
geom_sf(color = "red", aes(fill = n)) +
geom_text_repel(aes(label = mun_name_nl, geometry = geom),
stat = "sf_coordinates",
color = "gray50",
size = 2,
fontface = "bold") +
scale_colour_viridis_c()
```
### Note on `geom_sf()` and CRS *(Falk)*
```{r}
hogweed_31370 <- n_giant_hogweed_bxl %>%
sf::st_transform(31370)
print(sf::st_crs(hogweed_31370))
hogweed_31370 %>%
ggplot() + geom_sf()
# shows axis labels in degrees
```
## Challenge 3
### Solution Stijn
```r
g3 <- ggplot(data = n_giant_hogweed_bxl) +
annotation_map_tile(zoomin = -1, cachedir = "./data/20250826/") +
geom_sf(aes(fill=n),colour="red") +
scale_fill_viridis_c(option = "C") +
geom_sf_text(
aes(label = mun_name_nl),
size = 2,
fontface = "bold",
color = "gray50",
check_overlap = TRUE
) +
annotation_scale(location = "bl", width_hint = 0.5) +
annotation_north_arrow(location = "tl", which_north = "true",
style = north_arrow_fancy_orienteering)
g3
```
### Pieter's solution
```r
## 3.1 -> 3.3 ####
my_tempdir <- withr::local_tempdir(pattern = "coding_club", tmpdir = tempdir())
n_giant_hogweed_bxl %>%
ggplot() +
annotation_map_tile(
type = "osm",
zoomin = 0,
cachedir = my_tempdir
) +
annotation_north_arrow(location = "tl", which_north = "true") +
geom_sf(aes(fill = n))
## 3.4 ####
n_giant_hogweed_bxl %>%
ggplot() +
annotation_map_tile(
type = "cartolight",
zoomin = 0,
cachedir = my_tempdir
) +
annotation_north_arrow(location = "tl", which_north = "true") +
geom_sf(aes(fill = n))
```
### Emma's solution
```
## 3.1 ####
ggplot(data = n_giant_hogweed_bxl) +
annotation_map_tile(zoomin = 0) +
geom_sf(color = "red", aes(fill = n)) +
geom_text_repel(aes(label = mun_name_nl, geometry = geom),
stat = "sf_coordinates",
color = "gray50",
size = 2,
fontface = "bold") +
scale_colour_viridis_c()
## 3.2 ####
ggplot(data = n_giant_hogweed_bxl) +
annotation_map_tile(zoomin = 0, cachedir = "./data/20250826") +
geom_sf(color = "red", aes(fill = n)) +
geom_text_repel(aes(label = mun_name_nl, geometry = geom),
stat = "sf_coordinates",
color = "gray50",
size = 2,
fontface = "bold") +
scale_colour_viridis_c()
## 3.3 ####
ggplot(data = n_giant_hogweed_bxl) +
annotation_map_tile(zoomin = 0, cachedir = "./data/20250826") +
geom_sf(color = "red", aes(fill = n)) +
geom_text_repel(aes(label = mun_name_nl, geometry = geom),
stat = "sf_coordinates",
color = "gray50",
size = 2,
fontface = "bold") +
scale_colour_viridis_c() +
annotation_scale(location = "bl") +
annotation_north_arrow(location = "tl",
style = north_arrow_fancy_orienteering,
which_north = "true")
## 3.4 ####
ggplot(data = n_giant_hogweed_bxl) +
annotation_map_tile(type = "cartolight",
zoomin = 0,
cachedir = "./data/20250826") +
geom_sf(color = "red", aes(fill = n)) +
geom_text_repel(aes(label = mun_name_nl, geometry = geom),
stat = "sf_coordinates",
color = "gray50",
size = 2,
fontface = "bold") +
scale_colour_viridis_c() +
annotation_scale(location = "bl") +
annotation_north_arrow(location = "tl",
style = north_arrow_fancy_orienteering,
which_north = "true")
```