-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot-Seasonality-aggregated.Rmd
259 lines (198 loc) · 9.82 KB
/
plot-Seasonality-aggregated.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
---
title: "Seasonality-analysis"
author: "Abel Serrano Juste"
date: "`r Sys.Date()`"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Import packages:
```{r}
library('glue')
library('patchwork')
source('lib-ts-analysis.R')
```
Set global vars
```{r}
### DEFINE GLOBAL VARS ###
PATH = dirname(rstudioapi::getSourceEditorContext()$path)
print(PATH)
setwd(PATH)
```
Define periods
```{r}
## STUDY PERIOD ##
ts_start2022 <- "2022-04-01 09:00:00" # from first of April
ts_end2022 <-"2022-11-30 23:45:00" # to 30 November
ts_start2023 <- "2023-04-01 09:00:00" # from first of April
ts_end2023 <-"2023-11-30 23:45:00" # to 30 November
## DRY SEASON PERIOD ##
start_dry2022 <- "2022-06-01 09:00:00"
end_dry2022 <- "2022-08-31 23:45:00"
start_dry2023 <- "2023-06-01 09:00:00"
end_dry2023 <- "2023-08-31 23:45:00"
```
# Load data
Load seasonality and microclimate data
```{r}
db <- read.csv("cooked-data/seasonality-clim-proc-allsites.csv")
db.season <- db %>% mutate_at (vars(site, class, series), \(x) {as.factor(x)}) %>% mutate(date = as.Date(date), ts = as_datetime(ts, tz = 'Europe/Madrid')) %>% select(series, ts, seasonality, class, site)
View(db.season)
db.temp <- read.csv("output-data/clim-allsites.csv")
db.temp <- db.temp %>% select(ts, site, surface.temp) %>% mutate(ts = as_datetime(ts, tz = 'Europe/Madrid'), timeOfDay = as_hms(ts))
dim(db.temp)
remove(db)
```
# Filter data by study period
```{r}
data2022 <- db.season[which(db.season$ts>=ts_start2022 & db.season$ts<=ts_end2022),]
data2023 <- db.season[which(db.season$ts>=ts_start2023 & db.season$ts<=ts_end2023),]
temp.data2022 <- db.temp[which(db.temp$ts>=ts_start2022 & db.temp$ts<=ts_end2022),]
temp.data2023 <- db.temp[which(db.temp$ts>=ts_start2023 & db.temp$ts<=ts_end2023),]
```
# Final plots - 2022
For each site, plots of all classes aggregated in one day plus temperature for four periods: full study period 2022, dry season 2022, full study period 2023 and dry season 2023.
All study period (from Spring (April) to Autumn (November))
```{r}
i = 1
plots = list()
for (PLACE in c('Penaflor', 'Miedes', 'Corbalan')) {
season.data <- data2022 %>% filter(site == PLACE) %>% mutate(timeOfDay = as_hms(ts)) %>% summarise(meanSeasonalityTime = mean(seasonality), SE_SeasonalityTime = sd(seasonality)/sqrt(n()), .by = c(class, timeOfDay))
temp.data <- temp.data2022 %>% filter(site == PLACE) %>% summarise(temp = mean(surface.temp, na.rm = T), .by = timeOfDay)
# set_legend_sample_number( data2022 %>% filter(site == PLACE) )
plots[[i]] = plot_day_seasonality_byclass_and_temp(season.data, temp.data, glue("{PLACE}"))
ggsave(filename = glue('output/{PLACE}_aggoneday-allperiod-seasonalities-byclass-2022.png'),
plot = plots[[i]],
width = 15, height = 10)
i = i + 1;
}
plots_study2022 <- wrap_plots(plots) +
plot_annotation(title = "All study period - 2022", tag_levels = 'A') +
plot_layout(nrow = 1, guides = "collect") &
theme(legend.position='bottom')
plots_study2022
# ggsave(filename = glue('output/allplaces_aggoneday-allperiod-seasonalities-byclass-2022.png'), width = 18, height = 14)
```
June to August (dry season) - 2022:
```{r}
data.dry <- data2022 %>% filter(ts >= start_dry2022 & ts <= end_dry2022)
temp.dry <- temp.data2022 %>% filter(ts >= start_dry2022 & ts <= end_dry2022)
i = 1
plots = list()
for (PLACE in c('Penaflor', 'Miedes', 'Corbalan')) {
season.data <- data.dry %>% filter(site == PLACE) %>% mutate(timeOfDay = as_hms(ts)) %>% summarise(meanSeasonalityTime = mean(seasonality), SE_SeasonalityTime = sd(seasonality)/sqrt(n()), .by = c(class, timeOfDay))
temp.data <- temp.dry %>% filter(site == PLACE) %>% summarise(temp = mean(surface.temp, na.rm = T), .by = timeOfDay)
# set_legend_labels(data.dry %>% filter(site == PLACE))
plots[[i]] = plot_day_seasonality_byclass_and_temp(season.data, temp.data, glue("{PLACE}"))
ggsave(filename = glue('output/{PLACE}_aggoneday-dryperiod-seasonalities-byclass-2022.png'),
plot = plots[[i]],
width = 15, height = 10)
i = i + 1;
}
plots_dry2022 <- wrap_plots(plots) +
plot_annotation(title = "Dry period - 2022", tag_levels = list(c("D", "E", "F")) ) +
plot_layout(nrow = 1, guides = "collect") &
theme(legend.position='bottom')
# grid.arrange(grobs = plots, nrow = 1, top = "All study period - 2022")
# ggarrange(plotlist = plots, nrow = 1, main = "All study period - 2022", common.legend = T, legend="bottom", labels="auto")
```
```{r}
plots_study2022 <- plots_study2022 & theme(legend.position="none")
(wrap_elements(plots_study2022) / wrap_elements(plots_dry2022))
ggsave(glue('output/allplaces_aggoneday-seasonalities-byclass-2022.png'), width = 18, height = 14)
```
# Final plots - 2023
All study period (from Spring (April) to Autumn (November)) - 2023
```{r}
i = 1
plots = list()
for (PLACE in c('Penaflor', 'Miedes', 'Corbalan')) {
season.data <- data2023 %>% filter(site == PLACE) %>% mutate(timeOfDay = as_hms(ts)) %>% summarise(meanSeasonalityTime = mean(seasonality), SE_SeasonalityTime = sd(seasonality)/sqrt(n()), .by = c(class, timeOfDay))
temp.data <- temp.data2023 %>% filter(site == PLACE) %>% summarise(temp = mean(surface.temp, na.rm = T), .by = timeOfDay)
# set_legend_labels(data2023 %>% filter(site == PLACE))
plots[[i]] = plot_day_seasonality_byclass_and_temp(season.data, temp.data, glue("{PLACE}"))
ggsave(filename = glue('output/{PLACE}_aggoneday-allperiod-seasonalities-byclass-2023.png'),
plot = plots[[i]],
width = 15, height = 10)
i = i + 1;
}
plots_study2023 <- wrap_plots(plots) +
plot_annotation(title = "All study period - 2023", tag_levels = 'A') +
plot_layout(nrow = 1, guides = "collect") &
theme(legend.position='none')
plots_study2023
ggsave(filename = glue('output/allplaces_aggoneday-allperiod-seasonalities-byclass-2023.png'), width = 45, height = 10)
```
June to August (dry season) - 2023:
```{r}
data.dry <- data2023 %>% filter(ts >= start_dry2023 & ts <= end_dry2023)
temp.dry <- temp.data2023 %>% filter(ts >= start_dry2023 & ts <= end_dry2023)
i = 1
plots = list()
for (PLACE in c('Penaflor', 'Miedes', 'Corbalan')) {
season.data <- data.dry %>% filter(site == PLACE) %>% mutate(timeOfDay = as_hms(ts)) %>% summarise(meanSeasonalityTime = mean(seasonality), SE_SeasonalityTime = sd(seasonality)/sqrt(n()), .by = c(class, timeOfDay))
temp.data <- temp.dry %>% filter(site == PLACE) %>% summarise(temp = mean(surface.temp, na.rm = T), .by = timeOfDay)
# set_legend_labels(data.dry %>% filter(site == PLACE))
plots[[i]] = plot_day_seasonality_byclass_and_temp(season.data, temp.data, glue("{PLACE}"))
ggsave(filename = glue('output/{PLACE}_aggoneday-dryperiod-seasonalities-byclass-2023.png'),
plot = plots[[i]],
width = 15, height = 10)
i = i + 1;
}
plots_dry2023 <- wrap_plots(plots) +
plot_annotation(title = "Dry period - 2023", tag_levels = list(c("D", "E", "F")) ) +
plot_layout(nrow = 1, guides = "collect") &
theme(legend.position='bottom')
```
```{r}
wrap_elements(plots_study2023) / wrap_elements(plots_dry2023)
ggsave(glue('output/allplaces_aggoneday-seasonalities-byclass-2023.png'), width = 18, height = 14)
```
# Plots average 2022 & 2023
Study period:
```{r}
i = 1
data.2022_2023 <- rbind.data.frame(data2022, data2023)
temp.data2022_2023 <- rbind.data.frame(temp.data2022, temp.data2023)
plots = list()
for (PLACE in c('Penaflor', 'Miedes', 'Corbalan')) {
season.data <- data.2022_2023 %>% filter(site == PLACE) %>% mutate(timeOfDay = as_hms(ts)) %>% summarise(meanSeasonalityTime = mean(seasonality), SE_SeasonalityTime = sd(seasonality)/sqrt(n()), .by = c(class, timeOfDay))
temp.data <- temp.data2022 %>% filter(site == PLACE) %>% summarise(temp = mean(surface.temp, na.rm = T), .by = timeOfDay)
plots[[i]] = plot_day_seasonality_byclass_and_temp(season.data, temp.data, glue("{PLACE}"))
i = i + 1;
}
plots_study_2022_2023 <- wrap_plots(plots) +
# plot_annotation(title = "All study period - 2022 & 2023", tag_levels = 'A') +
plot_annotation(tag_levels = 'A') +
plot_layout(nrow = 3, ncol = 1, byrow = T, guides = "collect", axes = "collect_x") &
theme(legend.position='bottom', legend.direction = "vertical",
plot.title = element_text(size = 20, hjust = 0.5, face = "bold"),
text=element_text(size=20, color = "gray9"),
legend.title=element_text(size=16, color = "gray9"),
legend.text=element_text(size=16, color = "gray9"),
axis.title=element_text(size=20),
axis.text=element_text(size=20, color = "gray9")
)
plots_study_2022_2023
ggsave(glue('output/allplaces_aggoneday-studyperiod-seasonalities-byclass-2022&2023.png'), width = 7, height = 15)
```
Dry period:
```{r}
i = 1
data.2022_2023 <- rbind.data.frame(data2022 %>% filter(ts >= start_dry2022 & ts <= end_dry2022), data2023 %>% filter(ts >= start_dry2023 & ts <= end_dry2023))
temp.data2022_2023 <- rbind.data.frame(temp.data2022 %>% filter(ts >= start_dry2022 & ts <= end_dry2022), temp.data2023 %>% filter(ts >= start_dry2023 & ts <= end_dry2023))
plots = list()
for (PLACE in c('Penaflor', 'Miedes', 'Corbalan')) {
season.data <- data.2022_2023 %>% filter(site == PLACE) %>% mutate(timeOfDay = as_hms(ts)) %>% summarise(meanSeasonalityTime = mean(seasonality), SE_SeasonalityTime = sd(seasonality)/sqrt(n()), .by = c(class, timeOfDay))
temp.data <- temp.data2022 %>% filter(site == PLACE) %>% summarise(temp = mean(surface.temp, na.rm = T), .by = timeOfDay)
plots[[i]] = plot_day_seasonality_byclass_and_temp(season.data, temp.data, glue("{PLACE}"))
i = i + 1;
}
plots_study_2022_2023 <- wrap_plots(plots) +
plot_annotation(title = "Dry periods - 2022 & 2023", tag_levels = 'A') +
plot_layout(nrow = 1, ncol = 3, byrow = F, guides = "collect") &
theme(legend.position='bottom', legend.direction = "horizontal")
plots_study_2022_2023
ggsave(glue('output/allplaces_aggoneday-dryperiod-seasonalities-byclass-2022&2023.png'), width = 15, height = 7)
```