Skip to content

Commit

Permalink
think this works now
Browse files Browse the repository at this point in the history
  • Loading branch information
sgaichas committed Nov 2, 2023
1 parent 34e14ba commit cc03ffa
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 67 deletions.
66 changes: 29 additions & 37 deletions R/plot_forage_index.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,56 @@
#'
#' Description should be here. This needs to be reworked to uncouple GB and GOM
#'
#' @param EPUs Character string. Which SOE report ("GB","MAB")
#' @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)
#' @param report Character string. Which SOE report ("MidAtlantic", "NewEngland")
#'
#' @return ggplot object
#'
#'
#' @export
#'

plot_forage_index <- function(EPUs="MAB",
shadedRegion=c(2012,2022),
shade.fill="lightgrey",
shade.alpha=0.3) {
plot_forage_index <- function(report="MidAtlantic") {

setup <- ecodata::plot_setup(report=report)

x.shade.min <- shadedRegion[1]
x.shade.max <- shadedRegion[2]

if (EPUs == "GB") {
filterEPUs <- c("GOM", "GB")
if (report == "MidAtlantic") {
filterEPUs <- c("MAB")
} else {
filterEPUs <- EPUs
filterEPUs <- c("GB", "GOM")
}


fix<- ecodata::forage_index %>%
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) %>%
EPU %in% filterEPUs) |>
dplyr::group_by(EPU) |>
dplyr::summarise(max = max(Value))

p <- ecodata::forage_index %>%
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) %>%
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,
Lower = Mean - SE) |>
ggplot2::ggplot(ggplot2::aes(x = Time, y = Mean, group = Season))+
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(aes(ymin = Lower, ymax = Upper, fill = Season), alpha = 0.5)+
ggplot2::geom_ribbon(ggplot2::aes(ymin = Lower, ymax = Upper, fill = Season), alpha = 0.5)+
ggplot2::geom_point()+
ggplot2::geom_line()+
ggplot2::ggtitle("")+
Expand All @@ -71,7 +63,7 @@ plot_forage_index <- function(EPUs="MAB",
ecodata::theme_facet()+
ecodata::theme_title()

if (EPUs == "GB") {
if (report == "NewEngland") {
p <- p +
ggplot2::theme(legend.position = "bottom",
legend.title = element_blank())
Expand All @@ -80,22 +72,22 @@ plot_forage_index <- function(EPUs="MAB",

return(p)

# ecodata::forage_index %>%
# 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")) %>%
# 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) %>%
# 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) %>%
# 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,
Expand Down
93 changes: 63 additions & 30 deletions R/plot_setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,52 +15,50 @@
plot_setup <- function(shadedRegion=c(2014,2023),
report = report){

plotsettings <- list(
plotsettings <- list()

# from GIS-setup files (identical between reports)
#CRS
crs <- "+proj=longlat +lat_1=35 +lat_2=45 +lat_0=40 +lon_0=-77 +x_0=0 +y_0=0 +datum=NAD83 +no_defs +ellps=GRS80 +towgs84=0,0,0",
crs <- "+proj=longlat +lat_1=35 +lat_2=45 +lat_0=40 +lon_0=-77 +x_0=0 +y_0=0 +datum=NAD83 +no_defs +ellps=GRS80 +towgs84=0,0,0"

#EPU shapefile
epu_sf <- ecodata::epu_sf %>%
filter(EPU %in% c("MAB","GB","GOM")),
epu_sf <- ecodata::epu_sf |>
dplyr::filter(EPU %in% c("MAB","GB","GOM"))

#Map line parameters
map.lwd <- 0.4,
map.lwd <- 0.4

# Set lat/lon window for maps
xmin = -77,
xmax = -65,
ymin = 36,
ymax = 45,
xlims <- c(xmin, xmax),
ylims <- c(ymin, ymax),
xmin = -77
xmax = -65
ymin = 36
ymax = 45

#Time series constants
shade.alpha <- 0.3,
shade.fill <- "lightgrey",
lwd <- 1,
pcex <- 2,
trend.alpha <- 0.5,
trend.size <- 2,
hline.size <- 1,
hline.alpha <- 0.35,
hline.lty <- "dashed",
label.size <- 5,
hjust.label <- 1.5,
letter_size <- 4,
feeding.guilds1<- c("Piscivore","Planktivore","Benthivore","Benthos"),
feeding.guilds <- c("Apex Predator","Piscivore","Planktivore","Benthivore","Benthos"),
x.shade.min <- shadedRegion[1],
x.shade.max <- shadedRegion[2],
shade.alpha <- 0.3
shade.fill <- "lightgrey"
lwd <- 1
pcex <- 2
trend.alpha <- 0.5
trend.size <- 2
hline.size <- 1
hline.alpha <- 0.35
hline.lty <- "dashed"
label.size <- 5
hjust.label <- 1.5
letter_size <- 4
feeding.guilds1<- c("Piscivore","Planktivore","Benthivore","Benthos")
feeding.guilds <- c("Apex Predator","Piscivore","Planktivore","Benthivore","Benthos")
x.shade.min <- shadedRegion[1]
x.shade.max <- shadedRegion[2]

#Define constants for figure plot
series.col <- c("indianred","black"),
series.col <- c("indianred","black")

#Function for custom ggplot facet labels
label <- function(variable,value){
return(facet_names[value])
},
}

# from human dimensions setup files, region specific
# specify report as argument when calling individual plot functions
Expand Down Expand Up @@ -95,7 +93,42 @@ plot_setup <- function(shadedRegion=c(2014,2023),

#GIS directory
gis.dir <- here::here("data-raw","gridded")
)

plotsettings <- list(crs = crs,
epu_sf = epu_sf,
map.lwd = map.lwd,
xmin = xmin,
xmax = xmax,
ymin = ymin,
ymax = ymax,
xlims = c(xmin, xmax),
ylims = c(ymin, ymax),
shade.alpha = shade.alpha,
shade.fill =shade.fill,
lwd = lwd,
pcex = pcex,
trend.alpha = trend.alpha,
trend.size = trend.size,
hline.size = hline.size,
hline.alpha = hline.alpha,
hline.lty = hline.lty,
label.size = label.size,
hjust.label = hjust.label,
letter_size = letter_size,
feeding.guilds1 = feeding.guilds1,
feeding.guilds = feeding.guilds,
x.shade.min = x.shade.min,
x.shade.max = x.shade.max,
series.col = series.col,
label = label,
council = council,
council_abbr = council_abbr,
epu = epu,
epu_abbr = epu_abbr,
region = region,
region_abbr = region_abbr,
gis.dir = gis.dir
)

return(plotsettings)

Expand Down

0 comments on commit cc03ffa

Please sign in to comment.