Task 3: Create Join Key To compare Rosa and Sabi’s locations, we first need to match the two animals temporally. For that we can use a join, but need identical time stamps to serve as a join key. We therefore need to slightly adjust our time stamps to a common, concurrent interval.
library(dplyr)library(lubridate)wildschwein <- wildschwein %>%mutate(DatetimeUTC =ymd_hms(DatetimeUTC), # Convert to POSIXct formatDatetimeRound =round_date(DatetimeUTC, unit ="15 minutes") # Round to nearest 15 min )# print(head(wildschwein))
Task 4: Measuring distance at concurrent locations To measure the distance between concurrent locations, we need to follow the following steps.
Split the wildschwein_filter object into one data.frame per animal
Join2 these datasets by the new Datetime column created in the last task. The joined observations are temporally close.
In the joined dataset, calculate Euclidean distances between concurrent observations and store the values in a new column
Use a reasonable threshold on distance to determine if the animals are also spatially close enough to constitute a meet (we use 100 meters). Store this Boolean information (TRUE/FALSE) in a new column
# Step 1.Split the wildschwein_filter dataset by animalwildschwein_rosa <- wildschwein %>%filter(TierName =="Rosa")wildschwein_sabi <- wildschwein %>%filter(TierName =="Sabi")# Step 2. Join the datasets by the rounded datetime columnwildschwein_joined <-inner_join( wildschwein_rosa, wildschwein_sabi,by ="DatetimeRound", # Joining on rounded timestampssuffix =c("_Rosa", "_Sabi") # Add suffixes to distinguish columns)# print(wildschwein_joined)# Step 3. Calculate Euclidean distancewildschwein_joined <- wildschwein_joined %>%mutate(distance_m =sqrt((E_Rosa - E_Sabi)^2+ (N_Rosa - N_Sabi)^2) )# Step 4: Define a "meeting" based on a 100-meter thresholdwildschwein_joined <- wildschwein_joined %>%mutate(is_meeting = distance_m <=100# TRUE if distance is 100m or less )wildschwein_meets <- wildschwein_joined %>%filter(is_meeting ==TRUE)
Task 5: Visualize data
ggplot() +# Plot all locations for Rosageom_point(data = wildschwein_rosa, aes(x = E, y = N), color ="red", alpha =0.5, size =2) +# Plot all locations for Sabigeom_point(data = wildschwein_sabi, aes(x = E, y = N), color ="darkgreen", alpha =0.5, size =2) +# Highlight the meet locationsgeom_point(data = wildschwein_meets, aes(x = E_Rosa, y = N_Rosa), color ="black", size =4, shape =8) +# Manually set axis limits (adjust based on your dataset)xlim(min(wildschwein$E) -50, max(wildschwein$E) +50) +ylim(min(wildschwein$N) -50, max(wildschwein$N) +50) +# Add labels and themelabs(title ="Spatial Visualisation of Rosa (rot) and Sabi (grün)",subtitle ="Black stars (*) indicate meets",x ="Easting (E)", y ="Northing (N)" ) +theme_minimal()
Task 6 (optional): Visualize data as timecube with plotly
# install.packages("plotly")library("plotly")
Warning: Paket 'plotly' wurde unter R Version 4.4.3 erstellt
Attache Paket: 'plotly'
Das folgende Objekt ist maskiert 'package:ggplot2':
last_plot
Das folgende Objekt ist maskiert 'package:stats':
filter
Das folgende Objekt ist maskiert 'package:graphics':
layout
# Schritt 1: Stellen Sie sicher, dass TimeNum korrekt berechnet wirdmin_time <-min(wildschwein$DatetimeUTC, na.rm =TRUE)# Berechnung von TimeNum für den gesamten wildschwein-Datensatzwildschwein <- wildschwein %>%mutate(TimeNum =as.numeric(difftime(DatetimeUTC, min_time, units ="hours"))) # Zeit in Stunden# Berechnung von TimeNum für den wildschwein_meets-Datensatzwildschwein_meets <- wildschwein_joined %>%filter(is_meeting ==TRUE) %>%mutate(TimeNum =as.numeric(difftime(DatetimeRound, min_time, units ="hours"))) # Zeit in Stunden