-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot-raw-dendro.Rmd
225 lines (180 loc) · 6.41 KB
/
plot-raw-dendro.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
```{r}
source("lib-dendro.R")
library(ggplot2)
library(dplyr)
### DEFINE GLOBAL VARS ###
PATH = '/home/akronix/workspace/dendro';
setwd(PATH)
DATA_DIR = 'raw/Boalar-dataD'
FILENAME_EXCESS = "_2024_02_29_0.csv"
# Set initial and final date and sampling dates
ts_start<-"2022-03-17 11:15" # 2 days after installation
ts_end<-"2024-02-29 00:00" # last timestamp of downloaded data
### IMPORT DENDRO DATA ###
# importing dendro data #
list_files <- list.files(file.path(".",DATA_DIR), pattern="*.csv$", full.names=TRUE)
db<-read.all.dendro(list_files, ts_start, ts_end, old_format = T)
summary(db)
### CLEAN & PREPARE DATA ###
# Clean name of field series
db$series <- gsub(paste0("./", DATA_DIR, "/"),"",db$series)
db$series <- gsub(FILENAME_EXCESS,"",db$series) # remove trailing filename _%date%_0.csv
db$series <- substr(db$series,6,nchar(db$series)) # remove initial "data_" in filename
# INSPECT DATA
str(db)
head(db)
tail(db)
```
```{r}
## PLOT ALL DENDROS ##
plot_multiple_dendro <- function (data, title) {
ggplot(data = data, mapping = aes(x=ts, y=value, 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()
}
```
```{r}
plot_multiple_dendro(db, "All raw dendrometers data")
```
```{r}
db = filter(db, series != "92232429")
plot_multiple_dendro(db, "All raw dendrometers data")
```
# Normalization 0-1 to better compare dendros
```{r}
normalized.db <- db %>%
select (series, ts, value) %>%
group_by(series) %>%
mutate( normalized_value = ( (value - min(value, na.rm = TRUE) ) / (max(value, na.rm = TRUE) - min(value, na.rm = TRUE)) ), .keep = 'all' )
```
```{r}
plot_multiple_dendro(normalized.db, "Dendros data normalized to [0-1]")
```
# Mean + strip region of SE
```{r}
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=value)) +
ggtitle("Mean of raw darta for dendrometers with ± standard error strips") +
geom_ribbon(aes(ymin=value-se, ymax=value+se), fill='lightgreen', alpha=0.3, show.legend = FALSE, linetype = 0) +
geom_line( aes (linetype = "Quercus Ilex"), 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('mean+Valcuerna.png', width = 15, height = 10)
```
# By species
```{r}
db.lentiscos <- db %>% filter (series %in% c(92223485, 92232435, 92232429, 92232425, 92232432) )
db.sabinas <- db %>% filter (series %in% c(92232422, 92232436, 92232430, 92232434, 92232433, 92232427, 92232428, 92232431, 92232426))
```
```{r}
db.lentiscos.agg <- summarySE(db.lentiscos, measurevar = "value", groupvars = c("ts"), na.rm = TRUE)
head(db.lentiscos.agg)
tail(db.lentiscos.agg)
```
```{r}
#library(mdthemes)
plot_mean_se <- function (data, species, output_fn = FALSE) {
plot <-
ggplot(data = data, aes(x=ts, y=value)) +
ggtitle(paste0("Mean of raw data for ", species, " with ± standard error strips")) +
geom_ribbon(aes(ymin=value-se, ymax=value+se), 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() +
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){
ggsave(output_fn, width = 15, height = 10)
}
}
```
```{r}
plot_mean_se(db.lentiscos.agg, "Pistacia Lentiscus" )
```
```{r}
db.sabinas.agg <- summarySE(db.sabinas, measurevar = "value", groupvars = c("ts"), na.rm = TRUE)
head(db.sabinas.agg)
tail(db.sabinas.agg)
```
```{r}
plot_mean_se(db.sabinas.agg, "Juniperus Phoenicea" )
```
Join both df species in one
```{r}
db.sabinas.agg$class = factor("sabinas")
db.lentiscos.agg$class = factor("lentiscos")
db.sabinasylentiscos = rbind.data.frame(db.sabinas.agg,db.lentiscos.agg)
str(db.sabinasylentiscos)
```
Plot both dendros in one graph
```{r}
plotValcuernaMeans <-
ggplot(data = db.sabinasylentiscos, aes(x=ts, y=value, col=class)) +
ggtitle("Mean of raw data for Pistacia Lentiscus and Juniperus Phoenicea with ± standard error strips") +
geom_line() +
geom_ribbon(aes(ymin=value-se, ymax=value+se, fill=class), alpha=0.2, show.legend = FALSE, linetype = 0) +
#geom_line( aes (linetype = "Quercus Ilex"), 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))
plotValcuernaMeans
#ggsave('mean+Valcuerna.png', width = 15, height = 10)
```
```{r}
stop("halted by user")
```
```{r}
# db.fresnos <- db %>% filter (series %in% c(92231801:92231805) )
# plot_multiple_dendro(db.fresnos, "Dendrómetros fresnos en bruto")
```
```{r}
# db.olmos <- db %>% filter (series %in% c(92231806:92231810) )
# plot_multiple_dendro(db.olmos,"Dendrómetros olmos en bruto")
```
# Normalization 0-1 to better compare dendros
```{r}
normalized.olmos <- db.olmos %>%
group_by(series) %>%
mutate( normalized_value = ( (value - min(value, na.rm = TRUE) ) / (max(value, na.rm = TRUE) - min(value, na.rm = TRUE)) ))
normalized.olmos
```
```{r}
normalized.olmos$value = normalized.olmos$normalized_value
plot_multiple_dendro(normalized.olmos, "Dendrómetros olmos normalizados 0-1")
```
```{r}
normalized.fresnos <- db.fresnos %>%
group_by(series) %>%
mutate( normalized_value = ( (value - min(value, na.rm = TRUE) ) / (max(value, na.rm = TRUE) - min(value, na.rm = TRUE)) ))
normalized.fresnos
```
```{r}
normalized.fresnos$value = normalized.fresnos$normalized_value
plot_multiple_dendro(normalized.fresnos, "Dendrómetros fresnos normalizados 0-1")
```