|
1 |
| -#' PlotSimASAP |
2 |
| -#' |
3 |
| -#' Comparison plots of SSB, Freport, and Recruitment from SimASAP runs with true values. |
4 |
| -#' |
5 |
| -#' @param wd directory where original ASAP run is located |
6 |
| -#' @param asap.name base name of original dat file (without the .dat extension) |
7 |
| -#' @param whichsim vector of numbers for which simulated data sets to plot |
8 |
| -#' @param od location where new data sets are saved (defaults to sim subdirectory of wd) |
9 |
| -#' @param save.plots will save png files if TRUE (default = FALSE) |
10 |
| -#' @param returnwhat valid options are "results", "plot", "both", "nothing" (default="nothing") |
11 |
| -#' @export |
12 |
| - |
13 |
| -PlotSimASAP <- function(wd, asap.name, whichsim, od=file.path(wd, "sim"), save.plots=FALSE, returnwhat="nothing"){ |
14 |
| - |
15 |
| - # check for valid returnwhat value |
16 |
| - validoptions <- c("results", "plot", "both", "nothing") |
17 |
| - if (!(returnwhat %in% validoptions)){ |
18 |
| - return(paste("Error: returnwhat must be one of:", paste(validoptions, collapse = ", "))) |
19 |
| - } |
20 |
| - |
21 |
| - # check for files |
22 |
| - if (!file.exists(file.path(wd, paste0(asap.name, ".rdat")))){ |
23 |
| - return(paste0("Error: ", asap.name, ".rdat not located in ", wd)) |
24 |
| - } |
25 |
| - |
26 |
| - simrdats <- FALSE |
27 |
| - for (isim in 1:length(whichsim)){ |
28 |
| - if(file.exists(file.path(od, paste0(asap.name, "_sim", isim, ".rdat")))){ |
29 |
| - simrdats <- TRUE |
30 |
| - } |
31 |
| - } |
32 |
| - if (simrdats == FALSE){ |
33 |
| - return(paste0("Error: no files ", asap.name, "_sim(whichsim).rdat located in ", od)) |
34 |
| - } |
35 |
| - |
36 |
| - # get true values |
37 |
| - asap <- dget(file.path(wd, paste0(asap.name, ".rdat"))) |
38 |
| - years <- seq(asap$parms$styr, asap$parms$endyr) |
39 |
| - nyears <- length(years) |
40 |
| - res <- data.frame(Source = "True", |
41 |
| - metric = rep(c("SSB", "Freport", "Recruits"), each=nyears), |
42 |
| - Year = rep(years, 3), |
43 |
| - value = c(asap$SSB, asap$F.report, asap$N.age[,1])) |
44 |
| - |
45 |
| - # get simulated values |
46 |
| - for (isim in 1:length(whichsim)){ |
47 |
| - mysim <- whichsim[isim] |
48 |
| - fname <- file.path(od, paste0(asap.name, "_sim", mysim, ".rdat")) |
49 |
| - if (file.exists(fname)){ |
50 |
| - asap <- dget(fname) |
51 |
| - simres <- data.frame(Source = paste0("Sim", mysim), |
52 |
| - metric = rep(c("SSB", "Freport", "Recruits"), each=nyears), |
53 |
| - Year = rep(years, 3), |
54 |
| - value = c(asap$SSB, asap$F.report, asap$N.age[,1])) |
55 |
| - res <- rbind(res, simres) |
56 |
| - } |
57 |
| - } |
58 |
| - |
59 |
| - # make plot and optionally save |
60 |
| - p <- ggplot2::ggplot(res, aes(x=Year, y=value, color=Source)) + |
61 |
| - geom_line() + |
62 |
| - geom_point(data=dplyr::filter(res, Source == "True")) + |
63 |
| - facet_wrap(~metric, ncol = 1, scales = "free_y") + |
64 |
| - expand_limits(y=0) + |
65 |
| - theme_bw() |
66 |
| - |
67 |
| - if (length(unique(res$Source)) > 5){ |
68 |
| - p <- p + theme(legend.position = "none") |
69 |
| - } |
70 |
| - |
71 |
| - print(p) |
72 |
| - if (save.plots == TRUE){ |
73 |
| - ggplot2::ggsave(filename = file.path(od, "comparisonplots.png"), p) |
74 |
| - } |
75 |
| - |
76 |
| - myreturn <- NULL |
77 |
| - if (returnwhat == "results"){ |
78 |
| - myreturn <- res |
79 |
| - } else if (returnwhat == "plot"){ |
80 |
| - myreturn <- p |
81 |
| - } else if (returnwhat == "both"){ |
82 |
| - myreturn <- list(res = res, p = p) |
83 |
| - } |
84 |
| - return(myreturn) |
85 |
| -} |
| 1 | +#' PlotSimASAP |
| 2 | +#' |
| 3 | +#' Comparison plots of SSB, Freport, and Recruitment from SimASAP runs with true values. |
| 4 | +#' |
| 5 | +#' @param wd directory where original ASAP run is located |
| 6 | +#' @param asap.name base name of original dat file (without the .dat extension) |
| 7 | +#' @param whichsim vector of numbers for which simulated data sets to plot |
| 8 | +#' @param od location where new data sets are saved (defaults to sim subdirectory of wd) |
| 9 | +#' @param asapretro logical flag for whether to add "_000" before ".rdat" when selecting files (default=FALSE) |
| 10 | +#' @param save.plots will save png files if TRUE (default = FALSE) |
| 11 | +#' @param returnwhat valid options are "results", "plot", "both", "nothing" (default="nothing") |
| 12 | +#' @export |
| 13 | + |
| 14 | +PlotSimASAP <- function(wd, asap.name, whichsim, od=file.path(wd, "sim"), asapretro=FALSE, save.plots=FALSE, returnwhat="nothing"){ |
| 15 | + |
| 16 | + # check for valid returnwhat value |
| 17 | + validoptions <- c("results", "plot", "both", "nothing") |
| 18 | + if (!(returnwhat %in% validoptions)) { |
| 19 | + return(paste("Error: returnwhat must be one of:", paste(validoptions, collapse = ", "))) |
| 20 | + } |
| 21 | + |
| 22 | + # check for files |
| 23 | + rdat_ext <- ifelse(asapretro == FALSE, ".rdat", "_000.rdat") |
| 24 | + |
| 25 | + if (!file.exists(file.path(wd, paste0(asap.name, rdat_ext)))) { |
| 26 | + return(paste0("Error: ", asap.name, rdat_ext, " not located in ", wd)) |
| 27 | + } |
| 28 | + |
| 29 | + simrdats <- FALSE |
| 30 | + for (isim in 1:length(whichsim)) { |
| 31 | + if(file.exists(file.path(od, paste0(asap.name, "_sim", isim, rdat_ext)))) { |
| 32 | + simrdats <- TRUE |
| 33 | + } |
| 34 | + } |
| 35 | + if (simrdats == FALSE) { |
| 36 | + return(paste0("Error: no files ", asap.name, "_sim(whichsim)", rdat_ext," located in ", od)) |
| 37 | + } |
| 38 | + |
| 39 | + # get true values |
| 40 | + asap <- dget(file.path(wd, paste0(asap.name, rdat_ext))) |
| 41 | + years <- seq(asap$parms$styr, asap$parms$endyr) |
| 42 | + nyears <- length(years) |
| 43 | + res <- data.frame(Source = "True", |
| 44 | + metric = rep(c("SSB", "Freport", "Recruits"), each=nyears), |
| 45 | + Year = rep(years, 3), |
| 46 | + value = c(asap$SSB, asap$F.report, asap$N.age[,1])) |
| 47 | + |
| 48 | + # get simulated values |
| 49 | + for (isim in 1:length(whichsim)) { |
| 50 | + mysim <- whichsim[isim] |
| 51 | + fname <- file.path(od, paste0(asap.name, "_sim", mysim, rdat_ext)) |
| 52 | + if (file.exists(fname)) { |
| 53 | + asap <- dget(fname) |
| 54 | + simres <- data.frame(Source = paste0("Sim", mysim), |
| 55 | + metric = rep(c("SSB", "Freport", "Recruits"), each=nyears), |
| 56 | + Year = rep(years, 3), |
| 57 | + value = c(asap$SSB, asap$F.report, asap$N.age[,1])) |
| 58 | + res <- rbind(res, simres) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + # make plot and optionally save |
| 63 | + p <- ggplot2::ggplot(res, aes(x=Year, y=value, color=Source)) + |
| 64 | + geom_line() + |
| 65 | + geom_point(data=dplyr::filter(res, Source == "True")) + |
| 66 | + facet_wrap(~metric, ncol = 1, scales = "free_y") + |
| 67 | + expand_limits(y=0) + |
| 68 | + theme_bw() |
| 69 | + |
| 70 | + if (length(unique(res$Source)) > 5){ |
| 71 | + p <- p + theme(legend.position = "none") |
| 72 | + } |
| 73 | + |
| 74 | + print(p) |
| 75 | + if (save.plots == TRUE){ |
| 76 | + ggplot2::ggsave(filename = file.path(od, "comparisonplots.png"), p) |
| 77 | + } |
| 78 | + |
| 79 | + myreturn <- NULL |
| 80 | + if (returnwhat == "results"){ |
| 81 | + myreturn <- res |
| 82 | + } else if (returnwhat == "plot"){ |
| 83 | + myreturn <- p |
| 84 | + } else if (returnwhat == "both"){ |
| 85 | + myreturn <- list(res = res, p = p) |
| 86 | + } |
| 87 | + return(myreturn) |
| 88 | +} |
0 commit comments