################################################################################
#   Sample R program for testing and demonstrating the "FishGraph" collection
#   of R graphics functions for analysis of stock-assessment results.
#   M. H. Prager, June, 2007; revised September, 2009
################################################################################
# Set user options:
ptype <- c("wmf", "png")    # NULL (no quotes) for no plots, "eps" for eps, ...
dtype <- TRUE               # Set draft TRUE to get main titles on plots.
#-------------------------------------------------------------------------------
# Specify where the FishGraph functions are located:
# The following is the default location set by the Windows installer:
FGpath <-  paste(Sys.getenv("homedrive"), Sys.getenv("homepath"),
    "\\My Documents\\FishGraph\\functions", sep = "")

# Note: instead of the above, you can use a simple text string, such as:
# FGpath <- "C:\\Program Files\\FishGraph\\functions"
# On WINDOWS, use two "\" to get one under R, or use "/" instead.
#-------------------------------------------------------------------------------
# Read in the data from the rdat file generated by ADMB/ADMB2R:
gmm <- dget("gm_menhaden.rdat")
#-------------------------------------------------------------------------------
# Source the FishGraph functions:
source(file.path(FGpath, "fgsupport.r"))
source(file.path(FGpath, "rdat2ascii.r"))
source(file.path(FGpath, "BSR.time.plots.r"))
source(file.path(FGpath, "CLD.total.plots.r"))
source(file.path(FGpath, "Comp.plots.r"))
source(file.path(FGpath, "Comp.yearly.plots.r"))
source(file.path(FGpath, "F.time.plots.r"))
source(file.path(FGpath, "Growth.plots.r"))
source(file.path(FGpath, "index.plots.r"))
source(file.path(FGpath, "Landings.plots.r"))
source(file.path(FGpath, "NFZ.age.plots.r"))
source(file.path(FGpath, "PerRec.plots.r"))
source(file.path(FGpath, "EqRec.plots.r"))
source(file.path(FGpath, "Selectivity.plots.r"))
source(file.path(FGpath, "StockRec.plots.r"))
FGSetDefaults()             # set up default colors, etc.
#-------------------------------------------------------------------------------
# Close graphics windows; delete any existing plot history; open a graphics
# window; change FG multi-plot layout; call the function that can use a
# large page size:
#
graphics.off()
.SavedPlots <- NULL
windows(width = 8, height = 8, record = TRUE, xpos = 10, ypos = 10)
FGoptions$graphics$fglayout = c(5,3)

Comp.yearly.plots(gmm, graphics.type = ptype, plot.neff = FALSE,
    print.neff = FALSE,  compute.neff = FALSE, compact = TRUE,
    print.n = TRUE, print.angle = FALSE)
    
# Call the FG support function again to reset multi-plot layout:
source(file.path(FGpath, "fgsupport.r"))
#-------------------------------------------------------------------------------
# Open another graphics window & run the other FishGraph functions:

windows(width = 8, height = 6, record = TRUE, xpos = 10, ypos = 10)

Comp.plots(gmm, graphics.type = ptype, draft = dtype, use.color = TRUE)

Index.plots(gmm, graphics.type = ptype, log.resid = FALSE, draft = dtype)

Landings.plots(gmm, graphics.type = ptype, L.units=c("1000 mt"), draft = dtype)

F.time.plots(gmm, graphics.type = ptype, start.drop = 0, use.color = TRUE,
    draft = dtype, F.references = NULL)

NFZ.age.plots(gmm, graphics.type = ptype, use.color = TRUE, draft = dtype)

Growth.plots(gmm, graphics.type = ptype, plot.all = TRUE, draft = dtype)

PerRec.plots(gmm, graphics.type = ptype, use.color = TRUE, draft = dtype)

EqRec.plots(gmm, graphics.type = ptype, use.color = TRUE, draft = dtype)

StockRec.plots(gmm, graphics.type = ptype, draw.lowess = FALSE,
    use.color = TRUE, start.drop = 0, draft = dtype)


CLD.total.plots(gmm, graphics.type = ptype, first.year = gmm$parms$styr,
    plot.proportion = TRUE, use.color=TRUE, draft = dtype)

BSR.time.plots(gmm, start.drop = 0, use.color = TRUE, graphics.type = ptype,
    draft = dtype)

Selectivity.plots(gmm, graphics.type = ptype, draft = dtype, plot.points = TRUE)
#-------------------------------------------------------------------------------
# Run a function that saves the data object into an ASCII file (for spreadsheet
# users, etc.). For object "gmm", default ASCII file is "gmm.txt":
rdat2ascii(gmm)
#-------------------------------------------------------------------------------
# Clean up unneeded variables:
rm(FGpath, ptype, dtype)
#-------------------------------------------------------------------------------
# END OF SAMPLE FILE
#-------------------------------------------------------------------------------
