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 yellowfin tuna in the eastern Pacific Ocean.

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

YFT.OBJ.Catch.20002022 <- read.csv(paste0(save_dir,"YFT.OBJ.Catch.20002022.csv"))
YFT.OBJ.Catch.19751999 <- read.csv(paste0(save_dir,"YFT.OBJ.Catch.19751999.csv"))

YFT.NOA.Catch.20002022 <- read.csv(paste0(save_dir,"YFT.NOA.Catch.20002022.csv"))
YFT.NOA.Catch.19751999 <- read.csv(paste0(save_dir,"YFT.NOA.Catch.19751999.csv"))

YFT.DEL.Catch.20002022 <- read.csv(paste0(save_dir,"YFT.DEL.Catch.20002022.csv"))
YFT.DEL.Catch.19751999 <- read.csv(paste0(save_dir,"YFT.DEL.Catch.19751999.csv"))
Year_OBJ <- data.frame(Year = seq(1,(yr.end-1974)*4),
                       Area = rep(c("A1","A2","A3","A4","A5"), each = (yr.end-1974)*4))

YFT_OBJ_Catch <- rbind(YFT.OBJ.Catch.19751999,YFT.OBJ.Catch.20002022) %>% 
  mutate(Year=(year-1975)*4+quarter) %>%
  gather(3:7,key="Area",value="Catch") %>% 
  select(Year,Area,Catch)
YFT_OBJ_Catch <- left_join(Year_OBJ,YFT_OBJ_Catch) %>%
  mutate(Catch=ifelse(is.na(Catch),0,Catch),
         Type="OBJ")

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

Year_DEL <- data.frame(Year = seq(1,(yr.end-1974)*4),
                       Area = rep(c("A1","A2","A3","A4","A5","A6","A7"), each = (yr.end-1974)*4))
YFT_DEL_Catch <- rbind(YFT.DEL.Catch.19751999,YFT.DEL.Catch.20002022) %>% 
  mutate(Year=(year-1975)*4+quarter) %>%
  gather(3:9,key="Area",value="Catch") %>% 
  select(Year,Area,Catch)
YFT_DEL_Catch <- left_join(Year_DEL,YFT_DEL_Catch) %>%
  mutate(Catch=ifelse(is.na(Catch),0,Catch),
         Type="DEL")

YFT_PS_Catch <- rbind(YFT_OBJ_Catch,YFT_NOA_Catch,YFT_DEL_Catch)
write.csv(YFT_PS_Catch,file=paste0(save_dir,"YFT_PS_Catch_1975-",yr.end,".csv"),row.names = FALSE)
ggplot(data=YFT_PS_Catch) +
  geom_line(aes(x=Year,y=Catch,color=Area)) +
  facet_wrap(~Type,nrow=5,scales = "free") +
  geom_vline(xintercept = 100.5,linetype = "dashed") +
  theme_bw(16)
YFT.OBJ.Comp.20002022 <- read.csv(paste0(save_dir,"YFT.OBJ.Comp.20002022.csv"))
YFT.OBJ.Comp.19751999 <- read.csv(paste0(save_dir,"YFT.OBJ.Comp.19751999.csv"))

YFT.NOA.Comp.20002022 <- read.csv(paste0(save_dir,"YFT.NOA.Comp.20002022.csv"))
YFT.NOA.Comp.19751999 <- read.csv(paste0(save_dir,"YFT.NOA.Comp.19751999.csv"))

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

YFT_NOA_Comp <- rbind(YFT.NOA.Comp.19751999,YFT.NOA.Comp.20002022) %>%
    mutate(Year=(year-1975)*4+quarter, Type="NOA") %>%
  arrange(area,Year)
YFT_NOA_Comp <- YFT_NOA_Comp[c(207,206,3:205)]

YFT_DEL_Comp <- rbind(YFT.DEL.Comp.19751999,YFT.DEL.Comp.20002022) %>%
    mutate(Year=(year-1975)*4+quarter, Type="DEL") %>%
  arrange(area,Year)
YFT_DEL_Comp <- YFT_DEL_Comp[c(207,206,3:205)]

YFT_PS_Comp <- rbind(YFT_OBJ_Comp,YFT_NOA_Comp,YFT_DEL_Comp)
write.csv(YFT_PS_Comp,file=paste0(save_dir,"YFT_PS_Comp_1975-",yr.end,".csv"),row.names = FALSE)
names(YFT_PS_Comp)[5:205] <- 1:201
YFT_PS_Comp_mean <- YFT_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=YFT_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.