Skip to content

Commit

Permalink
creates exploratory plots of the data.
Browse files Browse the repository at this point in the history
given an ITIS/stock and a metric to be plotted. All assessments are plotted
  • Loading branch information
andybeet committed Jan 5, 2023
1 parent 1762453 commit 391a749
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ export(get_available_ts)
export(get_latest_full_assessment)
export(get_latest_metrics)
export(get_species_itis)
export(plot_ts)
importFrom(magrittr,"%>%")
importFrom(rlang,.data)
103 changes: 103 additions & 0 deletions R/plot_ts.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#' Plot time series data for a particular species
#'
#' Exploratory plots of Catch, Abundance, F, Recruitment for all assessments for a particular species
#'
#' @param itis Numeric. Species ITIS code
#' @param stock Character. Full name of stock (only required if more than one stock exists for ITIS code)
#' @param metric Character vector. Specifying which metric to plot (Catch, Abundance, Fmort, Recruitment)
#' @param plotAs Character. How to plpot results. Plot all assessments on a single plot ("one") or
#' plot each assessment in its own facet ("facet")
#'
#' @section Units:
#' The units of some of the metrics change over time. For example, catch for one assessment may be presented in metric tons
#' whereas other assessments may be in thousand metric tons. This is not resolved in the plotting. When this arises it is best
#' to use \code{plotAs = "facet"} since each facet uses its own yaxis scale
#'
#' @return A ggplot object is returned. A figure is also returned
#'
#' @importFrom magrittr "%>%"
#' @importFrom rlang .data
#'
#' @export

plot_ts <- function(itis=NULL, stock=NULL, metric = "Catch", plotAs="one") {


#error check for metric names
if (is.null(itis)) {
stop("If you do not know the ITIS code then please use get_species_itis()")
}
if (!metric %in% c("Catch","Fmort","Recruitment","Abundance","Index")) {
stop("Please use of the defined metrics to plot. Catch, Fmort, Recruitment, Abundance, Index")
}


# filter by ITIS and Metric
dataToPlot <- stocksmart::stockAssessmentData %>%
dplyr::filter(.data$ITIS == itis,Metric == metric) %>%
dplyr::mutate(AssessmentYear = as.factor(AssessmentYear))

if(nrow(dataToPlot) ==0){
message("The metric you selected doesn't exist for this stock")
return()
}

# select stocks for this ITIS
stocks <- dataToPlot %>%
dplyr::distinct(StockName)

# if multiple stocks and user argument - NULL
if ((nrow(stocks) > 1) && (is.null(stock))){
message(paste0("There are multiple stocks for ITIS = ",itis,". Please specify which stock you'd like to plot"))
stop(stocks)
}
# if multiple stocks and user argument doesn't match
if ((nrow(stocks) > 1) && (!stock %in% (stocks %>% dplyr::pull()))){
message(paste0("Please specify a valid stock to plot "))
stop(stocks)
}

# if only a single stock and user argument doesn't match
if (nrow(stocks) == 1) {
if (is.null(stock)) { # user left blank
stock = stocks
} else { # user entereres a string
if (!stock %in% (stocks %>% dplyr::pull())) {
message(paste0("Please specify a valid stock to plot "))
stop(stocks)
}
}

}

# filter out stock
stockToPlot <- dataToPlot %>%
dplyr::filter(StockName == stock)

# units check and standardize
# eg if units change over time from say mt to thousands mt


# plot data
if (plotAs == "facet"){
p <- ggplot2::ggplot(stockToPlot) +
ggplot2::geom_line(ggplot2::aes(x=Year,y=Value)) +
ggplot2::facet_wrap(~AssessmentYear,scales = "free_y")
} else {
if ((stockToPlot %>% dplyr::distinct(Units) %>% nrow()) >1) {
message("The units change over time. Plotting as a facet plot will be better")
}
p <- ggplot2::ggplot(stockToPlot) +
ggplot2::geom_line(ggplot2::aes(x=Year,y=Value,color = AssessmentYear))
}

p <- p +
ggplot2::ylab(paste0(metric," (",stockToPlot %>% dplyr::distinct(Units),")")) +
ggplot2::ggtitle(stockToPlot %>% dplyr::distinct(StockName))

print(p)

return(p)


}
31 changes: 31 additions & 0 deletions man/plot_ts.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 391a749

Please sign in to comment.