Skip to content

Commit

Permalink
add plot_hms_stock_status
Browse files Browse the repository at this point in the history
  • Loading branch information
sgaichas committed Dec 13, 2023
1 parent 5027be7 commit a789122
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export(plot_heatwave_peak_date)
export(plot_heatwave_year)
export(plot_hms_cpue)
export(plot_hms_landings)
export(plot_hms_stock_status)
export(plot_ichthyo_diversity)
export(plot_long_term_sst)
export(plot_mab_inshore_survey)
Expand Down
152 changes: 152 additions & 0 deletions R/plot_hms_stock_status.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#' plot hms_stock_status
#'
#' Kobe plot of F/Fmsy vs B/Bmsy for Highly Migratory Species (HMS) stocks.
#'
#' @param shadedRegion Numeric vector. Years denoting the shaded region of the plot (most recent 10)
#' @param report Character string. Which SOE report ("MidAtlantic" default, same for both)
#'
#' @return list of 2 items
#'
#' \item{p}{ggplot object}
#' \item{unknown}{data frame listing stocks with unklnown status}
#'
#' @export
#'

plot_hms_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") {
filterEPUs <- c("MAB")
} else {
filterEPUs <- c("GB", "GOM")
}

# optional code to wrangle ecodata object prior to plotting
# e.g., calculate mean, max or other needed values to join below
y.max <- 5
x.max <- 2
#Get data, spread for plotting, and filter
# for now include legend because it has 2 species not in table??
unknownp <- data.frame(text = c("Unknown Status", "ATL SBH", "SPF", "WA BFT"),
x = rep(0.9*x.max,4), y = seq(0.88*y.max,3.5,-0.3))




stock_status<-ecodata::hms_stock_status |>
#tidyr::spread(.,Var,Value) |>
tidyr::separate(Var, c("species_abr", "spp", "Var"), ":") |>
tidyr::pivot_wider(names_from = Var, values_from = Value) |>
dplyr::group_by(spp) |>
dplyr::filter(!species_abr == "------",
!species_abr == "-----") |>
dplyr::mutate(score = case_when(
#(B.Bmsy <0.5) ~"a",
#(B.Bmsy == 0.5) ~"a",
#(B.Bmsy <1) ~"a",
#(B.Bmsy == 1) ~"a",
(F.Fmsy == 1) ~ "a",
(F.Fmsy >1) ~ "a",
#(F.Fmsy < 1 & B.Bmsy > 0.5 & B.Bmsy < 1) ~ "b",
(F.Fmsy < 1 & B.Bmsy < 1) ~ "b",
(F.Fmsy < 1 & B.Bmsy > 1) ~ "c"))

unknown <- stock_status |>
dplyr::filter(is.na(F.Fmsy) | is.na(B.Bmsy)) |>
dplyr::select(spp,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 <- stock_status |>
ggplot2::ggplot() +
ggplot2::geom_vline(xintercept = 1, linetype = "dotted", color = "grey60")+
#ggplot2::geom_vline(xintercept = 0.5, linetype = "dashed", color = "grey60")+
ggplot2::geom_hline(yintercept = 1, linetype = "dashed", color = "grey60") +
ggplot2::geom_point(ggplot2::aes(x = B.Bmsy,
y = F.Fmsy,
color = stock_status$score)) +
ggrepel::geom_text_repel(ggplot2::aes(x = B.Bmsy, #geom_text_repel auto-jitters text around points
y = F.Fmsy,
label = species_abr,
color = stock_status$score), show.legend = FALSE,
nudge_y = -0.01, nudge_x = 0.05) +
ggplot2::ylim(0,y.max) +
ggplot2::xlim(0,x.max*1.1) +
ggplot2::geom_text(data = unknownp, ggplot2::aes(x = x, y = y, label = text), #Custom legend for unknown stock status
size = c(4.75,rep(4,3))) +
ggplot2::annotate(geom="text", x=0.43, y=5, label="ATL SBN (F/Fmsy = 22.5)",
color="#1B9E77")+
ggplot2::scale_color_brewer(palette = "Dark2", #Change legend labels for clarity
breaks = stock_status$score) +
ggplot2::xlab(expression(~B/B[msy])) +
ggplot2::ylab(expression(~F/F[msy])) +
ggplot2::guides(color = FALSE) +
ecodata::theme_ts()


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

# Paste commented original plot code chunk for reference
# y.max <- 5
# x.max <- 2
# #Get data, spread for plotting, and filter
# unknown <- data.frame(text = c("Unknown Status", "ATL SBH", "SPF", "WA BFT"),
# x = rep(0.9*x.max,4), y = seq(0.88*y.max,3.5,-0.3))
#
#
#
# stock_status<-ecodata::hms_stock_status %>%
# #tidyr::spread(.,Var,Value) %>%
# tidyr::separate(Var, c("species_abr", "spp", "Var"), ":") %>%
# tidyr::pivot_wider(names_from = Var, values_from = Value) %>%
# dplyr::group_by(spp) %>%
# dplyr::filter(!species_abr == "------",
# !species_abr == "-----") %>%
# dplyr::mutate(score = case_when(
# (B.Bmsy <0.5) ~"a",
# (B.Bmsy == 0.5) ~"a",
# (F.Fmsy == 1) ~ "a",
# (F.Fmsy >1) ~ "a",
# (F.Fmsy < 1 & B.Bmsy > 0.5 & B.Bmsy < 1) ~ "b",
# (F.Fmsy < 1 & B.Bmsy > 1) ~ "c"))
# #Plot constants
#
#
# #Plotting code
# ggplot2::ggplot(data = stock_status) +
# ggplot2::geom_vline(xintercept = 1, linetype = "dotted", color = "grey60")+
# ggplot2::geom_vline(xintercept = 0.5, linetype = "dashed", color = "grey60")+
# ggplot2::geom_hline(yintercept = 1, linetype = "dashed", color = "grey60") +
# ggplot2::geom_point(aes(x = B.Bmsy,
# y = F.Fmsy,
# color = stock_status$score)) +
# ggrepel::geom_text_repel(aes(x = B.Bmsy, #geom_text_repel auto-jitters text around points
# y = F.Fmsy,
# label = species_abr,
# color = stock_status$score), show.legend = FALSE,
# nudge_y = -0.01, nudge_x = 0.05) +
# ggplot2::ylim(0,y.max) +
# ggplot2::xlim(0,x.max*1.1) +
# ggplot2::geom_text(data = unknown, aes(x = x, y = y, label = text), #Custom legend for unknown stock status
# size = c(4.75,rep(4,3))) +
# ggplot2::annotate(geom="text", x=0.43, y=5, label="ATL SBN (F/Fmsy = 22.5)",
# color="#1B9E77")+
# ggplot2::scale_color_brewer(palette = "Dark2", #Change legend labels for clarity
# breaks = stock_status$score) +
# ggplot2::xlab(expression(~B/B[msy])) +
# ggplot2::ylab(expression(~F/F[msy])) +
# ggplot2::guides(color = FALSE) +
# ecodata::theme_ts()
#

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

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

0 comments on commit a789122

Please sign in to comment.