owned this note
owned this note
Published
Linked with GitHub
# INBO CODING CLUB
9 December 2021
Welcome!
Participants?
Yasmine
## 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*)
Share your solutions here below. Use `### my name solutions` to start a subsection with your solution. Thanks.
## Challenge 1
```r
legend <- tribble(
~red, ~green, ~blue, ~land_use,
223, 115, 255, "open natuur",
38, 115, 0, "bos",
152, 230, 0, "grasland",
255, 255, 0, "akker",
168, 0, 0, "urbaan",
137, 205, 102, "laag groen",
92, 137, 68, "hoog groen",
0, 197, 255, "water",
204, 204, 204, "overig") %>%
mutate(color = rgb(red, green, blue, maxColorValue = 255))
```
### floris
#' Challenge 1A - categorical rasters
```r
collegend <-
tibble(color = c(rgb(223, 115, 255, maxColorValue = 255),
rgb(38, 115, 0, maxColorValue = 255),
rgb(152, 230, 0, maxColorValue = 255),
rgb(255, 255, 0, maxColorValue = 255),
rgb(168, 0, 0, maxColorValue = 255),
rgb(137, 205, 102, maxColorValue = 255),
rgb(92, 137, 68, maxColorValue = 255),
rgb(0, 197, 255, maxColorValue = 255),
rgb(204, 204, 204, maxColorValue = 255)),
label = c("Open natuur",
"Bos",
"Grasland",
"Akker",
"Urbaan",
"Laag groen",
"Hoog groen",
"Water",
"Overig"))
```
#' Challenge 1B - continous rasters
```r
nitrogen <- rast("./data/20211209/20211209_vlops_tmd_1km_20MZ17M517.tif")
nitrogen
plot(nitrogen,
type = "continuous",
range = c(10, 47),
col = hcl.colors(n = 100, palette = "Blue-Red 3"))
nitrogenbreaks <- quantile(nitrogen[],
probs = c(0, 0.1, 0.25, 0.5, 0.75, 0.9, 1),
na.rm = TRUE)
plot(nitrogen,
type = "interval",
breaks = nitrogenbreaks,
col = hcl.colors(n = 6, palette = "TealRose"))
```
Raïsa
```r
##1A
legend <- data.frame(
land_use = c('Open natuur', 'Bos', 'Grasland', 'Akker', 'Urbaan',
'Laag groen', 'Hoog groen', 'Water', 'Overig'),
color = c(rgb(223, 115, 255, maxColorValue = 255),
rgb(38, 115, 0, maxColorValue = 255),
rgb(152, 230, 0, maxColorValue = 255),
rgb(255, 255, 0, maxColorValue = 255),
rgb(168, 0, 0, maxColorValue = 255),
rgb(137, 205, 102, maxColorValue = 255),
rgb(92, 137, 68, maxColorValue = 255),
rgb(0, 197, 255, maxColorValue = 255),
rgb(204, 204, 204, maxColorValue = 255)
)
)
help(topic = "plot", package = "terra")
lu_nara_2016 <- rast("./data/20211209/20211209_lu_nara_2016_100m.tif")
plot(lu_nara_2016,
type = "classes",
col = legend$color,
levels = legend$land_use)
##1B
nitrogen <- rast("./data/20211209/20211209_vlops_tmd_1km_20MZ17M517.tif")
plot(nitrogen,
col = hcl.colors(n = 100, palette = "Reds"),
range = c(10, 47))
#' - How to plot `nitrogen` with only 6 different color intervals? Use the color
#' palette you prefer.
plot(nitrogen,
type = "interval",
breaks = seq(10, 47, 6),
col = hcl.colors(n = 100, palette = "Blue-Red 3"))
```
Hans:
```r
#' Create a continuous palette function setting the right range (domain) for nitrogen. Tip:
#' https://rstudio.github.io/leaflet/colors.html)
pal <- colorNumeric(
palette = "Reds",
domain = c(0,47),
na.color = "transparent"
)
#' make the leaflet (adjust the number of bins)
leaflet() %>%
addTiles() %>%
addRasterImage(raster_nitrogen_wgs84,
colors = pal) %>%
addLegend(pal = pal,
values = c(0,47),
title = "Nitrogen deposition <br/> (kg N/(ha.year))",
bins = 5)
```
## Challenge 2
### floris
Challenge 2A
```r
# subset of data for faster plotting
leaflet(data = obs_spatial %>% slice(1:100)) %>%
addTiles() %>%
addCircleMarkers(label = ~species)
leaflet(data = obs_spatial) %>%
addTiles() %>%
addCircleMarkers(clusterOptions = markerClusterOptions())
ggplot(provinces, aes(fill = total)) + geom_sf()
```
Hans:
```r
labels <- sprintf(
"<strong>%s</strong><br/>%g observations",
provinces$NAAM, provinces$total
) %>% lapply(htmltools::HTML)
pal <- colorNumeric("YlOrRd", domain = range(provinces$total))
leaflet() %>%
addTiles() %>%
addPolygons(fillColor = ~pal(total),
label = labels,
data = provinces) %>%
addLegend(pal = pal,
values = ~total,
opacity = 0.7,
title = "Total number of observations",
position = "bottomright",
data = provinces)
```