Week 6

Input

library("readr")
library("sf")
Warning: Paket 'sf' wurde unter R Version 4.5.2 erstellt
Linking to GEOS 3.13.1, GDAL 3.11.4, PROJ 9.7.0; sf_use_s2() is TRUE
library("tmap")
Warning: Paket 'tmap' wurde unter R Version 4.5.2 erstellt
library("dplyr")

Attache Paket: 'dplyr'
Die folgenden Objekte sind maskiert von 'package:stats':

    filter, lag
Die folgenden Objekte sind maskiert von 'package:base':

    intersect, setdiff, setequal, union
library("lubridate")
Warning: Paket 'lubridate' wurde unter R Version 4.5.2 erstellt

Attache Paket: 'lubridate'
Die folgenden Objekte sind maskiert von 'package:base':

    date, intersect, setdiff, union
library("tidyr")
library("ggplot2")
library("terra")
Warning: Paket 'terra' wurde unter R Version 4.5.2 erstellt
terra 1.8.70

Attache Paket: 'terra'
Das folgende Objekt ist maskiert 'package:tidyr':

    extract
wildschwein_BE <- read_delim("datasets/wildschwein_BE_2056.csv", ",") |>
    st_as_sf(coords = c("E", "N"), crs = 2056, remove = FALSE)
Rows: 51246 Columns: 6
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr  (2): TierID, TierName
dbl  (3): CollarID, E, N
dttm (1): DatetimeUTC

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Task 1

── tmap v3 code detected ───────────────────────────────────────────────────────
[v3->v4] `tm_polygons()`: use 'fill' for the fill color of polygons/symbols
(instead of 'col'), and 'col' for the outlines (instead of 'border.col').
[v3->v4] `tm_polygons()`: use `fill_alpha` instead of `alpha`.
This message is displayed once every 8 hours.

Task 2

wildboar <- wildschwein_BE %>%
  filter(month(DatetimeUTC) %in% c(5, 6))

fanel_boar_join <- st_join(
  wildboar,
  fanel
)

tm_shape(fanel) +
  tm_polygons(
    col = "Frucht",
    palette = "Set3",
    border.col = "black",
    alpha = 0.6
  )
── tmap v3 code detected ───────────────────────────────────────────────────────
[v3->v4] `tm_tm_polygons()`: migrate the argument(s) related to the scale of
the visual variable `fill` namely 'palette' (rename to 'values') to fill.scale
= tm_scale(<HERE>).
[v3->v4] `tm_polygons()`: use `fill_alpha` instead of `alpha`.
[cols4all] color palettes: use palettes from the R package cols4all. Run
`cols4all::c4a_gui()` to explore them. The old palette name "Set3" is named
"brewer.set3"
Multiple palettes called "set3" found: "brewer.set3", "hcl.set3". The first one, "brewer.set3", is returned.
Warning: Number of levels of the variable assigned to the aesthetic "fill" of
the layer "polygons" is 44, which is larger than n.max (which is 30), so levels
are combined.
[plot mode] fit legend/component: Some legend items or map compoments do not
fit well, and are therefore rescaled.
ℹ Set the tmap option `component.autoscale = FALSE` to disable rescaling.

tm_shape(fanel) +
  tm_polygons(
    col = "Frucht",
    palette = "Set3",
    border.col = "black",
    alpha = 0.6
  ) +
  tm_shape(fanel_boar_join) +
  tm_dots(
    size = 0.09,
    col = "red"
  )

── tmap v3 code detected ───────────────────────────────────────────────────────
[v3->v4] `tm_tm_polygons()`: migrate the argument(s) related to the scale of
the visual variable `fill` namely 'palette' (rename to 'values') to fill.scale
= tm_scale(<HERE>).[v3->v4] `tm_polygons()`: use `fill_alpha` instead of `alpha`.[cols4all] color palettes: use palettes from the R package cols4all. Run
`cols4all::c4a_gui()` to explore them. The old palette name "Set3" is named
"brewer.set3"Multiple palettes called "set3" found: "brewer.set3", "hcl.set3". The first one, "brewer.set3", is returned.
Warning: Number of levels of the variable assigned to the aesthetic "fill" of
the layer "polygons" is 44, which is larger than n.max (which is 30), so levels
are combined.
[plot mode] fit legend/component: Some legend items or map compoments do not
fit well, and are therefore rescaled.
ℹ Set the tmap option `component.autoscale = FALSE` to disable rescaling.

fanel_boar_join$hour <- round_date(fanel_boar_join$DatetimeUTC, "hour")
fanel_boar_join$hour <- hour(fanel_boar_join$DatetimeUTC)

Task 3

df <- fanel_boar_join %>%
  st_drop_geometry()

ggplot(df, aes(x = hour, fill = Frucht)) +
  geom_bar(position = "fill") +
  facet_wrap(~ TierName) +
  scale_y_continuous(labels = scales::percent) +
  labs(x = "Hour", y = "Proportion (100%)", fill = "Frucht") +
  theme_minimal()

Task 4

vhm <- rast("datasets/vegetationshoehe_LFI.tif")

tm_shape(vhm)+
  tm_raster()
SpatRaster object downsampled to 2753 by 3634 cells.

fanel_boar_join$vhm_height <- extract(
  vhm,
  vect(fanel_boar_join)
)[,2]

#create bins
fanel_boar_join$vhm_bin <- case_when(
  fanel_boar_join$vhm_height < 1 ~ "<1 m",
  fanel_boar_join$vhm_height >= 1 & fanel_boar_join$vhm_height < 2 ~ "1–2 m",
  fanel_boar_join$vhm_height >= 2 & fanel_boar_join$vhm_height < 5 ~ "2–5 m",
  fanel_boar_join$vhm_height >= 5 ~ ">5 m",
  TRUE ~ NA_character_
)

df <- fanel_boar_join %>%
  st_drop_geometry()

ggplot(df, aes(x = hour, fill = vhm_bin)) +
  geom_bar(position = "fill") +
  facet_wrap(~ TierName) +
  scale_y_continuous(labels = scales::percent) +
  labs(x = "Hour", y = "Proportion (100%)", fill = "vhm_bin") +
  theme_minimal()