knitr::opts_chunk$set(warning = FALSE, message = FALSE)

This example code demonstrates how to compile the purse-seine catch and length composition data for the stock assessment of skipjack tuna in the eastern Pacific Ocean.

library(tidyverse)
save_dir <- "D:/OneDrive - IATTC/IATTC/2022/BSE stuff from Cleridy/SKJ/"
yr.end <- 2021

SKJ.OBJ.Catch.20002021 <- read.csv(paste0(save_dir,"SKJ.OBJ.Catch.20002021.csv"))

SKJ.NOA.Catch.20002021 <- read.csv(paste0(save_dir,"SKJ.NOA.Catch.20002021.csv"))

SKJ.DEL.Catch.20002021 <- read.csv(paste0(save_dir,"SKJ.DEL.Catch.20002021.csv"))
Year_OBJ <- data.frame(Year = seq(101,(yr.end-1974)*4),
                       Area = rep(c("A1","A2","A3","A4"), each = (yr.end-1999)*4))

SKJ_OBJ_Catch <- SKJ.OBJ.Catch.20002021 %>% 
  mutate(Year=(year-1975)*4+quarter) %>%
  gather(3:6,key="Area",value="Catch") %>% 
  select(Year,Area,Catch)
SKJ_OBJ_Catch <- left_join(Year_OBJ,SKJ_OBJ_Catch) %>%
  mutate(Catch=ifelse(is.na(Catch),0,Catch),
         Type="OBJ")

Year_NOA <- data.frame(Year = seq(101,(yr.end-1974)*4),
                       Area = rep(c("A1","A2","A3","A4"), each = (yr.end-1999)*4))
SKJ_NOA_Catch <- SKJ.NOA.Catch.20002021 %>% 
  mutate(Year=(year-1975)*4+quarter) %>%
  gather(3:6,key="Area",value="Catch") %>% 
  select(Year,Area,Catch)
SKJ_NOA_Catch <- left_join(Year_NOA,SKJ_NOA_Catch) %>%
  mutate(Catch=ifelse(is.na(Catch),0,Catch),
         Type="NOA")

Year_DEL <- data.frame(Year = seq(101,(yr.end-1974)*4),
                       Area = rep(c("A1","A2"), each = (yr.end-1999)*4))
SKJ_DEL_Catch <- SKJ.DEL.Catch.20002021 %>% 
  mutate(Year=(year-1975)*4+quarter) %>%
  gather(3:4,key="Area",value="Catch") %>% 
  select(Year,Area,Catch)
SKJ_DEL_Catch <- left_join(Year_DEL,SKJ_DEL_Catch) %>%
  mutate(Catch=ifelse(is.na(Catch),0,Catch),
         Type="DEL")

SKJ_PS_Catch <- rbind(SKJ_OBJ_Catch,SKJ_NOA_Catch,SKJ_DEL_Catch)
write.csv(SKJ_PS_Catch,file=paste0(save_dir,"SKJ_PS_Catch_1975-",yr.end,".csv"),row.names = FALSE)
ggplot(data=SKJ_PS_Catch) +
  geom_line(aes(x=Year,y=Catch,color=Area)) +
  facet_wrap(~Type,nrow=4,scales = "free") +
  theme_bw(16)
SKJ.OBJ.Comp.20002021 <- read.csv(paste0(save_dir,"SKJ.OBJ.Comp.20002021.csv"))

SKJ.NOA.Comp.20002021 <- read.csv(paste0(save_dir,"SKJ.NOA.Comp.20002021.csv"))

SKJ.DEL.Comp.20002021 <- read.csv(paste0(save_dir,"SKJ.DEL.Comp.20002021.csv"))
SKJ_OBJ_Comp <- SKJ.OBJ.Comp.20002021 %>%
    mutate(Year=(year-1975)*4+quarter, Type="OBJ") %>%
  arrange(area,Year)
SKJ_OBJ_Comp <- SKJ_OBJ_Comp[c(207,206,3:205)]

SKJ_NOA_Comp <- SKJ.NOA.Comp.20002021 %>%
    mutate(Year=(year-1975)*4+quarter, Type="NOA") %>%
  arrange(area,Year)
SKJ_NOA_Comp <- SKJ_NOA_Comp[c(207,206,3:205)]

SKJ_DEL_Comp <- SKJ.DEL.Comp.20002021 %>%
    mutate(Year=(year-1975)*4+quarter, Type="DEL") %>%
  arrange(area,Year)
SKJ_DEL_Comp <- SKJ_DEL_Comp[c(207,206,3:205)]

SKJ_PS_Comp <- rbind(SKJ_OBJ_Comp,SKJ_NOA_Comp,SKJ_DEL_Comp)
write.csv(SKJ_PS_Comp,file=paste0(save_dir,"SKJ_PS_Comp_1975-",yr.end,".csv"),row.names = FALSE)
names(SKJ_PS_Comp)[5:205] <- 1:201
SKJ_PS_Comp_mean <- SKJ_PS_Comp %>%
  gather(5:205,key="Length",value=comp) %>%
  group_by(Type,area,Length) %>%
  summarise(Comp=sum(comp*nwells)) %>%
  group_by(Type,area) %>%
  mutate(Length=as.numeric(Length),Comp=Comp/sum(Comp))

ggplot(data=SKJ_PS_Comp_mean) +
  geom_line(aes(x=Length,y=Comp,color=area)) +
  facet_wrap(~Type,nrow = 3) +
  theme_bw(16)


HaikunXu/BSE documentation built on Nov. 22, 2024, 8:22 a.m.