|
12 | 12 | #' @param proj_path Local path to data file |
13 | 13 | #' @return Data frame of observer data; contains both catch and haul information. |
14 | 14 | #' @export |
15 | | -#' @examples # nefsc <- pull_nefsc(proj_path = "~Data/trawl_dat.rds") |
| 15 | +#' @examples # nefsc <- pull_nefsc(proj_path = proj_path) |
16 | 16 |
|
17 | 17 | # Load and preliminary cleaning of raw data ---- |
18 | 18 | pull_nefsc <- function(proj_path){ |
@@ -478,3 +478,101 @@ map_nefsc <- function(species = "all", data = NULL){ |
478 | 478 | } |
479 | 479 | } |
480 | 480 |
|
| 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 | +} |
0 commit comments