library(ggplot2)
library(hrbrthemes)
library(viridis)
library(ggridges)
library(tidyverse)
library(lubridate)
library(prettydoc)
library(sf)
library(rgdal)
library(raster)
library(dplyr)
library(ggplot2)
library(RColorBrewer)
library(wesanderson)
library(ggplot2)
library(ggnewscale)
library(scales)
library(marmap) # bathymetry
library(tidyverse)
library(readxl)
library(ggpubr) ##
# Read in data
df <- read.csv(here::here('data/squibs_maturity_fine_scale.csv'))

# Switch to long format
df.long <- df %>% 
    group_by(sample_id, vessel_name, area_code, date, organism_id) %>%
    pivot_longer(cols=c('ml', 'ow', 'mw','tsl','spl','ast',
                           'ngl','agl', 'ep', 'aeo'),
                    names_to='param_type',
                    values_to='param_value_num') 

# Subset data by length and weight
ml <- df.long %>% filter(param_type == 'ml')

wt <- df.long %>% filter(param_type == 'ow')


## Shapefiles
wd = here::here('shapefiles')
US.areas <- rgdal::readOGR(wd, 'USA', verbose = FALSE)
canada.areas <-rgdal::readOGR(wd,'Canada', verbose = FALSE)
nafo <- rgdal::readOGR(wd,'NAFO', verbose = FALSE) 
## Stat areas
stat_areas <- rgdal::readOGR(wd,'groundfish_stat_areas', verbose = FALSE) 
proj4string(stat_areas) <- CRS("+init=epsg:4326")
proj4string(US.areas) <- CRS("+init=epsg:4326")
proj4string(canada.areas) <- CRS("+init=epsg:4326")
proj4string(nafo) <- CRS("+init=epsg:4326")
longfin <- nafo[na.omit(nafo@data$ZONE %in% c('5Ze','5Zw', '6A', '6B')),]
proj4string(longfin) <- CRS("+init=epsg:4326")
US.areas <- US.areas %>% st_as_sf()
canada.areas <- canada.areas %>% st_as_sf()
nafo <- nafo %>% st_as_sf()
long.sf <- st_as_sf(longfin)

## Bathy and ports
bathy <- marmap::getNOAA.bathy(-80,-58, 33, 46)
bathy = fortify.bathy(bathy)
ports <- data.frame(lon = c( -70.9342,-71.4856,-74.9060, -76.3452), 
                    lat = c(41.6362, 41.3731,38.9351, 37.0299),
                    port = c('New Bedford, MA', 'Point Judith, RI', 
                             'Cape May, NJ', 'Hampton, VA'))

Lengths

Range of lengths: 0, 379

# Define category breaks
size_breaks <- c(0,50,100,150,200,250,300,350,400)
# Making a function to bin the catches
label_interval <- function(breaks) {
  paste0("(", breaks[1:length(breaks) - 1], "-", breaks[2:length(breaks)], ")")
}
labels = label_interval(size_breaks)
# Define colors using the function 
discr_colors_fct <- 
  scales::div_gradient_pal(low = "chartreuse",
                           mid = "dodgerblue1", 
                           high = "blue4")
discr_colors <- discr_colors_fct(seq(0, 1, length.out = length(size_breaks)))
  
ggplot()+
  geom_sf(data=US.areas %>% st_as_sf(),color = 'gray20', fill = '#DCDCDC') +
  geom_sf(data=canada.areas %>% st_as_sf(),color = 'gray20', fill = '#DCDCDC') +
  geom_sf(data = long.sf, aes(fill = NA), alpha = 0.1, lwd = 1, col = 'gray20') +
  geom_point(data = df.long %>% filter(param_type == 'ml'),
             aes(x = lon, y = lat, 
                 fill = cut(param_value_num, 
                             breaks = size_breaks,
                             labels = label_interval(size_breaks))), 
             size = 3,pch = 21, color = 'black') +
  labs(fill = 'Size (mm)') + 
  guides(color = 'none', pch = 'none') +
  scale_fill_manual(breaks = labels,
                    values = discr_colors,
                     na.value = NA, na.translate=FALSE) +
  coord_sf(xlim = c(-76,-65.0), ylim = c(37,42.5), datum = sf::st_crs(4326)) +
  theme_classic()

tab = table(cut(ml$param_value_num, 
    breaks = size_breaks,
    labels = label_interval(size_breaks)))

barplot(tab, xlab = 'Length bins (mm)', main = '')

Weights

Range of weights: 0, 531

# Define category breaks
size_breaks <- c(0,50,100,150,200,250,300,350,400, 450, 500)
# Making a function to bin the catches
label_interval <- function(breaks) {
  paste0("(", breaks[1:length(breaks) - 1], "-", breaks[2:length(breaks)], ")")
}
labels = label_interval(size_breaks)
# Define colors using the function 
discr_colors_fct <- 
  scales::div_gradient_pal(low = "chartreuse",
                           mid = "dodgerblue1", 
                           high = "blue4")
discr_colors <- discr_colors_fct(seq(0, 1, length.out = length(size_breaks)))
  
ggplot()+
  geom_sf(data=US.areas %>% st_as_sf(),color = 'gray20', fill = '#DCDCDC') +
  geom_sf(data=canada.areas %>% st_as_sf(),color = 'gray20', fill = '#DCDCDC') +
  geom_sf(data = long.sf, aes(fill = NA), alpha = 0.1, lwd = 1, col = 'gray20') +
  geom_point(data = df.long %>% filter(param_type == 'ow'),
             aes(x = lon, y = lat, 
                 fill = cut(param_value_num, 
                            breaks = size_breaks,
                            labels =label_interval(size_breaks))), 
             size = 3,pch = 21, color = 'black') +
  labs(fill = 'Weight (g)') + 
  guides(color = FALSE, pch = FALSE) +
  scale_fill_manual(breaks = labels,
                    values = discr_colors,
                    na.value = NA, na.translate=FALSE) +
  coord_sf(xlim = c(-76,-65.0), ylim = c(37,42.5), datum = sf::st_crs(4326)) +
  theme_classic() 

tab = table(cut(wt$param_value_num, 
    breaks = size_breaks,
    labels = label_interval(size_breaks)))

barplot(tab, xlab = 'Weight bins (g)', main = '')

ggplot()+
  geom_sf(data=US.areas %>% st_as_sf(),color = 'gray20', fill = '#DCDCDC') +
  geom_sf(data=canada.areas %>% st_as_sf(),color = 'gray20', fill = '#DCDCDC') +
  geom_sf(data = long.sf, aes(fill = NA), alpha = 0.1, lwd = 1, col = 'gray20') +
  geom_point(data = df.long %>% filter(param_type == 'ow'),
             aes(x = lon, y = lat, 
                 fill = cut(param_value_num, 
                            breaks = size_breaks,
                            labels =label_interval(size_breaks))), 
             size = 3,pch = 21, color = 'black') +
  labs(fill = 'Weight (g)') + 
  guides(color = FALSE, pch = FALSE) +
  scale_fill_manual(breaks = labels,
                    values = discr_colors,
                    na.value = NA, na.translate=FALSE) +
  coord_sf(xlim = c(-76,-65.0), ylim = c(37,42.5), datum = sf::st_crs(4326)) +
  theme_classic() +
  facet_wrap(~week)

Sex and Maturity

ggplot()+
  geom_sf(data=US.areas %>% st_as_sf(),color = 'gray20', fill = '#DCDCDC') +
  geom_sf(data=canada.areas %>% st_as_sf(),color = 'gray20', fill = '#DCDCDC') +
  geom_sf(data = long.sf, aes(fill = NA), alpha = 0.1, lwd = 1, col = 'gray20') +
  geom_point(data = df.long,
             aes(x = lon, y = lat, 
                 fill = factor(mat_stage)), 
             size = 4,pch = 21, color = 'black') +
  labs(fill = 'Maturity stage') + 
  # scale_fill_manual(values = discr_colors,
  #                    na.value = NA, na.translate=FALSE) +
  #coord_sf(xlim = c(-76,-65.0), ylim = c(37,42.5), datum = sf::st_crs(4326)) +
  coord_sf(xlim = c(-76,-65.0), ylim = c(37,42.5), datum = sf::st_crs(4326)) +
  theme_classic() + 
  facet_wrap(~sex) + 
  theme(legend.position = "bottom") 

Zoomed in

# This is removing too many rows 
ggplot()+
  geom_jitter(data = df.long,
             aes(x = lon, y = lat,
                 fill = factor(mat_stage)),
             size = 4,pch = 21, color = 'black') + # alpha = 0.5,
  geom_sf(data=US.areas %>% st_as_sf(),
          color = 'gray20', fill = '#DCDCDC') +
  # geom_sf(data = long.sf, aes(fill = NA), alpha = 0, 
  #         lwd = 1, lty = 2, col = 'gray20') +
  #geom_point(size = 4,pch = 21, alpha = 0.5, color = 'black') +
  labs(fill = 'Maturity stage') + 
  # scale_fill_manual(values = discr_colors,
  #                    na.value = NA, na.translate=FALSE) +
  coord_sf(xlim = c(-74,-69.0), ylim = c(40.0,42), datum = sf::st_crs(4326)) +
  theme_classic() + 
  facet_wrap(~sex) + 
  theme(legend.position = "bottom") 

ggplot()+
  geom_jitter(data = df.long , #%>% filter(param_type == 'ml')
              aes(x = lon, y = lat,
                  fill = factor(sex)),
              size = 4,pch = 21, color = 'black') +
  geom_sf(data=US.areas %>% st_as_sf(),
          color = 'gray20', fill = '#DCDCDC') +
  # geom_sf(data = long.sf, aes(fill = NA), alpha = 0, 
  #         lwd = 1, lty = 2, col = 'gray20') +
  #geom_point(size = 4,pch = 21, alpha = 0.5, color = 'black') +
  labs(fill = 'Maturity stage') + 
  # scale_fill_manual(values = discr_colors,
  #                    na.value = NA, na.translate=FALSE) +
  coord_sf(xlim = c(-76,-65.0), ylim = c(37,42.5), datum = sf::st_crs(4326)) +
  # coord_sf(xlim = c(-74,-69.0), ylim = c(40.0,42), datum = sf::st_crs(4326)) +
  theme_classic() 
ggplot()+
  geom_jitter(data = df.long %>% filter(sex == 'Male'),
              aes(x = lon, y = lat,
                  fill = factor(sex)),
              size = 4,pch = 21, color = 'black') +
  geom_sf(data=US.areas %>% st_as_sf(),
          color = 'gray20', fill = '#DCDCDC') +
  # geom_sf(data = long.sf, aes(fill = NA), alpha = 0, 
  #         lwd = 1, lty = 2, col = 'gray20') +
  #geom_point(size = 4,pch = 21, alpha = 0.5, color = 'black') +
  labs(fill = 'Maturity stage') + 
  # scale_fill_manual(values = discr_colors,
  #                    na.value = NA, na.translate=FALSE) +
  coord_sf(xlim = c(-76,-65.0), ylim = c(37,42.5), datum = sf::st_crs(4326)) +
  # coord_sf(xlim = c(-74,-69.0), ylim = c(40.0,42), datum = sf::st_crs(4326)) +
  theme_classic()