library(dplyr)
library(ggplot2)
library(tidyverse)
library(patchwork)
source(here::here('coord_conversion.R'))
# Setting up color palettes 
pal.m = hcl.colors(5, palette = 'Dark Mint')
# scales::show_col(pal.m) # view color pallete
pal.f.full = hcl.colors(5, palette = 'BurgYl')
pal.f = pal.f.full[c(1,2,4)] # just playilng with colors bc there are only 3 stages
# scales::show_col(pal.f)
bold_pal <- rcartocolor::carto_pal(12, 'Bold')

world <- ggplot2::map_data('world') 

# Data 
squibs <- read.csv(here::here('data/squibs/2024-11-01_weekly_SQUIBS_pull.csv'))

squibs <- squibs %>% rename_all(., .funs = tolower) %>%
  rename(age.d = 'increment.count.1') %>% 
  mutate(lat = convertcoord(start_haul_lat),
         lon = convertcoord(start_haul_lon), 
         season = case_when(month %in% c(1,2,3) ~ 'winter', 
                            month %in% c(4,5,6) ~ 'spring', 
                            month %in% c(7,8,9) ~ 'summer', 
                            month %in% c(10,11,12) ~ 'fall'), 
         month.n = month.name[month], 
         age.m = round(age.d/30), 
         age.col = case_when(age.m == 0 ~ bold_pal[1], 
                             age.m == 3 ~ bold_pal[3], 
                             age.m == 4 ~ bold_pal[4], 
                             age.m == 5 ~ bold_pal[5],
                             age.m == 6 ~ bold_pal[6],
                             age.m == 7 ~ bold_pal[7],
                             age.m == 8 ~ bold_pal[8],
                             age.m == 9 ~ bold_pal[9],
                             age.m == 10 ~ bold_pal[10],
                             age.m == 11 ~ bold_pal[11],
                             age.m == 12 ~ bold_pal[12])) 

df <- squibs
# Sets up scatterpie for MATURITY
df.pie <- df %>% dplyr::select(tally_no, year, month, sex, 
                               maturity_stage, age.d, lat, lon) %>%
  group_by(tally_no,lat, lon, year, month, sex, maturity_stage) %>%
  tally() %>% 
  mutate(prop = n / sum(n), 
         labels = scales::percent(prop)) 

df.pie.w <- df.pie %>% tidyr::pivot_wider(names_from = maturity_stage, 
                                          values_from = prop)
df.pie.w <- df.pie.w[,-10] # remove NA column
df.pie.year <- df.pie.w %>%  filter(!is.na(lon)) %>%  # replace NAs in location
  mutate_at(c(9:13), ~replace(., is.na(.), 0))
# replace NAs in maturity stage with 0.00 for plotting



# Sets up scatterpie for AGES 
df.pie <- df %>% dplyr::select(tally_no, year, month, sex, 
                                age.m, lat, lon, age.col) %>%
  group_by(tally_no,lat, lon, year, month, sex, age.m, age.col) %>%
  tally() %>% 
  mutate(prop = n / sum(n), 
         labels = scales::percent(prop)) 

df.pie.age <- df.pie %>% tidyr::pivot_wider(names_from = age.m, 
                                          values_from = prop)
df.pie.age <- df.pie.age[,-12] # remove NA column
df.pie.age <- df.pie.age %>%  filter(!is.na(lon)) %>%  # replace NAs in location
  mutate_at(c(10:17), ~replace(., is.na(.), 0)) %>% 
  rename('1' = '0') 


# Set up map

p <- ggplot(data = world, aes(x=long, y=lat, group=group)) + 
  geom_polygon(fill = "darkseagreen", color = "black") + 
  coord_quickmap(xlim = c(-76,-70), ylim = c(37, 43)) +
  ylab("Latitude") + 
  xlab("Longitude") + 
  theme(
    panel.background = element_rect(fill = "lightsteelblue2"),
    panel.grid.minor = element_blank(), 
    panel.grid.major = element_line(colour = "grey90", size = 0.5), 
    legend.position = "top")

Background

From Dave Richardson: “For this conceptual diagram we assumed that individuals have about a month long egg stage and then a lifespan of about 7-month after hatching, and thus three generations of squid take 24 months. The figures have a monthly location (circle color) and a presumed age of the individual in months (white number in the circle, with E=Egg stage and S=Spawning).
To reiterate, the patterns here are not meant to encompass the whole range of squid distribution and spawning, which includes Georges Bank and the western Gulf of Maine. Rather, the figure is meant to present a hypothesis about inter-generational movement patterns that are consistent with the available data. We contend that a conceptual diagram, consistent with the data, can not made that excludes the continental shelf south of Cape Hatteras.”

knitr::include_graphics(here::here(paste0("figures/concept_mod.png")))

Interannual Comparisons

The following figures are plots of the proportion of maturity stages and ages for SQUIBS longfin squid samples processed in a given month. Maturity maps are generated monthly for each year for the year-month combinations we have available (May:October). Age maps are generated for subset of squid that have been aged to-date, which only includes individuals landed between May - December in the year 2023.

Ages

MAY

# AGES 

ma = p +
  scatterpie::geom_scatterpie(data = df.pie.age %>% 
                                filter(year == 2023 & month == 5 & sex =='M'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('1','3','4', '5', '6', '7',
                                     '8', '9'),pie_scale = 3) + 
  scale_fill_manual(values =  bold_pal[c(3:12)],
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'AGES: May 2023 - Males',
       fill = 'Months Old') 

fa = p +
  scatterpie::geom_scatterpie(data = df.pie.age %>% 
                                filter(year == 2023 & month == 5 & sex =='F'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('1','3','4', '5', '6', '7',
                                     '8', '9'),pie_scale = 3) + 
  scale_fill_manual(values = bold_pal[c(3:12)],
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'AGES: May 2023 - Females', 
       fill = 'Months Old') 

# Plot ages
ma + fa

JUNE

# AGES 

ma = p +
  scatterpie::geom_scatterpie(data = df.pie.age %>% 
                                filter(year == 2023 & month == 6 & sex =='M'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('1','3','4', '5', '6', '7',
                                     '8', '9'),pie_scale = 3) + 
  scale_fill_manual(values = bold_pal[c(3:12)],
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'AGES: June 2023 - Males', 
       fill = 'Months Old') 

fa = p +
  scatterpie::geom_scatterpie(data = df.pie.age %>% 
                                filter(year == 2023 & month == 6 & sex =='F'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('1','3','4', '5', '6', '7',
                                     '8', '9'),pie_scale = 3) + 
  scale_fill_manual(values = bold_pal[c(3:12)],
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'AGES: June 2023 - Females', 
       fill = 'Months Old') 

# Plot ages
ma + fa

JULY

# AGES 

ma = p +
  scatterpie::geom_scatterpie(data = df.pie.age %>% 
                                filter(year == 2023 & month == 7 & sex =='M'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('1','3','4', '5', '6', '7',
                                     '8', '9'),pie_scale = 5) + 
  scale_fill_manual(values = bold_pal[c(3:12)],
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'AGES: July 2023 - Males', 
       fill = 'Months Old') 

fa = p +
  scatterpie::geom_scatterpie(data = df.pie.age %>% 
                                filter(year == 2023 & month == 7 & sex =='F'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('1','3','4', '5', '6', '7',
                                     '8', '9'),pie_scale = 5) + 
  scale_fill_manual(values = bold_pal[c(3:12)],
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'AGES: July 2023 - Females', 
       fill = 'Months Old') 

# Plot ages
ma + fa

AUG

# AGES 

ma = p +
  scatterpie::geom_scatterpie(data = df.pie.age %>% 
                                filter(year == 2023 & month == 8 & sex =='M'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('1','3','4', '5', '6', '7',
                                     '8', '9'),pie_scale = 15) + 
  scale_fill_manual(values = bold_pal[c(4:12)],
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'AGES: August 2023 - Males', 
       fill = 'Months Old') 

fa = p +
  scatterpie::geom_scatterpie(data = df.pie.age %>% 
                                filter(year == 2023 & month == 8 & sex =='F'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('1','3','4', '5', '6', '7',
                                     '8', '9'),pie_scale = 15) + 
  scale_fill_manual(values = bold_pal[c(4:12)],
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'AGES: August 2023 - Females', 
       fill = 'Months Old') 

# Plot ages
ma + fa

# #### August: age maps (males) {.tabset}
# 
# A closer look to deal with overlaping data points 
# 
# *Males :*

tals = unique(df.pie.age %>% filter(month == 8 & sex =='M') %>% 
         dplyr::select(tally_no))
tals <- tals$tally_no
j=9

mat.map <- function(tals){
  df.pie.age %>% 
   filter(year == 2023 & month == 8 & sex =='M'& 
                                         tally_no %in% tals) %>% #  tals[c(i,j)]
   ggplot() + 
scatterpie::geom_scatterpie(aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('1','3','4', '5', '6', '7',
                                     '8', '9'), pie_scale = 5) + 
  scale_fill_manual(values = bold_pal[c(4:12)],
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'AGES: August 2023 - Males', 
       fill = 'Months Old') 
  }


for(i in 1:length(tals)){
 cat("\n#####",  as.character(tals[i]),"\n")
    print(mat.map(tals[c(i,j)])) 
    cat("\n")   
}
# #### August: age maps (females) {.tabset}
# 
# A closer look to deal with overlaping data points 
# 
# *Females :*
#   
  
tals = unique(df.pie.age %>% filter(month == 8 & sex =='F') %>% 
         dplyr::select(tally_no))
tals <- tals$tally_no
j=9

mat.map <- function(tals){
  df.pie.age %>% 
   filter(year == 2023 & month == 8 & sex =='M'& 
                                         tally_no %in% tals) %>% 
   ggplot() + 
scatterpie::geom_scatterpie(aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('1','3','4', '5', '6', '7',
                                     '8', '9'), pie_scale = 5) + 
  scale_fill_manual(values = bold_pal[c(4:12)],
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'AGES: August 2023 - Females', 
       fill = 'Months Old') 
  }


for(i in 1:length(tals)){
 cat("\n#####",  as.character(tals[i]),"\n")
    print(mat.map(tals[c(i,j)])) 
    cat("\n")   
}

SEPT

# AGES 

ma = p +
  scatterpie::geom_scatterpie(data = df.pie.age %>% 
                                filter(year == 2023 & month == 9 & sex =='M'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('1','3','4', '5', '6', '7',
                                     '8', '9'),pie_scale = 8) + 
  scale_fill_manual(values = bold_pal[c(5:12)],
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'AGES: September 2023 - Males', 
       fill = 'Months Old') 

fa = p +
  scatterpie::geom_scatterpie(data = df.pie.age %>% 
                                filter(year == 2023 & month == 9 & sex =='F'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('1','3','4', '5', '6', '7',
                                     '8', '9'),pie_scale = 8) + 
  scale_fill_manual(values = bold_pal[c(5:12)],
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'AGES: September 2023 - Females', 
       fill = 'Months Old') 

# Plot ages
ma + fa

OCT

# AGES 

ma = p +
  scatterpie::geom_scatterpie(data = df.pie.age %>% 
                                filter(year == 2023 & month == 10 & sex =='M'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('1','3','4', '5', '6', '7',
                                     '8', '9'),pie_scale = 5) + 
  scale_fill_manual(values =  bold_pal[c(3:12)],
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'AGES: October 2023 - Males', 
       fill = 'Months Old') 

fa = p +
  scatterpie::geom_scatterpie(data = df.pie.age %>% 
                                filter(year == 2023 & month == 10 & sex =='F'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('1','3','4', '5', '6', '7',
                                     '8', '9'),pie_scale = 5) + 
  scale_fill_manual(values = bold_pal[c(1,3:12)],
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'AGES: October 2023 - Females', 
       fill = 'Months Old') 

# Plot ages
ma + fa

NOV

# AGES 

ma = p +
  scatterpie::geom_scatterpie(data = df.pie.age %>% 
                                filter(year == 2023 & month == 11 & sex =='M'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('1','3','4', '5', '6', '7',
                                     '8', '9'),pie_scale = 6) + 
  scale_fill_manual(values =  bold_pal[c(3:12)],
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'AGES: November 2023 - Males', 
       fill = 'Months Old') 

fa = p +
  scatterpie::geom_scatterpie(data = df.pie.age %>% 
                                filter(year == 2023 & month == 11 & sex =='F'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('1','3','4', '5', '6', '7',
                                     '8', '9'),pie_scale = 6) + 
  scale_fill_manual(values = bold_pal[c(1,3:12)],
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'AGES: November 2023 - Females', 
       fill = 'Months Old') 

# Plot ages
ma + fa

DEC

# AGES 

ma = p +
  scatterpie::geom_scatterpie(data = df.pie.age %>% 
                                filter(year == 2023 & month == 12 & sex =='M'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('1','3','4', '5', '6', '7',
                                     '8', '9'),pie_scale = 6) + 
  scale_fill_manual(values = bold_pal[c(4:12)],
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'AGES: December 2023 - Males', 
       fill = 'Months Old') 

fa = p +
  scatterpie::geom_scatterpie(data = df.pie.age %>% 
                                filter(year == 2023 & month == 12 & sex =='F'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('1','3','4', '5', '6', '7',
                                     '8', '9'),pie_scale = 6) + 
  scale_fill_manual(values =  bold_pal[c(4:12)],
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'AGES: December 2023 - Females', 
       fill = 'Months Old') 

# Plot ages
ma + fa

Seasonal cycle of egg mop occurrence

Dave Richardson fit a double logistic function to the presence/absence data, noting no cutoff was used and suggesting that a future iteration should do that. Assuming individuals have about a month long egg stage, we would expect spawning to precede larval occurrence by about a month.

Figure 1. Seasonal cycle of paralarval occurrence by region. Green is the Mid Atlantic Bight (Hudson Canyon south), Red is Southern New England, Blue is Georges Bank and Black is the Gulf of Maine.

knitr::include_graphics(here::here(paste0("figures/paralarval_occurance.png")))

A double logistic regression was fit to the egg mop presence/absence data. This data set is a compilation of multiple survey data sets as well as data from the NEFSC Observer and Study Fleet programs. Assuming the deposition of egg mops is an indicator of a spawning event, we would expect the occurrence of egg mops to precede larval occurrence by about a month, which is what we see here.

Figure 2. Seasonal cycle of longfin egg mop occurrence across region. A double logistic function was fit to the presence/absence data.

library(sicegar)

# Pull in data: 
mops.env <- read.csv('mops_env_v3.csv')

mm = mops.env %>% mutate(pa = case_when(presence ==  'absent'~ 0, 
                                        presence ==  'present'~ 1)) %>% 
  filter(survey != 'ecomon') %>% 
  dplyr::select(pa, lat, lon, year, month)

# Create training/testing data sets
fold <- predicts::folds(x = mm,
                        k = 5,
                        by = mm$pa)

# table(fold)

testing <- mm[fold == 1, ]
training <- mm[fold != 1, ]

# Build GLM model using training data
glm_model <- glm(pa ~  year + month, # year, doy
                 data = training, family = binomial(link = 'logit'))

# Get predicted values from the model
glm_predict <- predict(glm_model, newdata=testing , type = "response")

# Build GAM
gam_model <- mgcv::gam(factor(pa) ~ #s(month),
                       ti(year,month, bs = c('tp','cc')),
                       data = training,
                       bs = 'cr', 
                       family = 'binomial',
                       method = 'REML', 
                       select = TRUE)

# Get predicted values from the model
gam_predict <- predict(gam_model, newdata=testing , type = "response")

# Create a dataframe holding obs/predictions
data = data.frame(month = testing$month, observed = testing$pa, 
                  glm = glm_predict, gam = gam_predict) #year = testing$year, 

# Fit double logistic curves 
## GAM
dataInput= data.frame(time=data$month,intensity=data$gam)
fitObj <- fitAndCategorize(dataInput,
                           threshold_minimum_for_intensity_maximum = 0.3,
                           threshold_intensity_range = 0.1, 
                           threshold_t0_max_int = 0.05)

sicegar.fit <- figureModelCurves(dataInput = fitObj$normalizedInput,
                                 doubleSigmoidalFitVector= fitObj$doubleSigmoidalModel, 
                                 showParameterRelatedLines = TRUE)
sicegar.fit$labels$y="Probability"
sicegar.fit$labels$x="Month"
plot(sicegar.fit)

Maturity

MAY

## MATURITY

# May 2023
m23 = p +
  scatterpie::geom_scatterpie(data = df.pie.year %>% 
                                filter(year == 2023 & month == 5 & sex =='M'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('0','1','2','3','4'),pie_scale = 3) + 
  scale_fill_manual(values = rev(pal.m),
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'MATURITY: May 2023 - Males', 
       fill = 'Maturity Stage') 

# May 2024

m24 = p +
  scatterpie::geom_scatterpie(data = df.pie.year %>% 
                                filter(year == 2024 & month == 5 & sex =='M'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('0','1','2','3','4'),pie_scale = 8) + 
  scale_fill_manual(values = rev(pal.m),
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'MATURITY: May 2024 - Males', 
       fill = 'Maturity Stage') 
## May 2023

f23 = p +
  scatterpie::geom_scatterpie(data = df.pie.year %>% 
                                filter(year == 2023 & month == 5 & sex =='F'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('0','1','2','3','4'),pie_scale = 3) + 
  scale_fill_manual(values = rev(pal.f),
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'MATURITY: May 2023 - Females', 
       fill = 'Maturity Stage') 


# May 2024


f24 = p +
  scatterpie::geom_scatterpie(data = df.pie.year %>% 
                                filter(year == 2024 & month == 5 & sex =='F'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('0','1','2','3','4'),pie_scale = 8) + 
  scale_fill_manual(values = rev(pal.f),
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'MATURITY: May 2024 - Females', 
       fill = 'Maturity Stage') 

# Plot Maturity 
m23 + m24

f23 + f24

JUNE

## June 2023
m23 = p +
  scatterpie::geom_scatterpie(data = df.pie.year %>% 
                                filter(year == 2023 & month == 6 & sex =='M'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('0','1','2','3','4'),pie_scale = 3) + 
  scale_fill_manual(values = rev(pal.m),
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'June 2023 - Males', 
       fill = 'Maturity Stage') 

# June 2024

m24 =p +
  scatterpie::geom_scatterpie(data = df.pie.year %>% 
                                filter(year == 2024 & month == 6 & sex =='M'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('0','1','2','3','4'),pie_scale = 4) + 
  scale_fill_manual(values = rev(pal.m),
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'June 2024 - Males', 
       fill = 'Maturity Stage') 
## June 2023

f23 =p +
  scatterpie::geom_scatterpie(data = df.pie.year %>% 
                                filter(year == 2023 & month == 6 & sex =='F'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('0','1','2','3','4'),pie_scale = 3) + 
  scale_fill_manual(values = rev(pal.f),
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'June 2023 - Females', 
       fill = 'Maturity Stage') 


# June 2024


f24 =p +
  scatterpie::geom_scatterpie(data = df.pie.year %>% 
                                filter(year == 2024 & month == 6 & sex =='F'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('0','1','2','3','4'),pie_scale = 4) + 
  scale_fill_manual(values = rev(pal.f),
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'June 2024 - Females', 
       fill = 'Maturity Stage') 

# Plot Maturity 
m23 + m24

f23 + f24

JULY

## July 2023
m23 = p +
  scatterpie::geom_scatterpie(data = df.pie.year %>% 
                                filter(year == 2023 & month == 7 & sex =='M'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('0','1','2','3','4'),pie_scale = 5) + 
  scale_fill_manual(values = rev(pal.m),
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'July 2023 - Males', 
       fill = 'Maturity Stage') 

# July 2024

m24 = p +
  scatterpie::geom_scatterpie(data = df.pie.year %>% 
                                filter(year == 2024 & month == 7 & sex =='M'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('0','1','2','3','4'),pie_scale = 4) + 
  scale_fill_manual(values = rev(pal.m),
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'July 2024 - Males', 
       fill = 'Maturity Stage') 
## July 2023

f23 = p +
  scatterpie::geom_scatterpie(data = df.pie.year %>% 
                                filter(year == 2023 & month == 7 & sex =='F'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('0','1','2','3','4'),pie_scale = 5) + 
  scale_fill_manual(values = rev(pal.f),
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'July 2023 - Females', 
       fill = 'Maturity Stage') 

# July 2024

f24 = p +
  scatterpie::geom_scatterpie(data = df.pie.year %>% 
                                filter(year == 2024 & month == 7 & sex =='F'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('0','1','2','3','4'),pie_scale = 4) + 
  scale_fill_manual(values = rev(pal.f),
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'July 2024 - Females', 
       fill = 'Maturity Stage') 

# Plot Maturity 
m23 + m24

f23 + f24

AUG

## August 2023
m23 = p +
  scatterpie::geom_scatterpie(data = df.pie.year %>% 
                                filter(year == 2023 & month == 8 & sex =='M'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('0','1','2','3','4'),pie_scale = 15) + 
  scale_fill_manual(values = rev(pal.m),
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'August 2023 - Males', 
       fill = 'Maturity Stage') 

# August 2024

m24 = p +
  scatterpie::geom_scatterpie(data = df.pie.year %>% 
                                filter(year == 2024 & month == 8 & sex =='M'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('0','1','2','3','4'),pie_scale = 8) + 
  scale_fill_manual(values = rev(pal.m),
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'August 2024 - Males', 
       fill = 'Maturity Stage') 
## August 2023

f23 = p +
  scatterpie::geom_scatterpie(data = df.pie.year %>% 
                                filter(year == 2023 & month == 8 & sex =='F'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('0','1','2','3','4'),pie_scale = 15) + 
  scale_fill_manual(values = rev(pal.f),
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'August 2023 - Females', 
       fill = 'Maturity Stage') 

# August 2024

f24 = p +
  scatterpie::geom_scatterpie(data = df.pie.year %>% 
                                filter(year == 2024 & month == 8 & sex =='F'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('0','1','2','3','4'),pie_scale = 8) + 
  scale_fill_manual(values = rev(pal.f),
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'August 2024 - Females', 
       fill = 'Maturity Stage') 


# Plot Maturity 
m23 + m24

f23 + f24

SEPT

## September 2023
m23 = p +
  scatterpie::geom_scatterpie(data = df.pie.year %>% 
                                filter(year == 2023 & month == 9 & sex =='M'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('0','1','2','3','4'),pie_scale = 5) + 
  scale_fill_manual(values = rev(pal.m),
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'September 2023 - Males', 
       fill = 'Maturity Stage') 

# September 2024

m24 = p +
  scatterpie::geom_scatterpie(data = df.pie.year %>% 
                                filter(year == 2024 & month == 9 & sex =='M'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('0','1','2','3','4'),pie_scale = 0.1) + 
  scale_fill_manual(values = rev(pal.m),
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'September 2024 - Males', 
       fill = 'Maturity Stage') 
## September 2023

f23 = p +
  scatterpie::geom_scatterpie(data = df.pie.year %>% 
                                filter(year == 2023 & month == 9 & sex =='F'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('0','1','2','3','4'),pie_scale = 5) + 
  scale_fill_manual(values = rev(pal.f),
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'September 2023 - Females', 
       fill = 'Maturity Stage') 


# September 2024

f24 = p +
  scatterpie::geom_scatterpie(data = df.pie.year %>% 
                                filter(year == 2024 & month == 9 & sex =='F'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('0','1','2','3','4'),pie_scale =  0.1) + 
  scale_fill_manual(values = rev(pal.f),
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'September 2024 - Females', 
       fill = 'Maturity Stage') 

# Plot Maturity 
m23 + m24

f23 + f24

OCT

 ## October 2023
m23 = p +
  scatterpie::geom_scatterpie(data = df.pie.year %>% 
                                filter(year == 2023 & month == 10 & sex =='M'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('0','1','2','3','4'),pie_scale = 5) + 
  scale_fill_manual(values = rev(pal.m),
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'October 2023 - Males', 
       fill = 'Maturity Stage') 

# October 2024

m24 = p +
  scatterpie::geom_scatterpie(data = df.pie.year %>% 
                                filter(year == 2024 & month == 10 & sex =='M'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('0','1','2','3','4'),pie_scale = 8) + 
  scale_fill_manual(values = rev(pal.m),
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'October 2024 - Males', 
       fill = 'Maturity Stage') 
## October 2023

f23 = p +
  scatterpie::geom_scatterpie(data = df.pie.year %>% 
                                filter(year == 2023 & month == 10 & sex =='F'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('0','1','2','3','4'),pie_scale = 5) + 
  scale_fill_manual(values = rev(pal.f),
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'October 2023 - Females', 
       fill = 'Maturity Stage') 


# October 2024


f24 = p +
  scatterpie::geom_scatterpie(data = df.pie.year %>% 
                                filter(year == 2024 & month == 10 & sex =='F'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('0','1','2','3','4'),pie_scale = 8) + 
  scale_fill_manual(values = rev(pal.f),
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'October 2024 - Females', 
       fill = 'Maturity Stage') 


# Plot Maturity 
m23 + m24

f23 + f24

NOV

## November 2023
m23 = p +
  scatterpie::geom_scatterpie(data = df.pie.year %>% 
                                filter(year == 2023 & month == 11 & sex =='M'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('0','1','2','3','4'),pie_scale = 5) + 
  scale_fill_manual(values = rev(pal.m),
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'November 2023 - Males', 
       fill = 'Maturity Stage') 

# November 2024

m24 = p +
  scatterpie::geom_scatterpie(data = df.pie.year %>% 
                                filter(year == 2024 & month == 11 & sex =='M'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('0','1','2','3','4'),pie_scale = 8) + 
  scale_fill_manual(values = rev(pal.m),
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'November 2024 - Males', 
       fill = 'Maturity Stage') 
## November 2023

f23 = p +
  scatterpie::geom_scatterpie(data = df.pie.year %>% 
                                filter(year == 2023 & month == 11 & sex =='F'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('0','1','2','3','4'),pie_scale = 5) + 
  scale_fill_manual(values = rev(pal.f),
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'November 2023 - Females', 
       fill = 'Maturity Stage') 


# November 2024


f24 = p +
  scatterpie::geom_scatterpie(data = df.pie.year %>% 
                                filter(year == 2024 & month == 11 & sex =='F'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('0','1','2','3','4'),pie_scale = 8) + 
  scale_fill_manual(values = rev(pal.f),
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'November 2024 - Females', 
       fill = 'Maturity Stage') 

# Plot Maturity 
m23 + m24

f23 + f24

DEC

## December 2023
m23 = p +
  scatterpie::geom_scatterpie(data = df.pie.year %>% 
                                filter(year == 2023 & month == 12 & sex =='M'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('0','1','2','3','4'),pie_scale = 5) + 
  scale_fill_manual(values = rev(pal.m),
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'December 2023 - Males', 
       fill = 'Maturity Stage') 

# December 2024

m24 = p +
  scatterpie::geom_scatterpie(data = df.pie.year %>% 
                                filter(year == 2024 & month == 12 & sex =='M'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('0','1','2','3','4'),pie_scale = 8) + 
  scale_fill_manual(values = rev(pal.m),
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'December 2024 - Males', 
       fill = 'Maturity Stage') 
## December 2023

f23 = p +
  scatterpie::geom_scatterpie(data = df.pie.year %>% 
                                filter(year == 2023 & month == 12 & sex =='F'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('0','1','2','3','4'),pie_scale = 5) + 
  scale_fill_manual(values = rev(pal.f),
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'December 2023 - Females', 
       fill = 'Maturity Stage') 

# December 2024

f24 = p +
  scatterpie::geom_scatterpie(data = df.pie.year %>% 
                                filter(year == 2024 & month == 12 & sex =='F'), 
                              aes(x=lon, y=lat, group=factor(tally_no)), 
                              cols=c('0','1','2','3','4'),pie_scale = 8) + 
  scale_fill_manual(values = rev(pal.f),
                    na.value = NA, na.translate=FALSE) +
  labs(title = 'December 2024 - Females', 
       fill = 'Maturity Stage') 


# Plot Maturity 
m23 + m24

f23 + f24

## Temp map

temp = df %>% dplyr::select(tally_no, year, month, month.n,
                           mean_temp_c, mean_dpth_m, lat, lon) %>% 
  na.omit(mean_temp_c)

temp <- read.csv(here::here('data/study_fleet/sf_bt_longfin.csv')) 
temp <- temp %>% rename_all(., .funs = tolower) %>% 
  mutate(month.n = month.name[sail_month],
         lat = start_haul_lat_dd, 
         lon = start_haul_lon_dd)

log.err <- which(temp$gte_code >= 50 & temp$gte_code <60)

df.no.err <- temp[-log.err,]

load(here::here('nes_map.Rdata'))
ggplot() +
  # draw full grid
  geom_sf(data = us.coast, color = 'grey99') +
  geom_point(data = df.no.err %>% filter(depth_meters <= 50 &
                                           mean_temp_c >=12 & mean_temp_c <=20),
             aes(x = lon, y = lat), color = 'red') + 
  coord_sf(xlim = c(-76,-66.0), ylim = c(36,44), datum = sf::st_crs(4326)) +    
  theme_minimal() +
  facet_wrap(~factor(month.n, levels = month.name))