afsc-gap-products / gap_bs_data_report

Reproducible and annotated code for developing the EBS and NBS Annual Data report and presentation.
9 stars 2 forks source link

Creating a multi-panel temperature plot with EBS and NBS regions on the same panel #8

Closed sean-rohan-NOAA closed 1 year ago

sean-rohan-NOAA commented 1 year ago

Code for combining temperature from EBS-only years with temperature from EBS+NBS years in the same rasterbrick. Would slot-in around here: https://github.com/EmilyMarkowitz-NOAA/gap_bs_data_report/blob/861560b2bba6def981309652ba1c64c9a4effa35/code/figtab.Rmd#L579-L593

# Setup for working example 
library(coldpool)
library(akgfmaps)
reg_dat <- akgfmaps::get_base_layers(select.region = "ebs", set.crs = "EPSG:3338")
maxyr <- 2022
yrs <- (maxyr-19):maxyr 

# CODE STARTS HERE #

# Names of EBS layers for years without NBS data
sebs_only <- names(coldpool::ebs_bottom_temperature)[which(!(names(coldpool::ebs_bottom_temperature) %in% paste0("s", names(coldpool::nbs_ebs_bottom_temperature))))]

# Select EBS years to include in rasterbrick and reproject EBS years to the extent and resolution of the EBS+NBS grid
sebs_temp <- raster::subset(coldpool::ebs_bottom_temperature,
                            subset = sebs_only) %>%
  raster::projectRaster(coldpool::nbs_ebs_bottom_temperature)

# Combine EBS and NBS layers into the same rasterbrick
rasterbrick <- raster::addLayer(sebs_temp, coldpool::nbs_ebs_bottom_temperature)

# Remove non-numeric characters from layer names to simplify subsetting based on the year range
names(rasterbrick) <- gsub("[^0-9.-]", "", names(rasterbrick))

# Subset layers for the selected years
rasterbrick <- raster::subset(rasterbrick, subset = paste0("X", yrs))

In the plotting function, remove NA values from the reprojected extent: https://github.com/EmilyMarkowitz-NOAA/gap_bs_data_report/blob/861560b2bba6def981309652ba1c64c9a4effa35/code/functions.R#L2372-L2375

with:

  temp_df <- temp_df %>% 
    tidyr::pivot_longer(values_to = "value", 
                        names_to = "year", 
                        cols = dplyr::all_of(temp1)) %>%
    tidyr::drop_na(value)
EmilyMarkowitz-NOAA commented 1 year ago

Completed! Thanks for your insight and helpful code, @sean-rohan-NOAA!