Exercise_5

Author

Robin Merz

Setup

rm(list = ls())

#libraries
library(tidyverse)
library(ggplot2)
library(tmap)
library(sf)
library(readr)

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

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

Euclidian <- function(x1, x2, y1, y2){
  sqrt(((x2-x1)^2) + ((y2-y1)^2))
}

#additional way: store functions in different script and read in script with source("functions.skript"

#additional info: browser() function for debugging in R scripts

Exercise 1

Information: Functions in Setup

#Calculations
My_BMI <- BMI(87, 1.93)
My_BMI
[1] 23.35633
Temperature_f <- Farenheit(20)
Temperature_f
[1] 68
Distances <- Euclidian(12345, 12347, 23456, 23459)
Distances
[1] 3.605551

Exercise 2:

prepare analysis

wildschwein <- read_delim("wildschwein_BE_2056.csv")

wildschwein <- wildschwein %>%
  filter(TierName %in% c("Rosa", "Sabi"), DatetimeUTC >= "2015-04-01" & DatetimeUTC <= "2015-04-15")

Create join key

wildschwein <- wildschwein %>%
  mutate(RoundedTime = round_date(DatetimeUTC, unit = "15 minutes"))

Measuring distance at concurrent locations

# separate
rosa <- wildschwein %>% filter(TierName == "Rosa")
sabi <- wildschwein %>% filter(TierName == "Sabi")

# join
join <- full_join(rosa, sabi, by = "RoundedTime", suffix = c("_Rosa", "_Sabi"))

# euclidean distance, function from above

join <- join %>% mutate(Euclidian =  Euclidian(E_Rosa, E_Sabi, N_Rosa, N_Sabi))

# define threshold

join <- join %>%
  mutate(Meet = Euclidian <= 100)

Visualize

meets_data <- join %>%
  filter(Meet == TRUE)

ggplot() +
  geom_point(data = meets_data, aes(x = E_Rosa, y = N_Rosa), color = "blue", size = 3, alpha = 0.7, shape = 16) +
  geom_point(data = meets_data, aes(x = E_Sabi, y = N_Sabi), color = "red", size = 3, alpha = 0.7, shape = 16) +
  labs(title = "Spatial Locations of the meeting points of Rosa and Sabi",
       x = "East",
       y = "North") +
  xlim(min(meets_data$E_Rosa, meets_data$E_Sabi) - 100, max(meets_data$E_Rosa, meets_data$E_Sabi) + 100) +
  ylim(min(meets_data$N_Rosa, meets_data$N_Sabi) - 100, max(meets_data$N_Rosa, meets_data$N_Sabi) + 100) +
  theme_minimal() +
  theme(legend.position = "none")

meets_data <- join %>%
  filter(Meet == TRUE)

ggplot() +
  geom_point(data = sabi, aes(x = E, y = N), color = "lightblue", size = 2, alpha = 0.6, shape = 16) +
  geom_point(data = rosa, aes(x = E, y = N), color = "lightcoral", size = 2, alpha = 0.6, shape = 16) +
  geom_point(data = meets_data, aes(x = E_Sabi, y = N_Sabi), color = "blue", size = 3, alpha = 0.7, shape = 16) +
  geom_point(data = meets_data, aes(x = E_Rosa, y = N_Rosa), color = "red", size = 3, alpha = 0.7, shape = 16) +
  labs(title = "Spatial Locations of Rosa and Sabi with Meets",
       x = "East",
       y = "North") +
  xlim(min(c(rosa$E, sabi$E)) - 100, max(c(rosa$E, sabi$E)) + 100) +
  ylim(min(c(rosa$N, sabi$N)) - 100, max(c(rosa$N, sabi$N)) + 100) +
  theme_minimal() +
scale_color_manual(
    values = c("lightblue", "lightcoral", "blue", "red"),
    labels = c("Rosa (Regular)", "Sabi (Regular)", "Rosa (Meet)", "Sabi (Meet)")
  ) +
  theme(legend.position = "right")

Time-cube

library(plotly)
wildschwein$Timestamp <- as.numeric(wildschwein$DatetimeUTC)

# Treffpunkte filtern
meets_data <- join %>% filter(Meet == TRUE)


start_date <- as.Date("2015-04-01")
end_date <- as.Date("2015-04-06")

# Filtern der Daten, um nur den Zeitraum vom 1. bis 9. April anzuzeigen
rosa <- rosa %>%
  filter(RoundedTime >= start_date & RoundedTime <= end_date)

sabi <- sabi %>%
  filter(RoundedTime >= start_date & RoundedTime <= end_date)

meets_data <- meets_data %>%
  filter(RoundedTime >= start_date & RoundedTime <= end_date)




plot_ly() %>%
  add_trace(
    data = rosa,
    x = ~E, y = ~N, z = ~RoundedTime,
    type = "scatter3d", mode = "lines",
    line = list(color = 'orange', width = 2),
    name = "Rosa"
  ) %>%
  add_trace(
    data = sabi,
    x = ~E, y = ~N, z = ~RoundedTime,
    type = "scatter3d", mode = "lines",
    line = list(color = 'blue', width = 2),
    name = "Sabi"
  ) %>%
  add_trace(
    data = meets_data,
    x = ~E_Sabi, y = ~N_Sabi, z = ~RoundedTime,
    type = "scatter3d", mode = "markers",
    marker = list(size = 5, color = 'green', opacity = 0.8),
    name = "Treffpunkte"
  ) %>%
  layout(
    title = "Space-Time Cube: Treffen von Rosa & Sabi",
    scene = list(
      xaxis = list(title = "Ost (E)"),
      yaxis = list(title = "Nord (N)"),
      zaxis = list(title = "Datum", autorange = "true")  
    )
  )