Tasks

Task 1: Writing functions

BMI

BMI <- function(weight, height) {weight/(height^2)}

Celsius to Fahrenheit

CtoF <- function(celsius) {celsius*9/5+32}

Euclidean distance

EuDi <- function(coord1, coord2) {sqrt(((coord2$x - coord1$x)^2)+((coord2$y - coord1$y)^2))}

Task 2: Prepare Analysis

Libraries

library(pacman)
p_load(readr)
p_load(dplyr)
p_load(lubridate)
p_load(sf)
p_load(tmaptools)
p_load(ggplot2)
p_load(plotly)
wild <- read_delim("wildschwein_BE_2056.csv") |> 
  filter(TierName %in% c("Sabi","Rosa"),
        DatetimeUTC >= ymd_hms("2015-04-01 00:00:00") & DatetimeUTC <= ymd_hms("2015-04-15 23:59:59")) 
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 3: Create Join Key

Round Datetimes

wild$Datetime_round <- round_date(wild$DatetimeUTC, unit = "15 mins")

Task 4: Measuring distance at concurrent locations

Split df

sabi <- wild |> 
  filter(TierName == "Sabi")

rosa <- wild |> 
  filter(TierName == "Rosa")

Join by date

joined <- full_join(sabi, rosa, by = "Datetime_round", suffix = c("sabi", "rosa"))

Calculate Euclidean distance

joined <- joined |> 
  mutate(
    locsabi = st_sfc(mapply(function(x, y) st_point(c(x, y)), 
                                joined$Esabi, joined$Nsabi, SIMPLIFY = FALSE),
                         crs = 2056),
    locrosa = st_sfc(mapply(function(x, y) st_point(c(x, y)), 
                                joined$Erosa, joined$Nrosa, SIMPLIFY = FALSE),
                         crs = 2056)) |> 
    st_as_sf() |> 
    mutate(
      distance = as.numeric(st_distance(locrosa, locsabi, by_element = TRUE)))

Apply Threshold of 100m

joined <- joined |> 
  mutate(
    clash = distance < 100
  )

Task 5: Visualize data

meets_sf <- joined %>% filter(clash)

# Plot
ggplot() +
  # Plot rosa points
  geom_point(data = rosa, aes(x = E, y = N, color = "Rosa Points"), alpha = 0.5, size = 2) +
  
  # Plot sabi points
  geom_point(data = sabi, aes(x = E, y = N, color = "Sabi Points"), alpha = 0.5, size = 2) +
  
  # Plot meets (Esabi and Erosa)
  geom_point(data = meets_sf, aes(x = Esabi, y = Nsabi, color = "Meet Points Sabi"), size = 4, shape = 18) +
  geom_point(data = meets_sf, aes(x = Erosa, y = Nrosa, color = "Meet Points Rosa"), size = 4, shape = 18) +
  
  # Labels and theme
  labs(title = "Spatial Visualization of Meets") +
  theme_minimal() +
  
  # Manually set the colors for each group in the legend
  scale_color_manual(
    values = c("Rosa Points" = "blue", "Sabi Points" = "red", "Meet Points Sabi" = "green", "Meet Points Rosa" = "orange"),
    name = "Point Type"
  ) +
  
  # Adjusting legend position
  theme(legend.position = "right")

Task 6: Visualize as timecube

fig <- plot_ly(joined, x = ~Esabi, y = ~Nsabi, z = ~Datetime_round, type = 'scatter3d', mode = 'lines', opacity = 1, line = list(width = 6, color = "blue")) |> 
  add_trace(joined, x = ~Erosa, y = ~Nrosa, z = ~Datetime_round, type = 'scatter3d', mode = 'lines', opacity = 1, line = list(width = 6, color = "red"))


fig