-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathplot.R
More file actions
235 lines (177 loc) · 5.42 KB
/
Copy pathplot.R
File metadata and controls
235 lines (177 loc) · 5.42 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
# plot.R - DESC
# mse/R/plot.R
# Copyright (c) WUR, 2022.
# Author: Iago MOSQUEIRA (WMR) <iago.mosqueira@wur.nl>
#
# Distributed under the terms of the EUPL-1.2
# FLo {{{
setMethod("plot", signature(x="FLo", y="missing"),
function(x, ..., metrics=list(Rec=rec, SSB=ssb, Catch=catch, F=fbar)) {
args <- list(...)
# IF args, plot list
if(length(args) >= 1)
return(plot(x, args, metrics=metrics))
# COMPUTE metrics
mets <- do.call("metrics", list(object=x, metrics=metrics))
return(plot(mets) + ylim(c(0, NA)))
}
)
# }}}
# FLo, FLo {{{
setMethod("plot", signature(x="FLo", y="FLo"),
function(x, y, ..., metrics=list(Rec=rec, SSB=ssb, Catch=catch, F=fbar)) {
# GET all args
args <- c(list(x, y), list(...))
# ID FLo objects
idx <- unlist(lapply(args, is, "FLo"))
# CALL metrics
fqs <- lapply(args[idx], function(x) metrics(stock(x), metrics=metrics))
plotListFLQuants(fqs)
}
)
# }}}
# FLo, FLmse {{{
setMethod("plot", signature(x="FLo", y="FLmse"),
function(x, y, ..., metrics=list(Rec=rec, SSB=ssb, Catch=catch, F=fbar),
window=TRUE) {
# MERGE all FLmse args
y <- c(list(y), list(...))
plot(x, y, metrics=metrics, window=window)
}
)
# }}}
# FLo, fwdControl {{{
setMethod("plot", signature(x="FLo", y="fwdControl"),
function(x, y, fill="#E69F00", ...) {
yrs <- range(y$year)
# CREATE standard plot
p <- plot(x, ...)
# GET x variable
if(rlang::as_name(p$mapping$x) == "date") {
yrs <- range(p$data[with(p$data, year %in% yrs), "date"])
}
p + geom_vline(xintercept=yrs[1], alpha=0.4) +
geom_vline(xintercept=yrs[2], alpha=0.2) +
annotate("rect", xmin = yrs[1], xmax = yrs[2], ymin = -Inf, ymax = Inf,
fill = fill, alpha=0.1)
}
) # }}}
# FLom, list {{{
setMethod("plot", signature(x="FLom", y="list"),
function(x, y, ..., window=TRUE,
metrics=list(Rec=rec, SSB=ssb, Catch=catch, F=fbar)){
# WINDOW om
if(isTRUE(window))
maxyear <- min(unlist(lapply(y, function(i) dims(i)$minyear)))
else if(is.numeric(window))
maxyear <- window
else
maxyear <- min(unlist(lapply(y, function(i) dims(i)$maxyear)))
x <- window(x, end=maxyear)
# PARSE args
args <- list(...)
# COMPUTE metrics
mets <- lapply(c(list(OM=x), y), metrics, metrics=metrics)
# PLOT
do.call(plotListFLQuants, c(list(x=mets), args))
}
)
# }}}
# FLombf {{{
setMethod("plot", signature(x="FLombf", y="missing"),
function(x, ...) {
# GET extra args
args <- list(...)
# DISPATCH if args are not FLmse
if(length(args) == 0) {
# 1. SSB
bs <- lapply(ssb(x), unitSums)
ubs <- units(bs[[1]])
bs <- lapply(bs, function(u) {units(u) <- ""; return(u)})
p1 <- plot(bs) + ylim(c(0,NA)) +
ylab(paste0("SSB (", ubs , ")"))
# 2. F
fs <- lapply(fbar(x), unitMeans)
fs <- lapply(fs, function(u) {units(u) <- ""; return(u)})
p2 <- plot(fs) + ylim(c(0,NA)) +
ylab(paste0("F"))
# 3. F
rs <- lapply(rec(x), unitSums)
urs <- units(rs[[1]])
rs <- lapply(rs, function(u) {units(u) <- ""; return(u)})
p3 <- plot(rs) + ylim(c(0,NA)) +
ylab(paste0("Rec (", urs , ")"))
# 4. catch by fleet
cas <- lapply(catch(fisheries(x)), unitSums)
p4 <- ggplot(cas, aes(x=date, y=data, group=qname, fill=qname,
colour=qname)) +
geom_flquantiles(probs=c(0.25, 0.50, 0.75), alpha=0.3) +
geom_flquantiles(probs=c(0.05, 0.50, 0.95), alpha=0.3) +
ylim(c(0, NA)) +
ylab(paste0("Catch (", units(cas[[1]]), ")")) +
xlab("") +
# TODO: WRITE geom_label_end
geom_label_repel(data=as.data.frame(
lapply(cas, function(x) window(iterMedians(x), start=-1)),
date=TRUE), aes(label = qname), nudge_x = 2, na.rm = TRUE,
fill='white') +
theme(legend.position="none")
# COMBINE p1 + p2
return(wrap_plots(p1/p2/p3, p4))
} else {
plot(x, args)
}
}) # }}}
# FLombf, FLmse, ... {{{
setMethod("plot", signature(x="FLombf", y="FLmse"),
function(x, y, window=TRUE, ...) {
y <- c(list(y), list(...))
plot(x, y, window=window)
}
)
# }}}
# FLombf, list, ... {{{
setMethod("plot", signature(x="FLombf", y="list"),
function(x, y, window=TRUE, ...) {
# EXTRACT stock(om) as FLStocks
oms <- stock(x)
# EXTRACT FLStocks from each FLmse
mses <- lapply(y, stock)
# BUG: LIMIT F
mses <- lapply(mses, function(x) {
harvest(x[[1]])[harvest(x[[1]]) > 4] <- 4
return(x)
})
# COMBINE by biol
stks <- lapply(seq(oms), function(i)
FLStocks(c(list(oms[[i]]), lapply(mses, "[[", i)))
)
if(length(stks) == 1)
.plotom(stks[[1]], window=window, ...)
else
Reduce("|", lapply(stks, .plotom, window=window))
})
# }}}
# FLmse, missing {{{
setMethod("plot", signature(x="FLmse", y="missing"),
function(x, ...) {
# PLOT om
plot(om(x), ...)
}
)
# }}}
# .plotom {{{
.plotom <- function(stocks, window=TRUE, ...) {
# WINDOW om
if(isTRUE(window))
maxyear <- min(unlist(lapply(stocks[-1], function(i) dims(i)$minyear)))
else
maxyear <- min(unlist(lapply(stocks[-1], function(i) dims(i)$maxyear)))
stocks[[1]] <- window(stocks[[1]], end=maxyear)
# SORT OUT names
if(is.null(names(stocks)))
names(stocks) <- c("OM", paste0("MP", seq(length(stocks) - 1)))
# PLOT FLStocks
plot(FLStocks(stocks), ...)
}
# }}}