-
Notifications
You must be signed in to change notification settings - Fork 0
/
Monthly_Ranges.R
331 lines (308 loc) · 12.6 KB
/
Monthly_Ranges.R
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# INSTALL AND ACTIVATE DEPENDENCIES
# install.packages("XYZ")
library(Hmisc)
library(corrplot)
library(dplyr)
library(ggpubr)
library(cowplot)
library(dplyr)
library(timetk)
require(scales)
# SET SYSTEM LOCALE TO ENGLISH TO ENSURE ENGLISH AXIS LABELS
Sys.setenv("LANGUAGE"="En")
Sys.setlocale("LC_ALL","English")
# LOAD DATAFRAME WITH ALL DATA AND MICROCLIMATE MEASUREMENTS
model_df <- read.csv("FILEPATH/model_df_2_1.csv")
model_df$hourofday <- as.POSIXct(model_df$hourofday)
# SUMMARIZE FOR EACH DAY
daily_range <- model_df %>%
group_by(las_name) %>%
summarise_by_time(
.date_var = hourofday,
.by = "day",
# Summarization
t_soil_min = min(t_soil),
t_soil_max = max(t_soil),
#t_soil_range = range(t_soil),
#t_surface_range = range(t_surface),
t_air_min = min(t_air),
t_air_max = max(t_air),
#t_air_range = range(t_air)
)
# CALCULATE DAILY TEMPERATURE RANGES
daily_range$t_soil_range = (daily_range$t_soil_max - daily_range$t_soil_min)
daily_range$t_air_range = (daily_range$t_air_max - daily_range$t_air_min)
# SUMMARIZE FOR EACH MONTH
monthly_range <- daily_range %>%
group_by(las_name) %>%
summarise_by_time(
.date_var = hourofday,
.by = "month",
# Summarization
air_range_mean = mean(t_air_range),
air_range_min = min(t_air_range),
air_range_max = max(t_air_range),
#t_soil_range = range(t_soil),
#t_surface_range = range(t_surface),
sor_range_mean = mean(t_soil_range),
sor_range_min = min(t_soil_range),
sor_range_max = max(t_soil_range),
#t_air_range = range(t_air)
)
model_df <- model_df[,-c(2:7)]
# MELT DATAFRAME UNIQUE COLUMNS TO GET THE STRUCTURAL METRICS
unique_model_df <- unique(model_df)
unique_model_df <- unique_model_df[-14,]
# JOIN WITH MONTHLY RANGE TO GET A DF WITH MONTHLY RANGES + STRUCTURAL METRICS
monthly_range_join <- left_join(monthly_range, unique_model_df, by = "las_name")
monthly_range_join$hourofday <- as.Date(monthly_range_join$hourofday)
monthly_range_join$FHD2_mean <- as.numeric(monthly_range_join$FHD2_mean)
# MEAN MONTHLY RANGE + FHD
air_mean <- ggplot(monthly_range_join, aes(x=hourofday, y=air_range_mean, color = FHD_mean, group = factor(las_name)))+
geom_path()+
geom_point(shape = 20, size = 2)+
theme_bw()+
scale_colour_gradient("Mean FHD",
low = "purple",
high = "yellow",
space = "Lab",
na.value = "grey50",
guide = "colourbar",
aesthetics = "colour"
)+
theme(axis.text = element_text(face="bold"),
legend.position = "bottom",
axis.text.x = element_text(angle = 45, hjust = 1),
panel.grid.major = element_line(size = 1),
axis.title.y=element_text(size=9))+
labs(x = "", y = "Mean monthly range (°C)", fill = "Mean FHD")+
geom_rect(data=monthly_range_join[1,],
aes(xmin=as.Date("2019-05-31"), xmax=as.Date("2019-07-10"), ymin=-Inf,ymax=Inf),
alpha=0.3,
fill="grey",
color = NA)+
annotate("text", x=as.Date("2019-07-15"), y=31, size = 4, label="(*)", alpha = 0.3,
hjust = 2.5, vjust = -2)+
scale_x_date(labels = date_format("%b"),
breaks = date_breaks("month"),
limits = c(as.Date("2019-5-28"), as.Date("2020-02-02")))+
ylim(0,40)
# GET LEGEND FOR GGARRANGE
legend <- get_legend(air_mean)
# MEAN MONTHLY RANGE + FHD, THIS TIME WITHOUT LEGEND SO ITS NOT DOUBLED IN THE
# FINAL PLOT
air_mean <- ggplot(monthly_range_join, aes(x=hourofday, y=air_range_mean, color = FHD_mean, group = factor(las_name)))+
geom_path()+
geom_point(shape = 20, size = 2)+
theme_bw()+
scale_colour_gradient("Mean FHD",
low = "purple",
high = "yellow",
space = "Lab",
na.value = "grey50",
guide = "colourbar",
aesthetics = "colour"
)+
theme(axis.text = element_text(face="bold"),
legend.position = "none",
axis.text.x = element_text(angle = 45, hjust = 1),
panel.grid.major = element_line(size = 1),
axis.title.y=element_text(size=9))+
labs(x = "", y = "Mean monthly range (°C)", fill = "Mean FHD")+
geom_rect(data=monthly_range_join[1,],
aes(xmin=as.Date("2019-05-31"), xmax=as.Date("2019-07-10"), ymin=-Inf,ymax=Inf),
alpha=0.3,
fill="grey",
color = NA)+
annotate("text", x=as.Date("2019-07-15"), y=31, size = 4, label="(*)", alpha = 0.3,
hjust = 2.5, vjust = -2)+
scale_x_date(labels = date_format("%b"),
breaks = date_breaks("month"),
limits = c(as.Date("2019-5-28"), as.Date("2020-02-02")))+
ylim(0,40)
# MAX MONTHLY RANGE + FHD
air_max <- ggplot(monthly_range_join, aes(x=hourofday, y=air_range_max, color = FHD_mean, group = factor(las_name)))+
geom_path()+
geom_point(shape = 20, size = 2)+
theme_bw()+
scale_colour_gradient("",
low = "purple",
high = "yellow",
space = "Lab",
na.value = "grey50",
guide = "colourbar",
aesthetics = "colour"
)+
theme(axis.text = element_text(face="bold"),
legend.position = "none",
axis.text.x = element_text(angle = 45, hjust = 1),
panel.grid.major = element_line(size = 1),
axis.title.y=element_text(size=9))+
labs(x = "", y = "Max. monthly range (°C)", fill = "Mean FHD")+
geom_rect(data=monthly_range_join[1,],
aes(xmin=as.Date("2019-05-31"), xmax=as.Date("2019-07-10"), ymin=-Inf,ymax=Inf),
alpha=0.3,
fill="grey",
color = NA)+
annotate("text", x=as.Date("2019-07-15"), y=31, size = 4, label="(*)", alpha = 0.3,
hjust = 2.5, vjust = -2)+
scale_x_date(labels = date_format("%b"),
breaks = date_breaks("month"),
limits = c(as.Date("2019-5-28"), as.Date("2020-02-02")))+
ylim(0,40)
# MIN MONTHLY RANGE + FHD
air_min <- ggplot(monthly_range_join, aes(x=hourofday, y=air_range_min, color = FHD_mean, group = factor(las_name)))+
geom_path()+
geom_point(shape = 20, size = 2)+
theme_bw()+
scale_colour_gradient("",
low = "purple",
high = "yellow",
space = "Lab",
na.value = "grey50",
guide = "colourbar",
aesthetics = "colour"
)+
theme(axis.text = element_text(face="bold"),
legend.position = "none",
axis.text.x = element_text(angle = 45, hjust = 1),
panel.grid.major = element_line(size = 1),
axis.title.y=element_text(size=9))+
labs(title = "Air Temperature", x = "", y = "Min. monthly range (°C)", fill = "Mean FHD")+
geom_rect(data=monthly_range_join[1,],
aes(xmin=as.Date("2019-05-31"), xmax=as.Date("2019-07-10"), ymin=-Inf,ymax=Inf),
alpha=0.3,
fill="grey",
color = NA)+
annotate("text", x=as.Date("2019-07-15"), y=31, size = 4, label="(*)", alpha = 0.3,
hjust = 2.5, vjust = -2)+
scale_x_date(labels = date_format("%b"),
breaks = date_breaks("month"),
limits = c(as.Date("2019-5-28"), as.Date("2020-02-02")))+
ylim(0,40)
# GGARRANGE LEFT COLUMN OF FINAL PLOT
fig1 <- ggarrange(air_min, air_mean, air_max,
labels = c("A", "B", "C"),
ncol = 1, nrow = 3,
align = "hv"
)
# SOIL MEAN MONTHLY RANGE + FHD
sor_mean <- ggplot(monthly_range_join, aes(x=hourofday, y=sor_range_mean, color = FHD_mean, group = factor(las_name)))+
geom_path()+
geom_point(shape = 20, size = 2)+
theme_bw()+
scale_colour_gradient("Mean FHD",
low = "purple",
high = "yellow",
space = "Lab",
na.value = "grey50",
guide = "colourbar",
aesthetics = "colour"
)+
theme(axis.text = element_text(face="bold"),
legend.position = "none",
axis.text.x = element_text(angle = 45, hjust = 1),
panel.grid.major = element_line(size = 1))+
labs(x = "", y = "", fill = "Mean FHD")+
geom_rect(data=monthly_range_join[1,],
aes(xmin=as.Date("2019-05-31"), xmax=as.Date("2019-07-10"), ymin=-Inf,ymax=Inf),
alpha=0.3,
fill="grey",
color = NA)+
annotate("text", x=as.Date("2019-07-15"), y=31, size = 4, label="(*)", alpha = 0.3,
hjust = 2.5, vjust = -2)+
scale_x_date(labels = date_format("%b"),
breaks = date_breaks("month"),
limits = c(as.Date("2019-5-28"), as.Date("2020-02-02")))+
ylim(0,40)
# SOIL MAX MONTHLY RANGE + FHD
sor_max <- ggplot(monthly_range_join, aes(x=hourofday, y=sor_range_max, color = FHD_mean, group = factor(las_name)))+
geom_path()+
geom_point(shape = 20, size = 2)+
theme_bw()+
scale_colour_gradient("",
low = "purple",
high = "yellow",
space = "Lab",
na.value = "grey50",
guide = "colourbar",
aesthetics = "colour"
)+
theme(axis.text = element_text(face="bold"),
legend.position = "none",
axis.text.x = element_text(angle = 45, hjust = 1),
panel.grid.major = element_line(size = 1),
axis.title.y=element_text(size=14))+
labs(x = "", y = "", fill = "Mean FHD")+
geom_rect(data=monthly_range_join[1,],
aes(xmin=as.Date("2019-05-31"), xmax=as.Date("2019-07-10"), ymin=-Inf,ymax=Inf),
alpha=0.3,
fill="grey",
color = NA)+
annotate("text", x=as.Date("2019-07-15"), y=31, size = 4, label="(*)", alpha = 0.3,
hjust = 2.5, vjust = -2)+
scale_x_date(labels = date_format("%b"),
breaks = date_breaks("month"),
limits = c(as.Date("2019-5-28"), as.Date("2020-02-02")))+
ylim(0,40)
# SOIL MIN MONTHLY RANGE + FHD
sor_min <- ggplot(monthly_range_join, aes(x=hourofday, y=sor_range_min, color = FHD_mean, group = factor(las_name)))+
geom_path()+
geom_point(shape = 20, size = 2)+
theme_bw()+
scale_colour_gradient("",
low = "purple",
high = "yellow",
space = "Lab",
na.value = "grey50",
guide = "colourbar",
aesthetics = "colour"
)+
theme(axis.text = element_text(face="bold"),
legend.position = "none",
axis.text.x = element_text(angle = 45, hjust = 1),
panel.grid.major = element_line(size = 1))+
labs(title = "Soil Temperature", x = "", y = "", fill = "Mean FHD")+
geom_rect(data=monthly_range_join[1,],
aes(xmin=as.Date("2019-05-31"), xmax=as.Date("2019-07-10"), ymin=-Inf,ymax=Inf),
alpha=0.3,
fill="grey",
color = NA)+
annotate("text", x=as.Date("2019-07-15"), y=31, size = 4, label="(*)", alpha = 0.3,
hjust = 2.5, vjust = -2)+
scale_x_date(labels = date_format("%b"),
breaks = date_breaks("month"),
limits = c(as.Date("2019-5-28"), as.Date("2020-02-02")))+
ylim(0,40)
# GGARRANGE RIGHT COLUMN OF FINAL PLOT
fig2 <- ggarrange(sor_min, sor_mean, sor_max,
labels = c("D", "E", "F"),
ncol = 1, nrow = 3,
align = "hv"
)
# GGARRANGE FINAL PLOT WITH COMMON LEGEND
figure <- ggarrange(fig1, fig2,
ncol = 2, nrow = 1,
align = "hv",
common.legend = T,
legend.grob = legend,
legend = "bottom"
)
figure
# ANNOTATE
annotate_figure(
figure,
#top = text_grob("", color = "black", face = "bold", size = 14),
bottom = text_grob("(*) 13.06.2019 - 10.07.2019, study period: Aalto et al. (2022)", color = "black",
hjust = 1, x = 1, size = 10)
#left = text_grob("Daily temperature range (°C)",
#color = "black", rot = 90),
#right = text_grob("Contains modified Copernicus Climate Change Service Information [2019]",
#size = 8, rot = 270, face = "italic")
# fig.lab = "Figure 1", fig.lab.face = "bold"
)
# SAVE FIGURE
ggsave("FILEPATH/monthly_ranges.pdf",
width = 20,
height = 23,
units = "cm")