-
Notifications
You must be signed in to change notification settings - Fork 35
/
SSplotDynamicB0.R
330 lines (309 loc) · 10.2 KB
/
SSplotDynamicB0.R
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
323
324
325
326
327
328
329
330
#' Plot Dynamic B0
#'
#' Plots the spawning output with and without fishing mortality
#'
#' @template replist
#' @param ylab Y-axis label. Default is "Spawning biomass (mt)" which is replaced
#' by "Spawning output" for models with `replist[["SpawnOutputUnits"]] == "numbers"`
#' @param equilibrium Show equilibrium in plot? Applies whether "yrs" is specified
#' or not.
#' @param forecast Show forecast years in plot? Only applies if yrs = "all".
#' @param yrs Which years to include. Default "all" will show startyr to endyr + 1
#' modified by the arguments `forecast`.
#' @template plot
#' @template print
#' @template plotdir
#' @template verbose
#' @param uncertainty Show 95% uncertainty intervals around point estimates?
#' These intervals will only appear when uncertainty in the dynamic B0
#' estimates is available via the control file settings for
#' "read specs for more stddev reporting".
#' @template legend
#' @param legendlabels Character vector with labels for the unfished
#' equilibrium point (if `equilibrium = TRUE`) and the two lines showing
#' spawning biomass or output without and with fishing.
#' @template legendloc
#' @param col Optional vector of colors to be used for the two lines
#' (single value will apply to both lines).
#' @param lty Optional vector of line types to be used for the two lines
#' (single value will apply to both lines).
#' @param lwd Optional vector of line widths to be used for the two lines.
#' Single value will apply to both lines.
#' @param add add to existing plot
#' @template pwidth
#' @template pheight
#' @template punits
#' @template ptsize
#' @template res
#' @template mainTitle
#' @template mar
#' @author Ian G. Taylor
#' @export
#' @seealso [SSplotTimeseries()]
SSplotDynamicB0 <- function(replist,
ylab = "Spawning biomass (mt)",
equilibrium = TRUE,
forecast = FALSE,
yrs = "all",
plot = TRUE, print = FALSE, plotdir = "default", verbose = TRUE,
uncertainty = TRUE,
legend = TRUE,
legendlabels = c("equilibrium", "without fishing", "with fishing"),
legendloc = "bottom",
col = c("blue", "red"),
lty = 1,
lwd = 2,
add = FALSE,
pwidth = 6.5, pheight = 5.0, punits = "in", res = 300, ptsize = 10,
mainTitle = FALSE,
mar = NULL) {
Dynamic_Bzero <- replist[["Dynamic_Bzero"]]
if (is.null(Dynamic_Bzero)) {
warning('No element "Dynamic_Bzero" in replist input')
return()
}
if (!"SSB_nofishing" %in% names(replist[["Dynamic_Bzero"]])) {
warning("Skipping Dynamic B0 plot (not yet working for this model configuration)")
return()
}
# check if spawning output rather than spawning biomass is plotted
if (is.null(replist[["SpawnOutputUnits"]]) ||
is.na(replist[["SpawnOutputUnits"]]) ||
replist[["SpawnOutputUnits"]] == "numbers") { # quantity from test in SS_output
if (ylab == "Spawning biomass (mt)") {
ylab <- "Spawning output"
}
}
# set default plot margins
if (is.null(mar)) {
if (mainTitle) {
main <- "Dynamic B0"
mar <- c(5, 4, 4, 2) + 0.1
} else {
main <- ""
mar <- c(5, 4, 2, 2) + 0.1
}
}
# repeat inputs for line style if needed
if (length(col) == 1) {
col <- rep(col, 2)
}
if (length(lty) == 1) {
lty <- rep(lty, 2)
}
if (length(lwd) == 1) {
lwd <- rep(lwd, 2)
}
plotinfo <- NULL
# set plot directory
if (plotdir == "default") {
plotdir <- replist[["inputs"]][["dir"]]
}
# set default range of years
if (yrs[1] == "all") {
yrs <- replist[["startyr"]]:(replist[["endyr"]] + 1)
if (forecast) {
yrs <- replist[["startyr"]]:max(Dynamic_Bzero[["Yr"]])
}
}
# equilibrium year shifted by 1 because INIT point isn't shown
sub_equil <- Dynamic_Bzero[["Era"]] == "VIRG"
equil_yr <- Dynamic_Bzero[["Yr"]][sub_equil] + 1
# set ymax
ymax <- max(
ifelse(equilibrium,
Dynamic_Bzero[["SSB"]][sub_equil],
NA
),
Dynamic_Bzero[["SSB"]][Dynamic_Bzero[["Yr"]] %in% yrs],
Dynamic_Bzero[["SSB_nofishing"]][Dynamic_Bzero[["Yr"]] %in% yrs],
na.rm = TRUE
)
# get subsets of derived quantities and extract year value
quants <- replist[["derived_quants"]]
quants_SSB <- quants[grep("SSB_", quants[["Label"]]), ]
quants_SSB_nofishing <- quants[grep("Dyn_Bzero_", quants[["Label"]]), ]
if (uncertainty & nrow(quants_SSB_nofishing) == 0) {
uncertainty <- FALSE
warning(
"Dynamic B0 not found in derived quantities, ",
"changing uncertainty to FALSE. ",
"To get uncertainty, modify control file under ",
"'read specs for more stddev reporting'."
)
}
if (uncertainty) {
# calculate intervals
# add columns to store info
Dynamic_Bzero[["SSB_lo"]] <- NA
Dynamic_Bzero[["SSB_hi"]] <- NA
Dynamic_Bzero[["SSB_nofishing_lo"]] <- NA
Dynamic_Bzero[["SSB_nofishing_hi"]] <- NA
# get year for each row
quants_SSB[["Yr"]] <- substring(quants_SSB[["Label"]],
first = nchar("SSB_") + 1
)
quants_SSB_nofishing[["Yr"]] <- substring(quants_SSB_nofishing[["Label"]],
first = nchar("Dyn_Bzero_") + 1
)
# get unfished equilibrium uncertainty intervals (same with/without fishing)
Dynamic_Bzero[sub_equil, c("SSB_lo", "SSB_hi")] <-
qnorm(
p = c(0.025, 0.975),
mean = quants["SSB_Virgin", "Value"],
sd = quants["SSB_Virgin", "StdDev"]
)
# loop over years to get uncertainty intervals
for (y in Dynamic_Bzero[["Yr"]]) {
# fill in SSB uncertainty
if (y %in% quants_SSB[["Yr"]]) {
Dynamic_Bzero[Dynamic_Bzero[["Yr"]] == y, c("SSB_lo", "SSB_hi")] <-
qnorm(
p = c(0.025, 0.975),
mean = quants_SSB[paste0("SSB_", y), "Value"],
sd = quants_SSB[paste0("SSB_", y), "StdDev"]
)
}
# fill in dynamic B0 uncertainty
if (y %in% quants_SSB_nofishing[["Yr"]]) {
Dynamic_Bzero[Dynamic_Bzero[["Yr"]] == y, c("SSB_nofishing_lo", "SSB_nofishing_hi")] <-
qnorm(
p = c(0.025, 0.975),
mean = quants_SSB_nofishing[paste0("Dyn_Bzero_", y), "Value"],
sd = quants_SSB_nofishing[paste0("Dyn_Bzero_", y), "StdDev"]
)
}
}
# update ymax
ymax <- max(ymax,
ifelse(equilibrium,
Dynamic_Bzero[["SSB_hi"]][sub_equil],
NA
),
Dynamic_Bzero[["SSB_hi"]][Dynamic_Bzero[["Yr"]] %in% yrs],
Dynamic_Bzero[["SSB_nofishing_hi"]][Dynamic_Bzero[["Yr"]] %in% yrs],
na.rm = TRUE
)
}
plotfun <- function() {
if (!add) {
plot(0,
type = "n", , xlab = "Year", ylab = ylab, xlim = range(yrs),
ylim = c(0, 1.1 * ymax),
yaxs = "i",
cex.lab = 1.0, cex.axis = 1.0, cex = 0.7,
mar = mar,
main = main
)
}
sub <- Dynamic_Bzero[["Yr"]] %in% yrs & Dynamic_Bzero[["Era"]] != "VIRG"
if (uncertainty) {
# add intervals to plot
if (equilibrium) {
# add equilibrium intervals
arrows(
x0 = equil_yr,
y0 = Dynamic_Bzero[sub_equil, "SSB_lo"],
x1 = equil_yr,
y1 = Dynamic_Bzero[sub_equil, "SSB_hi"],
length = 0.01, angle = 90, code = 3, col = col[1],
lwd = 1
)
}
# function to add shaded uncertainty intervals behind line
addpoly <- function(yrvec, lower, upper, col) {
lower[lower < 0] <- 0 # max of value or 0
good <- !is.na(lower) & !is.na(upper)
polygon(
x = c(yrvec[good], rev(yrvec[good])),
y = c(lower[good], rev(upper[good])),
border = NA, col = col
)
}
# add polygons around time series
addpoly(
yrvec = Dynamic_Bzero[["Yr"]][sub],
lower = Dynamic_Bzero[["SSB_lo"]][sub],
upper = Dynamic_Bzero[["SSB_hi"]][sub],
col = adjustcolor(col[2], alpha.f = 0.1)
)
addpoly(
yrvec = Dynamic_Bzero[["Yr"]][sub],
lower = Dynamic_Bzero[["SSB_nofishing_lo"]][sub],
upper = Dynamic_Bzero[["SSB_nofishing_hi"]][sub],
col = adjustcolor(col[1], alpha.f = 0.1)
)
} # end check for uncertainty
lines(
x = Dynamic_Bzero[["Yr"]][sub],
y = Dynamic_Bzero[["SSB"]][sub],
lwd = lwd[2], lty = lty[2], col = col[2]
)
lines(
x = Dynamic_Bzero[["Yr"]][sub],
y = Dynamic_Bzero[["SSB_nofishing"]][sub],
lwd = lwd[1], lty = lty[1], col = col[1]
)
if (equilibrium) {
points(
x = equil_yr,
y = Dynamic_Bzero[["SSB"]][sub_equil],
cex = 1.5,
pch = 16,
col = col[1]
)
# add to vectors used in the legend
lty <- c(NA, lty)
lwd <- c(NA, lwd)
col <- c(col[1], col)
pch <- c(16, NA, NA)
} else {
pch <- NA
legendlabels <- legendlabels[2:3]
}
if (legend) {
legend(
x = legendloc,
legend = legendlabels,
col = col,
lwd = lwd,
lty = lty,
pch = pch,
pt.cex = 1.5,
bty = "n",
ncol = ifelse(equilibrium, 3, 2)
)
}
}
if (plot) {
plotfun()
}
if (print) {
caption <- paste0(
"Dynamic B0 plot. The lower line shows the time series ",
"of estimated ", ylab, " in the presence of fishing ",
"mortality. The upper line shows the time series that ",
"could occur under the same dynamics (including ",
"deviations in recruitment), but without fishing."
)
if (equilibrium) {
caption <- paste0(
caption,
" The point at the left represents the unfished equilibrium."
)
}
file <- "ts_DynamicB0.png"
plotinfo <- save_png(
plotinfo = plotinfo, file = file, plotdir = plotdir, pwidth = pwidth,
pheight = pheight, punits = punits, res = res, ptsize = ptsize,
caption = caption
)
plotfun()
dev.off()
if (!is.null(plotinfo)) {
plotinfo[["category"]] <- "Timeseries"
}
}
if (verbose) message("Plotting Dynamic B0")
return(invisible(plotinfo))
}