Skip to content

Commit

Permalink
plot function template, FWIW
Browse files Browse the repository at this point in the history
  • Loading branch information
sgaichas committed Nov 9, 2023
1 parent c92d6b0 commit 317fd4f
Showing 1 changed file with 102 additions and 0 deletions.
102 changes: 102 additions & 0 deletions R/plot_function_template.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#' plot <dataset name>
#'
#' <Insert description of plot function.>
#'
#' @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 ggplot object
#'
#'
#' @export
#'

plot_function_template <- 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
fix<- ecodata::dataset |>
dplyr::filter(Var %in% c("...",
"..."),
EPU %in% filterEPUs) |>
dplyr::group_by(EPU) |>
dplyr::summarise(max = max(Value))

# 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 <- ecodata::dataset |>
dplyr::filter(Var %in% c("..."),
EPU == "...") |>
#... more dataset wrangling as necessary |>
dplyr::left_join(fix) |>
dplyr::mutate(#
Value = Value,
#Mean = as.numeric(Mean),
#max = as.numeric(Value),
#Mean = Mean/max,
#SE = SE/max,
Upper = Mean + SE,
Lower = Mean - SE) |>
ggplot2::ggplot(ggplot2::aes(x = Time, y = Value))+
ggplot2::annotate("rect", fill = setup$shade.fill, alpha = setup$shade.alpha,
xmin = setup$x.shade.min , xmax = setup$x.shade.max,
ymin = -Inf, ymax = Inf) +
ggplot2::geom_ribbon(ggplot2::aes(ymin = Lower, ymax = Upper), alpha = 0.5)+
ggplot2::geom_point()+
ggplot2::geom_line()+
ggplot2::ggtitle("")+
ggplot2::ylab(expression("Indicator (units)"))+
ggplot2::xlab(element_blank())+
ggplot2::facet_wrap(.~EPU)+
ecodata::geom_gls()+
ecodata::theme_ts()+
ecodata::theme_facet()+
ecodata::theme_title()

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

}

return(p)

# Paste commented original plot code chunk for reference
# ecodata::dataset |>
# dplyr::filter(Var %in% c("..."),
# EPU == "...") |>
# ... more dataset wrangling as necessary |>
# 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("Title")+
# ggplot2::ylab(expression("Y label"))+
# ggplot2::xlab(element_blank())+
# ecodata::geom_gls()+
# ecodata::theme_ts()+
# ecodata::theme_title()
#
#

}

0 comments on commit 317fd4f

Please sign in to comment.