Skip to content

Commit ab63565

Browse files
author
Carly Lovas
committed
biomass proportions
1 parent e46d119 commit ab63565

3 files changed

Lines changed: 111 additions & 12 deletions

File tree

R/garfo_landings.R

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,14 @@ plot_landings_trends <- function(species = "all", data = "landings") {
9494
# Build plots only for relevant species
9595
plots <- data |>
9696
dplyr::rename("landings" = "land") |>
97+
dplyr::rename("revenue" = "value")
9798
tidyr::pivot_longer(
98-
cols = c(landings, value),
99+
cols = c(landings, revenue),
99100
names_to = "metric",
100101
values_to = "value"
101102
) |>
102103
dplyr::group_by(year, comname, state_full, metric) |>
103-
dplyr::summarise(total = sum(value), .groups = "drop") |>
104+
dplyr::summarise(total = sum(revenue), .groups = "drop") |>
104105
dplyr::group_by(comname) |>
105106
tidyr::nest() |>
106107
dplyr::mutate(
@@ -177,16 +178,16 @@ plot_state_landings <- function(species = "all", data = "landings") {
177178
plots <- data |>
178179
dplyr::rename("landings" = "land") |>
179180
tidyr::pivot_longer(
180-
cols = c(landings, value),
181+
cols = c(landings, revenue),
181182
names_to = "metric",
182183
values_to = "value"
183184
) |>
184185
dplyr::group_by(year, comname, state_full, metric) |>
185-
dplyr::summarise(state_value = sum(value, na.rm = T), .groups = "drop") |>
186+
dplyr::summarise(state_revenue = sum(revenue, na.rm = T), .groups = "drop") |>
186187
dplyr::ungroup() |>
187188
dplyr::group_by(year, comname, metric) |>
188-
dplyr::mutate(total_value = sum(state_value, na.rm = T),
189-
prop = state_value / total_value) |>
189+
dplyr::mutate(total_revenue = sum(state_revenue, na.rm = T),
190+
prop = state_revenue / total_revenue) |>
190191
dplyr::group_by(comname) |>
191192
tidyr::nest() |>
192193
dplyr::mutate(
@@ -267,16 +268,16 @@ plot_council_landings <- function(species = "all", data = "landings") {
267268
plots <- data |>
268269
dplyr::rename("landings" = "land") |>
269270
tidyr::pivot_longer(
270-
cols = c(landings, value),
271+
cols = c(landings, revenue),
271272
names_to = "metric",
272273
values_to = "value"
273274
) |>
274275
dplyr::group_by(year, comname, council, metric) |>
275-
dplyr::summarise(council_value = sum(value, na.rm = T), .groups = "drop") |>
276+
dplyr::summarise(council_revenue = sum(revenue, na.rm = T), .groups = "drop") |>
276277
dplyr::ungroup() |>
277278
dplyr::group_by(year, comname, metric) |>
278-
dplyr::mutate(total_value = sum(council_value, na.rm = T),
279-
prop = council_value / total_value) |>
279+
dplyr::mutate(total_revenue = sum(council_revenue, na.rm = T),
280+
prop = council_revenue / total_revenue) |>
280281
dplyr::group_by(comname) |>
281282
tidyr::nest() |>
282283
dplyr::mutate(

R/nefsc.R

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#' @param proj_path Local path to data file
1313
#' @return Data frame of observer data; contains both catch and haul information.
1414
#' @export
15-
#' @examples # nefsc <- pull_nefsc(proj_path = "~Data/trawl_dat.rds")
15+
#' @examples # nefsc <- pull_nefsc(proj_path = proj_path)
1616

1717
# Load and preliminary cleaning of raw data ----
1818
pull_nefsc <- function(proj_path){
@@ -478,3 +478,101 @@ map_nefsc <- function(species = "all", data = NULL){
478478
}
479479
}
480480

481+
## Biomass proportions across Councils
482+
#' @title Council proportions of biomass
483+
#'
484+
#' @description Function to calculate and plot the proportion of surveyed biomass in each council region
485+
#' @param species Default is "all", includes Mid-Atlantic species represented in `species.shift::species_list()`
486+
#' @param data Default is "nefsc" `nefsc` must be run and named "observer" in order to run this function.
487+
#' @return Barplot of biomass proportions across Atlantic council management zones. Selecting `all` species will return a list.
488+
#' @export
489+
#' @examples # plot_council_biomass(species = "summer flounder", data = nefsc)
490+
#'
491+
plot_council_biomass <- function(species = "all", data = NULL){
492+
493+
# Get species list
494+
species_list <- species.shifts::species_list(source = "nefsc")
495+
496+
# Base filter
497+
data <- data |>
498+
dplyr::right_join(species_list)
499+
500+
# Validate and filter early if specific species requested
501+
if (species != "all") {
502+
if (!species %in% data$clean_name) {
503+
message("Species '", species, "' not found.")
504+
return(NULL)
505+
}
506+
data <- data |> dplyr::filter(clean_name == species)
507+
}
508+
509+
# Spatial goodies
510+
sf::sf_use_s2(FALSE)
511+
512+
shp_path <- here::here("data", "shapefiles", "Council_Scopes.shp")
513+
514+
boundaries <- sf::st_read(shp_path, quiet = TRUE)
515+
boundaries <- ggplot2::fortify(boundaries)
516+
517+
east_coast <- boundaries |>
518+
janitor::clean_names() |>
519+
dplyr::filter(council %in% c("New England", "Mid-Atlantic", "South Atlantic")) |>
520+
dplyr::mutate(factor = factor(council, levels = c("New England", "Mid-Atlantic", "South Atlantic")))
521+
522+
sf_data <- sf::st_as_sf(data, coords = c("lon", "lat"), crs = 4326, remove = FALSE)
523+
524+
# Overlap with Mgmt zones
525+
sf_data <- sf_data |>
526+
sf::st_join(east_coast, join = sf::st_intersects)
527+
528+
# Calculate & plot biomass proportions
529+
plots <- sf_data |>
530+
sf::st_drop_geometry() |>
531+
dplyr::filter(!is.na(council)) |>
532+
dplyr::mutate(council = factor(council, levels = c("New England", "Mid-Atlantic", "South Atlantic"))) |>
533+
dplyr::select(clean_name, year, lat, lon, total_biomass_kg, council) |>
534+
dplyr::group_by(year, clean_name, council) |>
535+
dplyr::summarise(biom = sum(total_biomass_kg, na.rm = T), .groups = "drop") |>
536+
dplyr::group_by(year, clean_name) |>
537+
dplyr::mutate(total = sum(biom, na.rm = T), .groups = "drop",
538+
prop = (biom/total)) |>
539+
dplyr::group_by(clean_name) |>
540+
tidyr::nest() |>
541+
dplyr::mutate(
542+
out = purrr::map2(data, clean_name, function(x, y) {
543+
ggplot2::ggplot(data = x) +
544+
ggplot2::geom_col(
545+
ggplot2::aes(x = year, y = prop, fill = council)
546+
) +
547+
ggplot2::scale_fill_manual(values = c("#363b45", "#00608a","#C1DEFF")) +
548+
ggplot2::guides(
549+
fill = ggplot2::guide_legend(nrow = 1)
550+
) +
551+
ggplot2::labs(
552+
title = "Proportion of biomass by council",
553+
x = "Year",
554+
y = "Proportion"
555+
) +
556+
ggplot2::theme(
557+
text = ggplot2::element_text(family = "Avenir", size = 13),
558+
legend.title = ggplot2::element_blank(),
559+
legend.position = "bottom",
560+
strip.background = ggplot2::element_blank(),
561+
strip.text = ggplot2::element_text(hjust = 0, face = "plain", size = 15),
562+
panel.grid.major = ggplot2::element_line(color = "#535353", linewidth = 0.1, linetype = 3),
563+
panel.grid.minor = ggplot2::element_blank(),
564+
panel.background = ggplot2::element_rect(fill = "transparent"),
565+
panel.border = ggplot2::element_rect(
566+
fill = "transparent",
567+
linetype = 1,
568+
linewidth = 0.5,
569+
color = "#535353"
570+
)
571+
)
572+
}))
573+
if (species == "all") {
574+
return(plots |> dplyr::select(clean_name, out))
575+
} else {
576+
return(plots$out[[1]])
577+
}
578+
}

man/pull_nefsc.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)