library(tidyverse)

hr <- read_csv('heart_rate.csv') %>%
	mutate(
		hour = hour(timestamp),
		time_dec = hour(timestamp) + minute(timestamp) / 60
	) %>%
	filter(hour < 11) %>% 
	select(-hour)

temp <- read_csv('body_temperature.csv') %>%
	mutate(
		hour = hour(timestamp),
		time_dec = hour(timestamp) + minute(timestamp) / 60
	) %>%
	filter(hour < 11) %>% 
	select(-hour)


res <- left_join(hr, temp, by='timestamp')


hr %>% write_tsv("hr.tsv")
temp %>% write_tsv("temp.tsv")

intervals <- read_tsv("intervals.tsv") %>%
	mutate(
		start_dec = hour(start) + minute(start) / 60,
		end_dec = hour(end) + minute(end) / 60
	)

print(intervals)

ymin = 50
ymax = 150

hr %>% 
	ggplot(aes(x=time_dec, y=`beats per minute`)) +
	labs(x="Time (UTC)", y="Beats Per Minute (BPM)") +
	annotate("rect", xmin = (intervals %>% slice(1))$start_dec, xmax = (intervals %>% slice(1))$end_dec, ymin = ymin, ymax = ymax,  alpha = .2) + 
	annotate("text", x= mean(c((intervals %>% slice(1))$start_dec, (intervals %>% slice(1))$end_dec)), y=ymax-2, label="Sleep") +
	annotate("rect", xmin = (intervals %>% slice(2))$start_dec, xmax = (intervals %>% slice(2))$end_dec, ymin = ymin, ymax = ymax,  alpha = .2) + 
	annotate("text", x= mean(c((intervals %>% slice(2))$start_dec, (intervals %>% slice(2))$end_dec)), y=ymax-2, label="Sleep") +
	annotate("rect", xmin = (intervals %>% slice(3))$start_dec, xmax = (intervals %>% slice(3))$end_dec, ymin = ymin, ymax = ymax,  alpha = .2) + 
	annotate("text", x= mean(c((intervals %>% slice(3))$start_dec, (intervals %>% slice(3))$end_dec)), y=ymax-2, label="Cycling") +
	annotate("rect", xmin = (intervals %>% slice(5))$start_dec, xmax = (intervals %>% slice(5))$end_dec, ymin = ymin, ymax = ymax,  alpha = .3) + 
	annotate("text", x= mean(c((intervals %>% slice(5))$start_dec, (intervals %>% slice(5))$end_dec)), y=ymax-5, label="Lekenpraatje") +
	annotate("rect", xmin = (intervals %>% slice(6))$start_dec, xmax = (intervals %>% slice(6))$end_dec, ymin = ymin, ymax = ymax,  alpha = .1) + 
	annotate("text", x= mean(c((intervals %>% slice(6))$start_dec, (intervals %>% slice(6))$end_dec)), y=ymax-8, label="Questions") +
	annotate("rect", xmin = (intervals %>% slice(7))$start_dec, xmax = (intervals %>% slice(7))$end_dec, ymin = ymin, ymax = ymax,  alpha = .1) + 
	annotate("text", x= mean(c((intervals %>% slice(7))$start_dec, (intervals %>% slice(7))$end_dec)), y=ymax-2, label="Zweetkamer") +
	geom_point(size=0.5, alpha=0.3)

ggsave('hr.png', width=16, height=6)

ymin = 20
ymax = 40

temp %>% 
	ggplot(aes(x=time_dec, y=`temperature celsius`)) +
	labs(x="Time (UTC)", y="Temperature (°C)") +
	annotate("rect", xmin = (intervals %>% slice(1))$start_dec, xmax = (intervals %>% slice(1))$end_dec, ymin = ymin, ymax = ymax,  alpha = .2) + 
	annotate("text", x= mean(c((intervals %>% slice(1))$start_dec, (intervals %>% slice(1))$end_dec)), y=ymax-1, label="Sleep") +
	annotate("rect", xmin = (intervals %>% slice(2))$start_dec, xmax = (intervals %>% slice(2))$end_dec, ymin = ymin, ymax = ymax,  alpha = .2) + 
	annotate("text", x= mean(c((intervals %>% slice(2))$start_dec, (intervals %>% slice(2))$end_dec)), y=ymax-1, label="Sleep") +
	annotate("rect", xmin = (intervals %>% slice(3))$start_dec, xmax = (intervals %>% slice(3))$end_dec, ymin = ymin, ymax = ymax,  alpha = .2) + 
	annotate("text", x= mean(c((intervals %>% slice(3))$start_dec, (intervals %>% slice(3))$end_dec)), y=ymax-1, label="Cycling") +
	annotate("rect", xmin = (intervals %>% slice(5))$start_dec, xmax = (intervals %>% slice(5))$end_dec, ymin = ymin, ymax = ymax,  alpha = .3) + 
	annotate("text", x= mean(c((intervals %>% slice(5))$start_dec, (intervals %>% slice(5))$end_dec)), y=ymax-2, label="Lekenpraatje") +
	annotate("rect", xmin = (intervals %>% slice(6))$start_dec, xmax = (intervals %>% slice(6))$end_dec, ymin = ymin, ymax = ymax,  alpha = .1) + 
	annotate("text", x= mean(c((intervals %>% slice(6))$start_dec, (intervals %>% slice(6))$end_dec)), y=ymax-3, label="Questions") +
	annotate("rect", xmin = (intervals %>% slice(7))$start_dec, xmax = (intervals %>% slice(7))$end_dec, ymin = ymin, ymax = ymax,  alpha = .1) + 
	annotate("text", x= mean(c((intervals %>% slice(7))$start_dec, (intervals %>% slice(7))$end_dec)), y=ymax-1, label="Zweetkamer") +
	geom_point(size=0.5, alpha=0.3)

ggsave('temp.png', width=16, height=6)
