week5_exercise

Author

anna

Task 1: Write own functions

  • BMI
  • Celcius to farenheight
  • Euclidean Distances

Calculate persons BMI based on height and weight

bmi <- function(weight_kg, height_m){ 
     bmi_output <- weight_kg/height_m^2
     return(bmi_output)
}

bmi(70, 1.7)
[1] 24.22145
bmi(60, 1.68)
[1] 21.2585

Convert degrees Celcius to Farenheight

celcius_to_farenheight <- function(celcius){celcius * 9/5+32}

celcius_to_farenheight(30)
[1] 86

Calculate distance between two sets of coordinates

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

euclidean_distance(0, 0, 1, 1)
[1] 1.414214

Task 2: Prepare Analysis

wildschwein <- read_delim("data/wildschwein_BE_2056.csv")

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

Task 3: Create Join Key

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

Task 4: 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(euclidean_distance =  euclidean_distance(E_Rosa, N_Rosa, E_Sabi, N_Sabi))

# define threshold

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

Task 5: Visualize data

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 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")

Task 6

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")  
    )
  )