From 543fc4fdf37ffc83f0b3e8ab0f3e157844dbece1 Mon Sep 17 00:00:00 2001 From: andybeet <22455149+andybeet@users.noreply.github.com> Date: Tue, 24 Oct 2023 17:51:46 -0400 Subject: [PATCH] add plot_forage_index --- DESCRIPTION | 1 - NAMESPACE | 1 + R/plot_forage_index.R | 113 +++++++++++++++++++++++++++++++++++++++ man/plot_forage_index.Rd | 28 ++++++++++ 4 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 R/plot_forage_index.R create mode 100644 man/plot_forage_index.Rd diff --git a/DESCRIPTION b/DESCRIPTION index ee743167..aa95dfa0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -10,7 +10,6 @@ Encoding: UTF-8 LazyData: true Roxygen: list(markdown = TRUE) Date: 2018-10-24 - Imports: dplyr, magrittr, diff --git a/NAMESPACE b/NAMESPACE index 534af6fb..b916ba02 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -7,6 +7,7 @@ export(plot_bottom_temp_anomaly_glorys) export(plot_bottom_temp_anomaly_space) export(plot_bottom_temp_anomaly_survey) export(plot_bottom_temp_anomaly_time) +export(plot_forage_index) export(plot_sst) export(plot_sst_anomaly) export(plot_sst_transition_dates) diff --git a/R/plot_forage_index.R b/R/plot_forage_index.R new file mode 100644 index 00000000..3ce4972d --- /dev/null +++ b/R/plot_forage_index.R @@ -0,0 +1,113 @@ +#' plot forage index +#' +#'@param report. Character string. Which SOE report ("G) +#' @param shadedRegion Numeric vector. Years denoting the shaded region of the plot +#' @param shade.fill Character string. Color of shaded region. (Default = "lightgrey) +#' @param shade.alpha Numeric scalar. Alpha of shaded region (Default = 0.5) +#' +#' @return ggplot object +#' +#' +#' @export +#' + +plot_forage_index <- function(EPUs="MAB", + shadedRegion=c(2012,2022), + shade.fill="lightgrey", + shade.alpha=0.3) { + + + x.shade.min <- shadedRegion[1] + x.shade.max <- shadedRegion[2] + + if (EPUs == "GB") { + filterEPUs <- c("GOM", "GB") + } else { + filterEPUs <- EPUs + } + + + fix<- ecodata::forage_index %>% + dplyr::filter(Var %in% c("Fall Forage Fish Biomass Estimate", + "Spring Forage Fish Biomass Estimate"), + EPU %in% filterEPUs) %>% + dplyr::group_by(EPU) %>% + dplyr::summarise(max = max(Value)) + + p <- ecodata::forage_index %>% + dplyr::filter(Var %in% c("Fall Forage Fish Biomass Estimate", + "Fall Forage Fish Biomass Estimate SE", + "Spring Forage Fish Biomass Estimate", + "Spring Forage Fish Biomass Estimate SE"), + EPU %in% filterEPUs) %>% + dplyr::group_by(EPU) %>% + tidyr::separate(Var, into = c("Season", "A", "B", "C", "D", "Var")) %>% + dplyr::mutate(Var = tidyr::replace_na(Var, "Mean")) %>% #, + #max = as.numeric(Value)) %>% + tidyr::pivot_wider(names_from = Var, values_from = Value) %>% + dplyr::left_join(fix) %>% + dplyr::mutate(#Value = Value/resca, + Mean = as.numeric(Mean), + #max = as.numeric(Value), + Mean = Mean/max, + SE = SE/max, + Upper = Mean + SE, + Lower = Mean - SE) %>% + ggplot2::ggplot(aes(x = Time, y = Mean, group = Season))+ + ggplot2::annotate("rect", fill = shade.fill, alpha = shade.alpha, + xmin = x.shade.min , xmax = x.shade.max, + ymin = -Inf, ymax = Inf) + + ggplot2::geom_ribbon(aes(ymin = Lower, ymax = Upper, fill = Season), alpha = 0.5)+ + ggplot2::geom_point()+ + ggplot2::geom_line()+ + ggplot2::ggtitle("")+ + ggplot2::ylab(expression("Relative forage biomass"))+ + ggplot2::xlab(element_blank())+ + ggplot2::facet_wrap(.~EPU)+ + ecodata::geom_gls()+ + ecodata::theme_ts()+ + ecodata::theme_facet()+ + ecodata::theme_title() + + if (EPUs == "GB") { + p <- p + + ggplot2::theme(legend.position = "bottom", + legend.title = element_blank()) + + } + + return(p) + + # ecodata::forage_index %>% + # dplyr::filter(Var %in% c("Fall Forage Fish Biomass Estimate", + # "Fall Forage Fish Biomass Estimate SE", + # "Spring Forage Fish Biomass Estimate", + # "Spring Forage Fish Biomass Estimate SE"), + # EPU == "MAB") %>% + # tidyr::separate(Var, into = c("Season", "A", "B", "C", "D", "Var")) %>% + # dplyr::mutate(Var = replace_na(Var, "Mean"), + # max = as.numeric(resca)) %>% + # tidyr::pivot_wider(names_from = Var, values_from = Value) %>% + # dplyr::mutate(#Value = Value/resca, + # Mean = as.numeric(Mean), + # Mean = Mean/max, + # SE = SE/max, + # Upper = Mean + SE, + # Lower = Mean - SE) %>% + # ggplot2::ggplot(aes(x = Time, y = Mean, group = Season))+ + # ggplot2::annotate("rect", fill = shade.fill, alpha = shade.alpha, + # xmin = x.shade.min , xmax = x.shade.max, + # ymin = -Inf, ymax = Inf) + + # ggplot2::geom_ribbon(aes(ymin = Lower, ymax = Upper, fill = Season), alpha = 0.5)+ + # ggplot2::geom_point()+ + # ggplot2::geom_line()+ + # ggplot2::ggtitle("Forage Biomass Index")+ + # ggplot2::ylab(expression("Relative forage biomass"))+ + # ggplot2::xlab(element_blank())+ + # ecodata::geom_gls()+ + # ecodata::theme_ts()+ + # ecodata::theme_title() + # + # + +} diff --git a/man/plot_forage_index.Rd b/man/plot_forage_index.Rd new file mode 100644 index 00000000..4b62ae8c --- /dev/null +++ b/man/plot_forage_index.Rd @@ -0,0 +1,28 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/plot_forage_index.R +\name{plot_forage_index} +\alias{plot_forage_index} +\title{plot forage index} +\usage{ +plot_forage_index( + EPUs = "MAB", + shadedRegion = c(2012, 2022), + shade.fill = "lightgrey", + shade.alpha = 0.3 +) +} +\arguments{ +\item{shadedRegion}{Numeric vector. Years denoting the shaded region of the plot} + +\item{shade.fill}{Character string. Color of shaded region. (Default = "lightgrey)} + +\item{shade.alpha}{Numeric scalar. Alpha of shaded region (Default = 0.5)} + +\item{report.}{Character string. Which SOE report ("G)} +} +\value{ +ggplot object +} +\description{ +plot forage index +}