-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot-proc-dendro-Corbalan.Rmd
323 lines (261 loc) · 9.41 KB
/
plot-proc-dendro-Corbalan.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
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
```{r}
source("lib-dendro.R")
library(ggplot2)
library(dplyr)
library(lubridate)
### DEFINE GLOBAL VARS ###
PATH = '/home/akronix/workspace/dendro';
setwd(PATH)
DATA_DIR = 'processed/Corbalan-processed'
ENV_DIR = 'processed/Corbalan-env-processed'
SELECTED_TMS <- "94231943"
### IMPORT DENDRO DATA ###
# importing dendro data #
list_files <- list.files(file.path(".",DATA_DIR), pattern="*.csv$", full.names=TRUE)
db<-read.all.processed(list_files)
# Set initial and final date for analysis
# ts_start<-"2022-04-01 09:00:00" # 2 days after installation
# ts_end<-"2023-09-27 08:00:00" # last timestamp of downloaded data
# db <- reset.initial.values(db, ts_start, ts_end)
### CLEAN & PREPARE DATA ###
# INSPECT DATA
str(db)
head(db)
tail(db)
```
```{r}
## PLOT ALL DENDROS ##
plot_multiple_dendro <- function (data, title, y = data$value) {
ggplot(data = data, mapping = aes(x=ts, y=y, col=series))+
geom_line( )+
# ggtitle(paste0("Dendro data for sensor series: ",db$series[1], " - ", db$sp[1])) +
labs(x=expression('Date'),
y=expression( Delta*"D (um)") ) +
geom_hline(yintercept=0,lty=2,linewidth=0.2)+
scale_x_datetime(date_breaks = "1 month", date_labels = "%m-%y") +
ggtitle(title) +
theme_bw() +
theme(axis.text.x = element_text(angle = 30, hjust=1))
}
```
```{r}
plot_multiple_dendro(db, "Plot by dendrometer")
ggsave('output/all-dendrometers-Corbalan.png', width = 15, height = 10)
```
# Normalization 0-1 to better compare dendros
```{r}
normalized.db <- db %>%
select (series, ts, value) %>%
group_by(series) %>%
mutate( normalized_value = normalize.0_1(value), .keep = 'all' )
```
```{r}
plot_multiple_dendro(normalized.db, "Dendros data normalized to [0-1]", y = normalized.db$normalized_value)
ggsave('output/normalized-all-dendrometers-Corbalan.png', width = 15, height = 10)
```
# Mean + strip region of SE
```{r}
dbagg = db %>% group_by(ts) %>% dplyr::summarise(mean = mean(value, na.rm = T), sd = sd(value, na.rm = T) )
#library(Rmisc) # for summarySE()
#dbagg <- summarySE(db, measurevar = "value", groupvars = c("ts"), na.rm = TRUE)
head(dbagg)
tail(dbagg)
```
```{r}
plot_cat_full_year<-
ggplot(data = dbagg, aes(x=ts, y=mean)) +
ggtitle("Mean of data for Corbalan dendrometers with ± standard deviation strips") +
geom_ribbon(aes(ymin=mean-sd, ymax=mean+sd), fill='lightgreen', alpha=0.3, show.legend = FALSE, linetype = 0) +
geom_line( aes (linetype = ""), col='darkgreen', show.legend = F) +
labs(x=expression(''),
y=expression(Delta*" D (um)"))+
theme_bw() +
geom_hline(yintercept=0,lty=2,linewidth=0.2)+
#facet_grid(class~.,scales = "free_y")+
scale_x_datetime(date_breaks = "1 month", date_labels="%b %Y") +
theme(axis.text.x = element_text(angle = 30, hjust=1))
plot_cat_full_year
ggsave('output/mean-Corbalan.png', width = 15, height = 10)
```
# By species
```{r}
TreeList<-read.table("TreeList.txt",header=T)
Qi = TreeList %>% filter(class == "Quercus") %>% pull(series)
P_D = TreeList %>% filter(class == "D") %>% pull(series)
P_ND = TreeList %>% filter(class == "ND") %>% pull(series)
db.Qi = db %>% filter (series %in% Qi)
db.D = db %>% filter (series %in% P_D)
db.ND = db %>% filter (series %in% P_ND)
```
```{r}
library(ggnewscale)
# Define color scales for each class
green_colors <- c("lightgreen", "green", "darkgreen", "darkolivegreen", "olivedrab", "springgreen")
blue_colors <- c("blue", "cyan", "aquamarine", "cadetblue", "dodgerblue", "darkblue")
orange_colors <- c("orange", "orangered", "tomato", "darkorange", "coral", "tan")
ggplot() +
ggtitle("Dendrometers painted by color scale according to its class") +
geom_line(data = db.D, aes(x = ts, y = value, col = series)) +
scale_color_manual(values = orange_colors, name = "Declining trees") +
new_scale_color() +
geom_line(data = db.ND, aes(x = ts, y = value, col = series), ) +
scale_color_manual(values = blue_colors, name = "Non-Declining trees") +
new_scale_color() +
geom_line(data = db.Qi, aes(x = ts, y = value, col = series)) +
scale_color_manual(values = green_colors, name = "Quercus Ilex") +
geom_hline(yintercept=0,lty=2,linewidth=0.2) +
labs(x=expression(''),
y=expression(Delta*" D (um)"))+
theme_bw() +
scale_x_datetime(date_breaks = "1 month", date_labels="%b %Y") +
theme(axis.text.x = element_text(angle = 30, hjust=1))
ggsave('output/all-dendrometers-by-class-Corbalan.png', width = 15, height = 10)
```
```{r}
# Dataframe with all data and a new column "class"
# db.by.class = db %>% select(series, ts, value) %>% mutate(class = case_when(
# series %in% Qi ~ "Quercus",
# series %in% P_D ~ "Declining",
# series %in% P_ND ~ "Non-Declining"
# )
# )
```
One plot of dendros by class:
```{r}
plot_multiple_dendro(db.Qi, "Quercus Ilex - Corbalan", y = db.Qi$value)
ggsave('output/Qi-Corbalan.png', width = 15, height = 10)
```
```{r}
plot_multiple_dendro(db.ND, "Non-Declining - Corbalan", y = db.ND$value)
ggsave('output/ND-Corbalan.png', width = 15, height = 10)
```
```{r}
plot_multiple_dendro(db.D, "Declining - Corbalan", y = db.D$value)
ggsave('output/D-Corbalan.png', width = 15, height = 10)
```
```{r}
db.Qi.agg <- db.Qi %>% group_by(ts) %>% dplyr::summarise(mean = mean(value, na.rm = T), sd = sd(value, na.rm = T) )
head(db.Qi.agg)
tail(db.Qi.agg)
```
```{r}
#library(mdthemes)
plot_mean_sd <- function (data, species, output_fn = FALSE) {
plot <-
ggplot(data = data, aes(x=ts, y=mean)) +
ggtitle(paste0("Mean of data for ", species, " with ± standard deviation strips")) +
geom_ribbon(aes(ymin=mean-sd, ymax=mean+sd), fill='lightgreen', alpha=0.3, show.legend = FALSE, linetype = 0) +
geom_line( aes (linetype = species), col='darkgreen', show.legend = T) +
labs(x=expression(''),
y=expression(Delta*" D (um)")) +
theme_bw() +
#md_theme_bw()+
geom_hline(yintercept=0,lty=2,linewidth=0.2) +
#facet_grid(class~.,scales = "free_y")+
scale_x_datetime(date_breaks = "1 month", date_labels="%b %Y") +
theme(axis.text.x = element_text(angle = 30, hjust=1))
plot(plot)
if (output_fn != F){
ggsave(output_fn, width = 15, height = 10)
}
}
```
```{r}
plot_mean_sd(db.Qi.agg, "Quercus Ilex", output_fn = "output/mean-Qi-Corbalan.png")
```
```{r}
db.D.agg <- db.D %>% group_by(ts) %>% dplyr::summarise(mean = mean(value, na.rm = T), sd = sd(value, na.rm = T) )
head(db.D.agg)
tail(db.D.agg)
```
```{r}
plot_mean_sd(db.D.agg, "Declining Pinus", output_fn = "output/mean-declining-Corbalan.png" )
```
```{r}
db.ND.agg <- db.ND %>% group_by(ts) %>% dplyr::summarise(mean = mean(value, na.rm = T), sd = sd(value, na.rm = T) )
head(db.ND.agg)
tail(db.ND.agg)
```
```{r}
plot_mean_sd(db.ND.agg, "Non-Declining Pinus", output_fn = "output/mean-ND-Corbalan.png" )
```
Join all aggregated df in one
```{r}
db.Qi.agg$class = factor("Quercus Ilex")
db.D.agg$class = factor("Declining Pinus")
db.ND.agg$class = factor("Non-Declining Pinus")
db.by.class = rbind.data.frame(db.Qi.agg, db.D.agg, db.ND.agg)
str(db.by.class)
```
Plot mean by class in one plot
```{r}
plot_multiple_dendro_by_class <-
ggplot(data = db.by.class, mapping = aes(x=ts, y=mean, col = class))+
geom_line( ) +
# ggtitle(paste0("Dendro data for sensor series: ",db$series[1], " - ", db$sp[1])) +
labs(x=expression('Date'),
y=expression( Delta*"D (um)") ) +
geom_hline(yintercept=0,lty=2,linewidth=0.2)+
scale_x_datetime(date_breaks = "1 month", date_labels = "%m-%y") +
ggtitle("Means by class") +
theme_bw() +
theme(axis.text.x = element_text(angle = 30, hjust=1))
plot_multiple_dendro_by_class
ggsave('output/mean-byclass-Corbalan.png', width = 15, height = 10)
```
```{r}
# library(plotly)
# plot_ly(
# data = db.by.class,
# x = ~ts,
# y = ~mean,
# type = "scatter",
# mode = "lines",
# color = ~class)
# + colors = c("blue")
```
# Environmental data from TMS sensors
```{r}
# importing processed environmental data #
list_files <- list.files(file.path(PATH,ENV_DIR), pattern="*.csv$", full.names=TRUE)
db.env<-read.all.env.processed(list_files)
str(db.env)
```
Now calculate the mean for vwc and temp:
```{r}
db.env.agg = db.env %>% group_by(ts) %>% dplyr::summarise(temp = mean(surface.temp, na.rm = T), vwc = mean(vwc, na.rm = T))
db.env.agg
```
Let's keep environmental data from only one dendro whose data is all valid:
```{r}
db.env <- db.env[db.env$series == SELECTED_TMS,]
```
And we plot it:
```{r}
plot_temp_and_humidity <-
ggplot(data = db.env, aes(x=ts, y=surface.temp)) +
ggtitle("Temperature and humidity for Corbalan (2022-2023)") +
geom_line(aes(colour = surface.temp)) +
theme_bw() +
scale_colour_gradient(low = "light blue", high = "red") +
scale_x_datetime(date_breaks = "1 month", date_labels="%b %Y") +
theme(axis.text.x = element_text(angle = 30, hjust=1)) +
scale_y_continuous("Temperature (ºC)", breaks=seq(0,50,10), sec.axis = dup_axis(name = "Volumetric Water Content (%)" ))+
geom_line(aes(x = ts, y = vwc * 100, linetype = "Volumetric Water Content (%)"), col="blue", show.legend = TRUE) +
scale_linetype_manual(NULL, values = 1) +
labs(x=expression(''), colour = "Temperature (ºC)")
plot_temp_and_humidity
ggsave('output/VWCytempAgg-Corbalan.png', width = 15, height = 10)
```
```{r}
# library(plotly)
# plot_ly(
# data = db.env,
# x = ~ts,
# y = ~vwc*100,
# type = "scatter",
# mode = "lines",
# # color = ~state,
# colors = c("blue")
# )
```