Skip to content

Commit

Permalink
added plot_stock_status
Browse files Browse the repository at this point in the history
  • Loading branch information
andybeet committed Dec 5, 2023
1 parent 7a79b9a commit d36c54d
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 1 deletion.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ Imports:
scales,
sf,
stringr,
kableExtra
kableExtra,
ggrepel
Suggests:
knitr,
rmarkdown,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export(plot_seasonal_bt_anomaly_gridded)
export(plot_seasonal_oisst_anom)
export(plot_seasonal_sst_anomaly_gridded)
export(plot_setup)
export(plot_stock_status)
export(plot_stom_fullness)
export(plot_storminess)
export(plot_survey_shannon)
Expand Down
99 changes: 99 additions & 0 deletions R/plot_stock_status.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#' plot stock_Status
#'
#' Kobe plots of regional stock status
#'
#' @param shadedRegion Numeric vector. Years denoting the shaded region of the plot (most recent 10)
#' @param report Character string. Which SOE report ("MidAtlantic", "NewEngland")
#'
#' @return list of 2 items
#'
#' \item{p}{ggplot object}
#' \item{unknown}{data frame listing stocks with unklnown status}
#'
#'
#' @export
#'

plot_stock_status <- function(shadedRegion = shadedRegion,
report="MidAtlantic") {

# generate plot setup list (same for all plot functions)
setup <- ecodata::plot_setup(shadedRegion = shadedRegion,
report=report)

# which report? this may be bypassed for some figures
if (report == "MidAtlantic") {
councils <- c("MAFMC","Both")
} else {
councils <- c("NEFMC","Both")
}

# optional code to wrangle ecodata object prior to plotting
# e.g., calculate mean, max or other needed values to join below
fix<- ecodata::stock_status |>
dplyr::mutate(Code = dplyr::recode(Code, "Dogfish" = "Sp. Dogfish" ),
Code = dplyr::recode(Code, "Mackerel" = "At. Mackerel"))
fix <- tidyr::pivot_wider(fix,names_from=Var,values_from=Value) |>
dplyr::filter(Council %in% councils) |>
(\(.) { . ->> unfiltered})() |>
dplyr::group_by(Stock) |>
dplyr::mutate(score = dplyr::case_when(
#(B.Bmsy < 0.5) ~"a",
(F.Fmsy > 1 & B.Bmsy > 0.5 & B.Bmsy < 1) ~ "a",
(F.Fmsy < 1 & B.Bmsy > 0.5 & B.Bmsy < 1) ~ "b",
(F.Fmsy < 1 & B.Bmsy > 1) ~ "c",
(F.Fmsy > 1 & B.Bmsy < 0.5) ~ "d",
(F.Fmsy < 1 & B.Bmsy < 0.5) ~ "b",
(F.Fmsy > 1 & B.Bmsy > 1) ~ "c")) |>
dplyr::mutate(Council = dplyr::recode(Council, "Both" = "NEFMC/MAFMC")) |>
dplyr::filter(!(is.na(F.Fmsy) | is.na(B.Bmsy)))

unknown <- unfiltered |>
dplyr::filter(is.na(F.Fmsy) | is.na(B.Bmsy)) |>
dplyr::select(Stock,F.Fmsy,B.Bmsy)

# code for generating plot object p
# ensure that setup list objects are called as setup$...
# e.g. fill = setup$shade.fill, alpha = setup$shade.alpha,
# xmin = setup$x.shade.min , xmax = setup$x.shade.max
#
p <- fix |>
ggplot2::ggplot()+
ggplot2::geom_vline(xintercept = 1, linetype = "dotted")+
ggplot2::geom_vline(xintercept = 0.5, linetype = "dashed")+
ggplot2::geom_hline(yintercept = 1, linetype = "dashed") +
ggplot2::geom_point(ggplot2::aes(x = B.Bmsy,
y = F.Fmsy,
shape = Council,
color = score)) +
ggrepel::geom_text_repel(ggplot2::aes(x = B.Bmsy, #geom_text_repel auto-jitters text around points
y = F.Fmsy,
label = Code,
color = score),
show.legend = FALSE, nudge_y = -0.01, nudge_x = 0.05) +
ggplot2::scale_color_brewer(palette = "Dark2",
breaks = fix$score) +
# ggplot2::ylim(0,y.max) +
# ggplot2::xlim(0,x.max) +
# ggplot2::geom_text(data = unknown, ggplot2::aes(x = x-0.5, y = y+0.2, label = text), #Custom legend for unknown stock status
# size = c(4.75,rep(4,6))) +
ggplot2::xlab(expression(~B/B[msy])) +
ggplot2::ylab(expression(~F/F[msy])) +
ggplot2::guides(color = "none") +
ggplot2::ggtitle(paste0(report,": stock status"))+
ecodata::theme_ts()+
ecodata::theme_title()

# # optional code for New England specific (2 panel) formatting
# if (report == "NewEngland") {
# p <- p +
# ggplot2::theme(legend.position = "bottom",
# legend.title = ggplot2::element_blank())
#
# }

return(list(p=p,unknown=unknown))



}
22 changes: 22 additions & 0 deletions man/plot_stock_status.Rd

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

0 comments on commit d36c54d

Please sign in to comment.