---
title: "data analysis and plotting"
output: html_notebook
---

1. loading packages
```{r}
library(plyr)
library(ggplot2)
library(gganimate)
library(gifski)
library(anytime)
library(scales)
library(png)

```

2. loading data
```{r}
a0.a = read.csv("PPCa0.csv", header = T)
a0.b = read.csv("PPCb0.csv", header = T)
a0 = data.frame('DateTime' = a0.a$DateTime, 'Temp' = (a0.a$Temp + a0.b$Temp)/2, group = rep('air',577))

s3.a = read.csv("PPCa3.csv", header = T)
s3.b = read.csv("PPCb3.csv", header = T)
s3 = data.frame('DateTime' = s3.a$DateTime, 'Temp' = (s3.a$Temp + s3.b$Temp)/2, group = rep('sediment_3cm',577))

shelters.t = rbind.fill(a0, s3)

```

3. define time
```{r}
# define time
datetime =seq.POSIXt(as.POSIXct("2020-8-7 17:0:0"), as.POSIXct("2020-8-13 17:0:0"), by = "15 min", tz="Europe/Berlin")
datetime_posix = data.frame(rep(datetime,2))
shelters.t$DateTime = datetime_posix[,1]

```

4. loading inundation time
```{r}
rect_range = read.csv('tern_rect.csv', header = T)
rect_range$start = as.POSIXct(rect_range$start, tz = "Europe/Berlin", format = "%m/%d/%Y %H:%M")
rect_range$end = as.POSIXct(rect_range$end, tz = "Europe/Berlin", format = "%m/%d/%Y %H:%M")

```

5. making plots
```{r}
filter1 = shelters.t$DateTime <= as.POSIXct("2020-08-13 17:00:00")
shelters.t = shelters.t[filter1, ]

filter2 = rect_range$start <= as.POSIXct("2020-08-13 17:00:00")
rect_range = rect_range[filter2, ]

p.shelters = ggplot(shelters.t, aes(DateTime, Temp, group = group)) + 
  geom_rect(data = rect_range, mapping=aes(xmin = start,xmax = end,ymin = -Inf, ymax = Inf), 
            fill = "#3399FF", color = 'NA', alpha = 0.2,inherit.aes = FALSE) +
  geom_line(aes(color=group),size = 1.2) + 
  labs(x = "Date and time", y = expression(bold('Temperature'*~degree*C)))+
  theme(plot.margin = margin(1, 30, 1, 0.5))+
  theme(legend.position="bottom")+
  scale_y_continuous(limits = c(15, 40)) +
  scale_x_datetime(labels = date_format("%m/%d/%y"), 
                   breaks = date_breaks("1 day")) +
  scale_color_manual(name="Measurements", 
                     breaks=c('air', 'sediment_3cm'),
                     values=c("#FF0033", "#996633"),
                     labels = c('Air temperature', 'Sediment temperature (3 cm depth)'))+
  theme(
    plot.title = element_text(color = "blue", size = 13, face = "bold", hjust = 0.5),
    panel.background = element_blank(),
    panel.border = element_rect(color = "black", fill=NA, size=2),
    legend.position = 'bottom', 
    legend.title = element_text(size=11, face="bold", color = "black"), 
    legend.text = element_text(size=10, face="bold"),
    axis.title.x = element_text(size = 12, face="bold"),
    axis.title.y = element_text(size = 12, face="bold"),
    axis.text.x = element_text(size = 10, face="bold"),
    axis.text.y = element_text(size = 10, face="bold"))+
  guides(color = guide_legend(override.aes = list(size = 1.5)))

p.shelters

ggsave("pp_shelters.png", p.shelters, units = 'in', width = 10, height = 4)

```
