-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot-proc-dendro-PacoEzpela.Rmd
222 lines (177 loc) · 7.54 KB
/
plot-proc-dendro-PacoEzpela.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
---
output:
pdf_document: default
html_document: default
---
```{r include=FALSE}
source("lib-dendro.R")
library(ggplot2)
theme_set(theme_bw())
library(ggnewscale)
library(dplyr)
library(lubridate)
library(glue)
### DEFINE GLOBAL VARS ###
PLACE = 'PacoEzpela'
DATA_DIR = glue('processed/{PLACE}-processed')
ENV_DIR = glue('processed/{PLACE}-env-processed')
SELECTED_TMS <- 94252896
Fagus <- c(92223484, 92232411, 92232420, 92232415, 92232419)
Non_Declining_Abies <- c(92222339, 92232424, 92232421, 92232416, 92232423)
Declining_Abies <- c(92232412, 92232418, 92232414, 92232431, 92232417)
### IMPORT DATA ###
# importing dendro data #
list_files <- list.files(file.path(".",DATA_DIR), pattern="*.csv$", full.names=TRUE)
db<-read.all.processed(list_files)
# importing climate data #
db.env <- read.env.proc(file.path(".",ENV_DIR,'proc-env.csv'))
db.env <- db.env[db.env$series == SELECTED_TMS,]
head(db.env)
```
```{r include=FALSE}
### CLEAN & PREPARE DATA ###
db = db %>% mutate(
date = date(db$ts),
year = year(date),
class = case_when(
series %in% Declining_Abies ~ factor("D"),
series %in% Non_Declining_Abies ~ factor("ND"),
series %in% Fagus ~ factor("European beech"),
.default = NA
)
)
```
Filtering by date:
```{r}
ts_start <- "2023-05-01"
ts_end <- "2023-09-30 23:45:00"
db <- db[which(db$ts>=ts_start & db$ts<=ts_end),]
db.env <- db.env[which(db.env$ts>=ts_start & db.env$ts<=ts_end),]
```
```{r}
# INSPECT DATA
str(db)
summary(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(glue('output/all-dendrometers-{PLACE}.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(glue('output/normalized-all-dendrometers-{PLACE}.png'), width = 15, height = 10)
```
# By species
```{r}
db.D <- db %>% filter (class == "D")
db.ND <- db %>% filter (class == "ND")
db.Fag <- db %>% filter (class == "European beech")
```
```{r}
# plot_multiple_dendro(db.Abi, "Silver fir dendrometers")
```
```{r}
# plot_multiple_dendro(db.Fag, "Dendrómetros European beech")
```
Plot both in one graph:
```{r}
# Define color scales for each class
blue_colors <- c("blue", "cyan", "aquamarine", "cadetblue", "dodgerblue", "darkblue", "turquoise4", "royalblue2")
green_colors <- c("lightgreen", "green", "darkgreen", "darkolivegreen", "olivedrab", "springgreen")
orange_colors <- c("orange", "orangered", "tomato", "darkorange", "coral", "tan")
plotByClass <-
ggplot() +
ggtitle("Dendrometers painted by color scale according to its specie") +
geom_line(data = db.Fag, aes(x = ts, y = value, col = series)) +
scale_color_manual(values = green_colors, name = "European beech") +
new_scale_color() +
geom_line(data = db.ND, aes(x = ts, y = value, col = series), ) +
scale_color_manual(values = blue_colors, name = "Non-Declining Abies Alba") +
new_scale_color() +
geom_line(data = db.D, aes(x = ts, y = value, col = series), ) +
scale_color_manual(values = orange_colors, name = "Declining Abies Alba") +
new_scale_color() +
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(glue('output/all-dendrometers-by-class-{PLACE}.png'), plot = plotByClass, width = 15, height = 10)
```
Plot means for each specie in one graph:
```{r}
db.mean.Fag <- db.Fag %>% group_by(ts) %>% dplyr::summarise(n = n(), mean = mean(value, na.rm = T), sd = sd(value, na.rm = T), se = sd / sqrt(n) ) %>% mutate(class = "European beech")
db.mean.D <- db.D %>% group_by(ts) %>% dplyr::summarise(n = n(), mean = mean(value, na.rm = T), sd = sd(value, na.rm = T), se = sd / sqrt(n) ) %>% mutate(class = "Declining Silver fir")
db.mean.ND <- db.ND %>% group_by(ts) %>% dplyr::summarise(n = n(), mean = mean(value, na.rm = T), sd = sd(value, na.rm = T), se = sd / sqrt(n) ) %>% mutate(class = "Non-Declining Silver fir")
db.means <- rbind.data.frame(db.mean.Fag, db.mean.D, db.mean.ND)
plotMeans <-
ggplot(data = db.means, mapping = aes(x=ts, y=mean, col = class))+
ggtitle("Mean of the dendrometer values for the three groups of study") +
scale_color_manual(values=c("European beech" = "green","Non-Declining Silver fir" = "blue", "Declining Silver fir" = "orange")) +
scale_fill_manual(values=c("European beech" = "green","Non-Declining Silver fir" = "blue", "Declining Silver fir" = "orange")) +
geom_line( ) +
geom_ribbon(aes(ymin=mean-se, ymax=mean+se, fill=class), alpha=0.2, show.legend = FALSE, linetype = 0) +
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="%b %Y") +
theme_bw() +
theme(axis.text.x = element_text(angle = 30, hjust=1))
plotMeans
ggsave(glue('output/means-by-class-{PLACE}.png'), plot = plotMeans, width = 15, height = 10)
```
# Tree Water Deficit (TWD)
Calculate means for TWD by sp:
```{r}
db.twd <- db %>% summarise(mean_twd = mean(twd, na.rm = T), n = as.integer(n() / 96), twd_se = sd(twd, na.rm = T) / sqrt(n), .by = c(date, class)) %>% mutate(doy = yday(date))
```
Calculate VWC:
```{r}
vwc <- db.env %>% mutate(date = date(db.env$ts)) %>% summarise(max = max(vwc), .by = date)
# vwc
```
```{r warning=FALSE}
plot.twd <- db.twd %>%
ggplot () +
geom_line(aes (x = date, y = mean_twd, color = class)) +
geom_ribbon(aes (x=date, ymin=mean_twd-twd_se, ymax=mean_twd+twd_se, fill=class), alpha=0.3, show.legend = FALSE, linetype = 0) +
scale_color_manual(values=c("European beech" = "green","ND" = "blue", "D" = "orange"), labels = c("European beech" = "European beech","ND" = "Non-Declining Silver fir", "D" = "Declining Silver fir")) +
scale_fill_manual(values=c("European beech" = "green","ND" = "blue", "D" = "orange")) +
labs(title = "Means of Tree Water Deficit for the three groups of study", x = "Date", y = expression(paste("Tree Water Deficit (", mu, "m)")))+
scale_x_date(date_breaks = "1 month", date_labels="%b %Y")+
scale_y_continuous(limits=c(0, 150), sec.axis = sec_axis(trans = ~ . / 1.5, name = "Volumetric Water Content (%)")) +
geom_line(data = vwc, aes(x = date, y = max*100, linetype = "Volumetric Water Content (%)"), col = "gray28", alpha = 0.9, show.legend = c(colour = FALSE, linetype = T)) +
scale_linetype_manual(NULL, values = 4) +
theme_bw() +
theme(axis.text.x = element_text(angle = 30, hjust=1))
plot.twd
ggsave(glue('output/twd-evolution-by-class-{PLACE}.png'), plot = plot.twd, width = 15, height = 10)
```
```{r}
boxplot(mean_twd ~ class, data = db.twd)
```