-
Notifications
You must be signed in to change notification settings - Fork 23
/
plot_ss3sim.R
51 lines (50 loc) · 1.63 KB
/
plot_ss3sim.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
#' Base plot for `ss3sim` data
#'
#' Use [ggplot2::ggplot] to plot data from `ss3sim`
#' simulation.
#' @template plot-functions
#' @template plot-functions-color
#'
plot_ss3sim <- function(data, x, y, color = NULL,
relative.error = FALSE, axes.free = TRUE, print = TRUE,
horiz = NULL, horiz2 = NULL, vert = NULL, vert2 = NULL) {
verify_plot_arguments(
data = data, x = x, y = y, print = print,
horiz = horiz, horiz2 = horiz2, vert = vert, vert2 = vert2,
color = color, relative.error = relative.error, axes.free = axes.free
)
data <- data[!is.na(data[, y]), ]
if (!"ID" %in% colnames(data)) {
data[, "ID"] <- paste(data[, "scenario"], data[, "iteration"], sep = "-")
}
if (!is.null(color)) {
stopifnot(is.numeric(data[, color]))
}
g <- ggplot2::ggplot(
data = data,
ggplot2::aes(x = .data[[x]], y = .data[[y]])
) +
ggplot2::xlab(gsub("year", "Year", x))
if (x == "year") {
g <- g + ggplot2::aes(group = .data[["year"]])
}
if (relative.error != FALSE) {
if (relative.error == TRUE) relative.error <- "black"
g <- g + ggplot2::coord_cartesian(ylim = c(-1, 1)) +
ggplot2::ylab(paste("Relative error for:", gsub("_re", "", y))) +
ggplot2::geom_hline(yintercept = 0, col = relative.error, lty = 2)
} else {
g <- g + ggplot2::ylab(y)
}
form <- facet_form(horiz, horiz2, vert, vert2)
if (!is.null(form)) {
g <- g + ggplot2::facet_grid(form,
scales = ifelse(axes.free, "free", "fixed")
)
}
if (!is.null(color)) {
g <- g + ggplot2::aes(color = .data[[color]]) +
ggplot2::labs(col = color)
}
invisible(g)
}