-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathplotSTPredictions.R
More file actions
249 lines (169 loc) · 9.57 KB
/
plotSTPredictions.R
File metadata and controls
249 lines (169 loc) · 9.57 KB
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
plotSTPredictions <- function(csite, substance = NULL, Wells.to.Plot = NULL,
UseLogScale = FALSE, solute_unit = "ug/l") {
# Maybe show message that nothing is selected
if (is.null(substance) || is.null(Wells.to.Plot)){
showNotification("Spatiotemporal Predictions: No data to plot.", type = "warning", duration = 5)
return(NULL)
}
if (length(Wells.to.Plot) == 0) {
showNotification("Spatiotemporal Predictions: No data to plot.", type = "warning", duration = 5)
return(NULL)
}
Cont.Data <- csite$All.Data$Cont.Data
# No model prediction possible without well coords!
Cont.Data <- Cont.Data[!is.na(Cont.Data$XCoord),]
Cont.Data <- Cont.Data[!is.na(Cont.Data$YCoord),]
SiteName <- csite$GWSDAT_Options$SiteName
# Extract selected contaminants and wells.
Cont.Data <- Cont.Data[as.character(Cont.Data$Constituent) %in% substance,]
Cont.Data$Constituent <- factor(as.character(Cont.Data$Constituent))
Cont.Data <- Cont.Data[as.character(Cont.Data$WellName) %in% Wells.to.Plot,]
# Check if there is data left to plot.
if (nrow(Cont.Data) == 0) {
showNotification("No Data to Plot!", type = "error", duration = 10)
return(NULL)
}
Cont.Data$WellName <- factor(as.character(Cont.Data$WellName), levels = sort(Wells.to.Plot))
Cont.Data <- Cont.Data[order(Cont.Data$SampleDate),]
# In the previous version, se.fit was set with this:
# 'se.fit = FALSE & panel$dlines["Conc. Trend Smoother"]'
# Seems, that it was always FALSE, so the trend lines are always plotted (which is good).
# Setting it to true omits the trend lines but prints the following error:
# "Error using packet X, Argument is of length zero".
# Thus, if disabling trend lines would be something we need, this is were we
# have to correct the code.
plotModelPredictions(csite, Cont.Data, SiteName = SiteName,
se.fit = FALSE,
UseLogScale = UseLogScale, solute_unit = solute_unit)
}
#' @importFrom lattice xyplot panel.grid
plotModelPredictions <- function(csite, Cont.Data, SiteName = "", se.fit = FALSE,
UseLogScale = FALSE, solute_unit = "ug/l"){
if (solute_unit == "mg/l") {
Cont.Data$Result.Corr.ND <- Cont.Data$Result.Corr.ND / 1000
}
if (solute_unit == "ng/l") {
Cont.Data$Result.Corr.ND <- Cont.Data$Result.Corr.ND * 1000
}
NAPL.Present <- any(tolower(as.character(na.omit(Cont.Data$Result))) == "napl")
Cont <- as.character(unique(Cont.Data$Constituent))
#my.xlim <- as.Date(range(c(csite$Cont.Data$SampleDate, csite$All.Data$GW.Data$SampleDate)))
my.xlim <- as.Date(range(Cont.Data$SampleDate))
my.xlim.orig = my.xlim
my.xlim[1] <- my.xlim.orig[1] - 0.025*as.numeric(diff(my.xlim.orig))
my.xlim[2] <- my.xlim.orig[2] + 0.025*as.numeric(diff(my.xlim.orig))
my.ylim <- range(csite$Fitted.Data[[Cont]]$Cont.Data[,c("ModelPred","Upper95","Lower95")], na.rm = TRUE)
if (solute_unit == "mg/l") {my.ylim <- my.ylim / 1000}
if (solute_unit == "ng/l") {my.ylim <- my.ylim * 1000}
my.key <- list(
space = "top",
border = FALSE,
columns = 3,
lines = list(
pch = c(0,19,19), lty = c(1,1,1), cex = rep(1.4,3), lwd = c(3), col = c("grey","black","orange"), type = c("l","p", "p")
),
text = list(
lab = c("Spatiotemporal Prediction","Detectable Data","Non-Detect Data")
)
)
if (NAPL.Present) {
my.key <- list(
space = "top",
border = FALSE,
columns = 3,
lines = list(
pch = c(0,19,19,19), lty = c(1,1,1,1), cex = rep(1.4,4), lwd = c(3), col = c("grey","black","orange","red"), type = c("l","p", "p","p")
),
text = list(
lab = c("Spatiotemporal Prediction","Detectable Data","Non-Detect Data","NAPL Substituted Data")
)
)
Cont.Data$ND<-as.character(Cont.Data$ND)
Cont.Data$ND[tolower(as.character(Cont.Data$Result))=="napl"]<-"NAPL"
}
#my.plot <-
plot(lattice::xyplot(Result.Corr.ND ~ as.Date(SampleDate) | WellName,
data = Cont.Data, groups = as.character(Cont.Data$ND),
panel = function(x, y,groups,subscripts) {
try( lattice::panel.grid(h = -1, v = 2) )
groupNDx <- x[groups[subscripts] == "TRUE"]
groupNDy <- y[groups[subscripts] == "TRUE"]
panel.xyplot(groupNDx, groupNDy, col = "orange", pch = 19, cex = 1.0)
groupx<-x[groups[subscripts]=="FALSE"]
groupy<-y[groups[subscripts]=="FALSE"]
panel.xyplot(groupx,groupy,col="black",pch=19,cex=1.0)
groupNAPLx <- x[groups[subscripts]=="NAPL"]
groupNAPLy <- y[groups[subscripts]=="NAPL"]
if(length(groupNAPLx) > 0) { panel.xyplot(groupNAPLx,groupNAPLy,col="red",pch=19,cex=1.0)}
#if(sm.fit && length(x)>1){
if(length(x)>1){
Model<-csite$Fitted.Data[[as.character(Cont)]]$Model.tune
if(!inherits(Model,"try-error")){
Model<-Model$best.model
eval.df<-data.frame(AggDate=seq(min(x,na.rm=T),max(x,na.rm=T),l=50),XCoord=rep(Cont.Data[subscripts,"XCoord"][1],50),YCoord=rep(Cont.Data[subscripts,"YCoord"][1],50))
pred <- predict(Model,eval.df, se = se.fit) ###
eval.df$pred<-pred$predicted
if (se.fit) {
eval.df$upper<-pred$predicted+pred$predicted.sd*1.96
eval.df$lower<-pred$predicted-pred$predicted.sd*1.96
}
if (solute_unit == "mg/l") {
eval.df$pred <-log(exp(eval.df$pred)/1000)
if (se.fit) {
eval.df$upper <- log(exp(eval.df$upper)/1000)
eval.df$lower <- log(exp(eval.df$lower)/1000)
}
}
if (solute_unit == "ng/l") {
eval.df$pred <-log(exp(eval.df$pred)*1000)
if(se.fit){
eval.df$upper<-log(exp(eval.df$upper)*1000)
eval.df$lower<-log(exp(eval.df$lower)*1000)
}
}
if(UseLogScale){
eval.df$pred <-log(exp(eval.df$pred),base=10)
if(se.fit){
eval.df$upper<-log(exp(eval.df$upper),base=10)
eval.df$lower<-log(exp(eval.df$lower),base=10)
}
}else{
eval.df$pred <-exp(eval.df$pred)
if (se.fit) {
eval.df$upper <- exp(eval.df$upper)
eval.df$lower <- exp(eval.df$lower)
}
}
panel.xyplot(as.Date(eval.df$AggDate),eval.df$pred,type="l",col="grey",lwd=3)
if (se.fit) {
panel.xyplot(as.Date(eval.df$AggDate), eval.df$upper,type="l",lty=2,col="grey",lwd=2)
panel.xyplot(as.Date(eval.df$AggDate), eval.df$lower,type="l",lty=2,col="grey",lwd=2)
}
}
}
},
scales = list(y = list(log = UseLogScale)),
xlab = list("Sampling Date", cex = 1.5), ylab = list(paste("Solute concentration"," (",solute_unit,")",sep=""),cex=1.5),
#layout = if (length(levels(Cont.Data$Well)) > 30) { c(4,4)} else{NULL},
xlim = my.xlim,
#ylim=my.ylim,
main = if (csite$Aquifer == "") {paste("Spatiotemporal Predictions for ",Cont,"at",SiteName) } else {
paste("Spatiotemporal Predictions for ", Cont," at ", SiteName, ": Aquifer-",csite$Aquifer, sep = "")},
drop.unused.levels = FALSE, key = my.key))
}
plotSTPredictionsPPT <- function(csite, fileout, substance = NULL, Wells.to.Plot = NULL,
UseLogScale = FALSE, solute_unit = "ug/l",
width = 900, height = 500) {
# Initialize Powerpoint file.
if (is.null(ppt_pres <- initPPT())) {
return(NULL)
}
# Create temporary wmf file.
mytemp <- tempfile(fileext = ".png")
png(mytemp, width = width, height = height)
plotSTPredictions(csite, substance, Wells.to.Plot, UseLogScale, solute_unit)
dev.off()
ppt_pres <- addPlotPPT(mytemp, ppt_pres, width, height)
print(ppt_pres, target = fileout) %>% invisible()
try(file.remove(mytemp))
}