Solutions

Tip

Hover over the code and copy the content by clicking on the clipboard icon on the top right. You can now paste this into an R-Script.

# task_1.R
################################################################################


crop_fanel <- read_sf("datasets/Feldaufnahmen_Fanel.gpkg")

head(crop_fanel)

summary(crop_fanel)

unique(crop_fanel$Frucht)

st_crs(crop_fanel)



library("ggplot2")

ggplot(crop_fanel) +
  geom_sf(aes(fill = Frucht))




# task_2.R
################################################################################



library("dplyr")
library("lubridate")

wildschwein_summer <- wildschwein_BE |>
  filter(month(DatetimeUTC) %in% 5:6)

wildschwein_summer <- st_join(wildschwein_summer, crop_fanel)

wildschwein_summer

ggplot(crop_fanel) +
  geom_sf(aes(fill = Frucht)) +
  geom_sf(data = wildschwein_summer)




# task_3.R
################################################################################


library("forcats")

wildschwein_smry <- wildschwein_summer |>
  st_set_geometry(NULL) |>
  mutate(
    hour = hour(round_date(DatetimeUTC, "hour")),
    Frucht = ifelse(is.na(Frucht), "other", Frucht),
    Frucht = fct_lump(Frucht, 5, other_level = "other"),
  ) |>
  count(TierName, hour, Frucht) |>
  group_by(TierName, hour) |>
  mutate(perc = n / sum(n)) |>
  ungroup() |>
  mutate(
    Frucht = fct_reorder(Frucht, n, sum, desc = TRUE)
  )

ggplot(wildschwein_smry, aes(hour, perc, fill = Frucht)) +
  geom_col(width = 1) +
  scale_fill_brewer(palette = "Set2") +
  scale_y_continuous(name = "Percentage", labels = scales::percent_format()) +
  scale_x_continuous(name = "Time (rounded to the nearest hour)") +
  facet_wrap(~TierName) +
  theme_light() +
  labs(
    title = "Percentages of samples in a given crop per hour",
    subtitle = "Only showing the most common categories"
  )


wildschwein_smry |>
  filter(TierName == "Rosa") |>
  ggplot(aes(hour, perc, fill = Frucht)) +
  geom_col(width = 1, colour = "black") +
  scale_fill_brewer(palette = "Set2") +
  scale_x_continuous(breaks = seq(0,23)) +
  coord_polar(start = -pi/24) +
  labs(
    title = "Percentages of samples in a given crop per hour",
    subtitle = "Only showing the most common categories for 'Rosa'"
  ) + 
  theme_light() +
  theme(axis.text.y = element_blank(),axis.title = element_blank())






# task_4.R
################################################################################


library("terra")
library("tmap")

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

tm_shape(veg_height) +
  tm_raster(palette = "viridis", style = "cont", legend.is.portrait = FALSE) +
  tm_layout(legend.outside = TRUE, legend.outside.position = "bottom", frame = FALSE)




# task_5.R
################################################################################


veg_height_df <- terra::extract(veg_height, st_coordinates(wildschwein_BE))

wildschwein_BE <- cbind(wildschwein_BE, veg_height_df)

wildschwein_BE