require(readr)
require(sf)
require(dplyr)
require(ggplot2)
require(tidyr)
require(lubridate)
require(plotly)Week 5 Excersices
Load all Modules
Task 1: Write my own functions
BMI <- function(weight, height) {
weight/(height^2)
}
converter_F_C <- function(celsius) {
celsius * (9/5) + 35
}
euclidian_distance <- function(x1,y1,x2,y2) {
sqrt((x1-x2)^2 - (y1-y2)^2)
}Task 2 Prepare Analysis
wildschwein <- read_delim("data/wildschwein_BE_2056.csv", ",")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.
data <- wildschwein |>
filter(
TierName %in% c("Rosa", "Sabi"),
DatetimeUTC >= "2015-04-01",
DatetimeUTC < "2015-04-15"
)
distance_by_element <- function(later, now) {
as.numeric(
st_distance(later, now, by_element = TRUE)
)
}Task 3: Create Join Key
data <- data |>
mutate(DatetimeRound = round_date(DatetimeUTC, unit = "15 minutes"))Task 4/5
sabi_data <- data |>
filter(
TierName == 'Sabi'
)
rosa_data <- data |>
filter(
TierName == 'Rosa'
)
meets <- sabi_data|>
inner_join(rosa_data, by = "DatetimeRound", suffix = c("_sabi", "_rosa")) |>
mutate(Euclidean_Distance = sqrt((E_sabi - E_rosa)^2 + (N_sabi - N_rosa)^2)) |>
mutate(Meets = Euclidean_Distance < 100) |>
filter(Meets)
ggplot() +
geom_point(data = sabi_data, aes(x = E, y = N, color = "Sabi"), size = 1, alpha = 0.5) +
geom_point(data = rosa_data, aes(x = E, y = N, color = "Rosa"), size = 1, alpha = 0.5) +
geom_point(data = meets, aes(x = E_sabi, y = N_sabi, color = "Meets"), size = 3, alpha = 0.3) +
scale_color_manual(values = c("Sabi" = "blue", "Rosa" = "green", "Meets" = "red")) +
xlim(min(sabi_data$E, rosa_data$E), max(sabi_data$E, rosa_data$E)) +
ylim(min(sabi_data$N, rosa_data$N), max(sabi_data$N, rosa_data$N)) +
labs(title = "Räumliche Visualisierung der Treffen",
x = "E",
y = "N",
color = "Legend") +
theme_minimal()Task 6: with Plotly
plot_ly() %>%
# Rosa und Sabi Pfade
add_trace(data = sabi_data, x = ~E, y = ~N, z = ~DatetimeRound, type = 'scatter3d', mode = 'lines',
line = list(color = 'blue', width = 3), name = 'Sabi Path') %>%
add_trace(data = rosa_data, x = ~E, y = ~N, z = ~DatetimeRound, type = 'scatter3d', mode = 'lines',
line = list(color = 'green', width = 3), name = 'Rosa Path') %>%
# Treffen (Meets)
add_trace(data = meets, x = ~E_sabi, y = ~N_sabi, z = ~DatetimeRound, type = 'scatter3d', mode = 'markers',
marker = list(color = 'red', size = 5, opacity = 0.7), name = 'Meets') %>%
layout(title = "Space-Time Cube: Meeting Patterns and Trajectories",
scene = list(
xaxis = list(title = "Easting (E)"),
yaxis = list(title = "Northing (N)"),
zaxis = list(title = "Time (DatetimeRound)")),
legend = list(x = 0.8, y = 0.9))