rm(list = ls())
#libraries
library(tidyverse)
library(ggplot2)
library(tmap)
library(sf)
library(readr)
#functions
<- function(weight, height){
BMI /(height^2)
weight
}
<- function(celsius){
Farenheit * (9/5) + 32
celsius
}
<- function(x1, x2, y1, y2){
Euclidian 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_5
Setup
Exercise 1
Information: Functions in Setup
#Calculations
<- BMI(87, 1.93)
My_BMI My_BMI
[1] 23.35633
<- Farenheit(20)
Temperature_f Temperature_f
[1] 68
<- Euclidian(12345, 12347, 23456, 23459)
Distances Distances
[1] 3.605551
Exercise 2:
prepare analysis
<- read_delim("wildschwein_BE_2056.csv")
wildschwein
<- 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
<- wildschwein %>% filter(TierName == "Rosa")
rosa <- wildschwein %>% filter(TierName == "Sabi")
sabi
# join
<- full_join(rosa, sabi, by = "RoundedTime", suffix = c("_Rosa", "_Sabi"))
join
# euclidean distance, function from above
<- join %>% mutate(Euclidian = Euclidian(E_Rosa, E_Sabi, N_Rosa, N_Sabi))
join
# define threshold
<- join %>%
join mutate(Meet = Euclidian <= 100)
Visualize
<- join %>%
meets_data 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")
<- join %>%
meets_data 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)
$Timestamp <- as.numeric(wildschwein$DatetimeUTC)
wildschwein
# Treffpunkte filtern
<- join %>% filter(Meet == TRUE)
meets_data
<- as.Date("2015-04-01")
start_date <- as.Date("2015-04-06")
end_date
# 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")
) )