---
title: "Scallop Example"
output: 
  rmarkdown::html_vignette:
    fig_width: 6
    fig_height: 4
vignette: >
  %\VignetteIndexEntry{Scallop Example}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```


## Introduction

This vignette is an example of an exploratory data analysis using `FishSET`. It 
utilizes a range of `FishSET` functions for importing and upload data, performing
quality assessment/quality control, and summarizing and visualizing data. 

<br><br>

## Packages

```{r setup}
library(FishSET)
library(dplyr)
library(tidyr)
library(ggplot2)
library(maps)
```

<br><br>

## Project Setup

The chunk below defines the location of the FishSET Folder. A temporary directory 
is used in this vignette example; for actual use, set the `folderpath` to a location 
that is not temporary. 
```{r}
folderpath <- tempdir()

proj <- "scallop"
```

<br><br>

## Data Import

Upload the northeast scallop data from the FishSET package.
```{r}
load_maindata(dat = FishSET::scallop, project = proj)
```

<br><br>

This data contains `r nrow(scallopMainDataTable)` rows and `r ncol(scallopMainDataTable)` variables.

<br><br>

View and upload the ten minute squares map and wind turbine closure areas from the FishSET
package. 
```{r}
plot_spat(FishSET::tenMNSQR)
plot_spat(FishSET::windLease)
```


```{r}
load_spatial(spat = FishSET::tenMNSQR, project = proj, name = "TenMNSQR")

load_spatial(spat = FishSET::windLease, project = proj, name = "WindClose")
```

<br><br>

Assign the regulatory zones (`scallopTenMNSQRSpatTable`) and closure areas (`scallopWindCloseSpatTable`) to the working environment.

```{r}
scallopTenMNSQRSpatTable <- table_view("scallopTenMNSQRSpatTable", proj)
scallopWindCloseSpatTable <- table_view("scallopWindCloseSpatTable", proj)
```

<br><br>

### Fleet assignment
Assign all observations to either "Access Area" or "Days at Sea" fleets. 
```{r create_fleet_tabs}
fleet_tab <- 
  data.frame(
    condition = c('`Plan Code` == "SES" & `Program Code` == "SAA"',
                  '`Plan Code` == "SES" & `Program Code` == "SCA"'),
    fleet = c("Access Area", "Days at Sea"))
```


```{r}
# save fleet table to FishSET DB
fleet_table(scallopMainDataTable, 
            project = proj,
            table = fleet_tab, save = TRUE)
# grab tab name
fleet_tab_name <- list_tables(proj, type = "fleet")
# create fleet column
scallopMainDataTable <- 
  fleet_assign(scallopMainDataTable, project = proj, 
               fleet_tab = fleet_tab_name)
```

<br><br>

### Bin Gears
The data contain several types of fishing gear. For simplicity, the 
`GEARCODE` column is re-binned to include three categories: `"DREDGE"`, `"TRAWL-BOTTOM"`,
and `"OTHER"`. 
```{r bin_gears}
scallopMainDataTable$GEARCODE_OLD <- scallopMainDataTable$GEARCODE
#Anything with "DREDGE" in the GEARCODE will be rebinned to "DREDGE" 
pat_match <- "*DREDGE*"
reg_pat <- glob2rx(pat_match)
scallopMainDataTable$GEARCODE[grep(reg_pat, scallopMainDataTable$GEARCODE)] <- 'DREDGE'
#Look at the GEARCODE NOW, there should be 'DREDGE', 'TRAWL-BOTTOM', and some funky stuff
table(scallopMainDataTable$GEARCODE)
scallopMainDataTable$GEARCODE[!(scallopMainDataTable$GEARCODE %in% c('DREDGE','TRAWL-BOTTOM'))] <- 'OTHER'
```

### Operating Profit
Calculate operating profit by subtracting 2020 trip costs from aggregated revenues in 2020 dollars.
```{r calculate operating profit}
scallopMainDataTable <- 
  scallopMainDataTable %>% 
  mutate(OPERATING_PROFIT_2020 = DOLLAR_ALL_SP_2020_OBSCURED - TRIP_COST_WINSOR_2020_DOL)
```

<br><br>

### Summary Table
```{r}
summary_stats(scallopMainDataTable, proj) %>% 
  pretty_tab_sb()
```

<br><br><br>

## QAQC

### NA Check
```{r}
na_filter(scallopMainDataTable, 
          project = proj, 
          replace = FALSE, remove = FALSE, 
          rep.value = NA, over_write = FALSE)
```

<br><br>

### NaN Check
```{r}
nan_filter(scallopMainDataTable, 
           project = proj, 
           replace = FALSE, remove = FALSE, 
           rep.value = NA, over_write = FALSE)
```

<br><br>

### Unique Rows

```{r}
unique_filter(scallopMainDataTable, project = proj, remove = FALSE)
```
<br><br>

### Empty Variables
"Empty" variables contain all `NA`s.
```{r}
empty_vars_filter(scallopMainDataTable, project = proj, remove = FALSE)
```

<br><br>


### Lon/Lat Format

```{r}
degree(scallopMainDataTable, project = proj,
       lat = "DDLAT", lon = "DDLON", 
       latsign = NULL, lonsign = NULL,
       replace = FALSE)
```
<br><br>

### Spatial QAQC 

```{r}
spat_qaqc_out <- spatial_qaqc(dat = scallopMainDataTable, 
                              project = proj,
                              spat = scallopTenMNSQRSpatTable, 
                              lon.dat = "DDLON", 
                              lat.dat = "DDLAT")

spat_qaqc_out$dataset <- NULL # drop dataset

spat_qaqc_out$spatial_summary %>%
  pretty_lab(cols = "n") %>%
  pretty_tab()

spat_qaqc_out[2:4]
```

<br><br><br>

## Data Creation

### Landing Year

```{r}
scallopMainDataTable <- 
  scallopMainDataTable %>% 
  mutate(DB_LANDING_YEAR = as.numeric(format(date_parser(DATE_TRIP), "%Y")))
```

<br><br>

### Finagle LANDED_OBSCURED to Thousands of Pounds
```{r}
scallopMainDataTable$LANDED_OBSCURED <- scallopMainDataTable$LANDED_OBSCURED / 1000
```

<br><br>

### CPUE
Create `CPUE` variable using `TRIP_LENGTH` and `LANDED_OBSCURED`. Filter out any 
infinite values. 
```{r}
scallopMainDataTable <- 
  cpue(scallopMainDataTable, proj,
       xWeight = "LANDED_OBSCURED",
       xTime = "TRIP_LENGTH", 
       name = "CPUE")

scallopMainDataTable <- 
  scallopMainDataTable %>% 
  filter(!is.infinite(CPUE))
```

<br><br>

#### CPUE Percent Rank
Add a percent rank column to filter outliers.
```{r}
outlier_table(scallopMainDataTable, proj, x = "CPUE") %>% 
  pretty_lab() %>% 
  pretty_tab()

scallopMainDataTable <- 
  scallopMainDataTable %>% 
  mutate(CPUE_p = percent_rank(CPUE))
```

<br><br>

### VPUE
Same as above but with revenue instead of meat pounds. 
```{r}
scallopMainDataTable <- 
  cpue(scallopMainDataTable, proj,
       xWeight = "DOLLAR_OBSCURED",
       xTime = "TRIP_LENGTH", 
       name = "VPUE")

scallopMainDataTable <- 
  scallopMainDataTable %>% 
  filter(!is.infinite(VPUE))
```

<br><br>

#### VPUE Percent Rank

Add a percent rank column to filter outliers.
```{r}
outlier_table(scallopMainDataTable, proj, x = "VPUE") %>% 
  pretty_lab() %>% 
  pretty_tab()

scallopMainDataTable <- 
  scallopMainDataTable %>% 
  mutate(VPUE_p = percent_rank(VPUE))
```

<br><br>

### Fleet Tabulation

```{r}
scallopMainDataTable %>% 
  count(fleet) %>% 
  mutate(perc = round(n/sum(n) * 100, 1)) %>% 
  pretty_lab(cols = "n") %>% 
  pretty_tab()
```

<br><br>

### Zone Assignment

Assign each observation to a regulatory zone. 
```{r}
scallopMainDataTable <- 
  assignment_column(scallopMainDataTable, project = proj,
                    spat = scallopTenMNSQRSpatTable, 
                    lon.dat = "DDLON",
                    lat.dat = "DDLAT", 
                    cat = "TEN_ID",
                    name = "ZONE_ID",
                    closest.pt = FALSE,
                    hull.polygon = FALSE)
```

<br><br>

### Closure Area Assignment
Assign each observation to a closure area. An observation will have an `NA` if
it  does not occur within a closure area.
```{r}
scallopMainDataTable <- 
  assignment_column(scallopMainDataTable, project = proj,
                    spat = scallopWindCloseSpatTable, 
                    lon.dat = "DDLON",
                    lat.dat = "DDLAT", 
                    cat = "NAME",
                    name = "closeID",
                    closest.pt = FALSE,
                    hull.polygon = FALSE) 

scallopMainDataTable <- 
  scallopMainDataTable %>% 
  mutate(in_closure = !is.na(closeID))
```

<br><br>

`r sum(scallopMainDataTable$in_closure)` 
observations (`r round(sum(scallopMainDataTable$in_closure)/nrow(scallopMainDataTable) * 100, 2)`%) occurred inside a closure area. 
```{r}
agg_helper(scallopMainDataTable, 
           value = "in_closure", 
           count = TRUE, 
           fun = NULL) %>% 
  pivot_wider(names_from = "in_closure", values_from = "n") %>% 
  rename("Outside Closure(s)" = "FALSE", "Inside Closure(s)" = "TRUE") %>% 
  pretty_lab() %>% 
  pretty_tab()
```

<br><br>

Observations inside/outside closures by fleet. 
```{r}
agg_helper(scallopMainDataTable, group = "fleet", 
            value = "in_closure", count = TRUE, fun = NULL) %>% 
  pivot_wider(names_from = "in_closure", values_from = "n") %>% 
  rename("Outside Closure(s)" = "FALSE", "Inside Closure(s)" = "TRUE") %>% 
  pretty_lab() %>% 
  pretty_tab()
```

<br><br>

Observations inside/outside closures by year. 
```{r}
agg_helper(scallopMainDataTable, value = "in_closure", 
            group = "DB_LANDING_YEAR", 
            count = TRUE, fun = NULL) %>% 
  pivot_wider(names_from = "in_closure", values_from = "n",
              values_fill = 0) %>% 
  arrange(DB_LANDING_YEAR) %>% 
  rename("Outside closure(s)" = "FALSE", "Inside closure(s)" = "TRUE") %>% 
  pretty_lab(ignore = "DB_LANDING_YEAR") %>% 
  pretty_tab()
```

<br><br>

Observations inside/outside closures by year and fleet. 
```{r}
agg_helper(scallopMainDataTable, value = "in_closure", 
            group = c("DB_LANDING_YEAR", "fleet"), 
            count = TRUE, fun = NULL) %>% 
  pivot_wider(names_from = "in_closure", values_from = "n",
              values_fill = 0) %>% 
  arrange(DB_LANDING_YEAR) %>% 
  rename("Outside closure(s)" = "FALSE", "Inside closure(s)" = "TRUE") %>% 
  pretty_lab(ignore = "DB_LANDING_YEAR") %>% 
  pretty_tab_sb(width = "60%") 
```

<br><br><br>

## Zone Summary

### Frequency

The number of observations by zone.
```{r}
zone_out <- zone_summary(scallopMainDataTable, project = proj,
                         spat = scallopTenMNSQRSpatTable,
                         zone.dat = "ZONE_ID",
                         zone.spat = "TEN_ID",
                         output = "tab_plot",
                         count = TRUE,
                         breaks = NULL, n.breaks = 10,
                         na.rm = TRUE)

zone_out$plot
zone_out$table %>%
  pretty_lab(cols = "n") %>%
  pretty_tab_sb(width = "40%")
```

<br><br>

Percent of observations by zone.
```{r}
zone_out <- 
  zone_summary(scallopMainDataTable,
               project = proj,
               spat = scallopTenMNSQRSpatTable,
               zone.dat = "ZONE_ID",
               zone.spat = "TEN_ID",
               output = "tab_plot",
               count = TRUE, fun = "percent",
               breaks = c(seq(.2, .5, .1), seq(1, 2, .5)), 
               na.rm = TRUE)

zone_out$plot
zone_out$table %>% 
  pretty_lab(ignore = "ZONE_ID") %>% 
  pretty_tab_sb(width = "40%")
```

<br><br>

Zone frequency (Access Area fleet).
```{r}
zone_out <- 
  scallopMainDataTable %>% 
    filter(fleet == "Access Area") %>% 
    zone_summary(project = proj,
                 spat = scallopTenMNSQRSpatTable, 
                 zone.dat = "ZONE_ID",
                 zone.spat = "TEN_ID",
                 output = "tab_plot",
                 count = TRUE,
                 breaks = NULL, n.breaks = 10, 
                 na.rm = TRUE)

zone_out$plot
zone_out$table %>% 
  pretty_lab(cols = "n") %>% 
  pretty_tab_sb(width = "40%")
```


<br><br><br>

Zone frequency (Days at Sea fleet).
```{r}
zone_out <- 
  scallopMainDataTable %>% 
    filter(fleet == "Days at Sea") %>% 
    zone_summary(project = proj,
                 spat = scallopTenMNSQRSpatTable,
                 zone.dat = "ZONE_ID",
                 zone.spat = "TEN_ID",
                 output = "tab_plot",
                 count = TRUE,
                 breaks = NULL, n.breaks = 10, 
                 na.rm = TRUE)

zone_out$plot
zone_out$table %>% 
  pretty_lab(cols = "n") %>% 
  pretty_tab_sb(width = "40%")
```

<br><br>

### Catch

Total catch (meats). 
```{r}
zone_out <- 
  zone_summary(scallopMainDataTable, 
               project = proj,
               spat = scallopTenMNSQRSpatTable, 
               zone.dat = "ZONE_ID",
               zone.spat = "TEN_ID",
               count = FALSE,
               var = "LANDED_OBSCURED", fun = "sum",
               breaks = c(1e3, 1e4, 5e4, 1e5, seq(1e6, 1.3e7, 2e6)),
               output = "tab_plot", na.rm = TRUE)

zone_out$plot
zone_out$table %>% 
  pretty_lab(cols = "LANDED_OBSCURED") %>% 
  pretty_tab_sb(width = "40%")
```

<br><br>

Average catch (meats). 
```{r}
zone_out <- 
  zone_summary(scallopMainDataTable,
               project = proj,
               spat = scallopTenMNSQRSpatTable, 
               zone.dat = "ZONE_ID",
               zone.spat = "TEN_ID",
               count = FALSE,
               var = "LANDED_OBSCURED", fun = "mean",
               breaks = c(1e3, 5e3, seq(1e4, 4e4, 5e3)),
               output = "tab_plot", na.rm = TRUE)

zone_out$plot
zone_out$table %>% 
  pretty_lab(cols = "LANDED_OBSCURED") %>% 
  pretty_tab_sb(width = "40%")
```

<br><br>

Percent of total catch (meat). 
```{r}
zone_out <- 
  zone_summary(scallopMainDataTable, project = proj,
               spat = scallopTenMNSQRSpatTable,
               zone.dat = "ZONE_ID",
               zone.spat = "TEN_ID",
               count = FALSE,
               var = "LANDED_OBSCURED", fun = "percent",
               breaks = seq(0, 2, .2),
               bin_colors = c("white", fishset_viridis(10)),
               output = "tab_plot", na.rm = TRUE)

zone_out$plot
zone_out$table %>% 
  pretty_lab(cols = c("LANDED_OBSCURED", "LANDED_OBSCURED_perc"), type = "scientific") %>%
  pretty_tab_sb(width = "60%")
```

<br><br>

### Trip Length

Average trip length for Access Area and Days at Sea fleet.
```{r}
zone_out <- 
  zone_summary(scallopMainDataTable, project = proj,
               spat = scallopTenMNSQRSpatTable, 
               zone.dat = "ZONE_ID",
               zone.spat = "TEN_ID",
               count = FALSE,
               var = "TRIP_LENGTH", fun = "mean",
               breaks = seq(2, 16, 2),
               output = "tab_plot", na.rm = TRUE)

zone_out$plot
zone_out$table %>% 
  pretty_lab(cols = "TRIP_LENGTH", type = "decimal") %>% 
  pretty_tab_sb(width = "40%")
```

<br><br>

### CPUE

Average CPUE for Access Area and Days at Sea fleets.
```{r}
zone_out <- 
  scallopMainDataTable %>% 
    filter(CPUE_p >= .025 & CPUE_p <= .975) %>%
    zone_summary(project = proj,
                 spat = scallopTenMNSQRSpatTable, 
                 zone.dat = "ZONE_ID",
                 zone.spat = "TEN_ID",
                 count = FALSE,
                 var = "CPUE", fun = "mean",
                 na.rm = TRUE, output = "tab_plot")
zone_out$plot
zone_out$table %>% 
  pretty_lab(cols = "CPUE") %>%
  pretty_tab_sb(width = "50%")
```

<br><br>

### VPUE


Average VPUE for Access Area and Days at Sea fleets.
```{r}
zone_out <- 
scallopMainDataTable %>% 
  filter(VPUE_p >= .025 & VPUE_p <= .975) %>% 
  zone_summary(project = proj,
               spat = scallopTenMNSQRSpatTable, 
               zone.dat = "ZONE_ID",
               zone.spat = "TEN_ID",
               count = FALSE,
               var = "VPUE", fun = "mean",
               breaks = seq(5e3, 3.5e4, 5e3),
               na.rm = TRUE, output = "tab_plot")
zone_out$plot
zone_out$table %>% 
  pretty_lab(cols = "VPUE") %>%
  pretty_tab_sb(width = "50%")
```

<br><br><br>

## Closure Summary


Number of observations in closure areas.
```{r}
zone_out <- 
  zone_summary(scallopMainDataTable, project = proj,
               spat = scallopWindCloseSpatTable, 
               zone.dat = "closeID",
               zone.spat = "NAME",
               count = TRUE,
               na.rm = TRUE, dat.center = FALSE, 
               output = "tab_plot")

zone_out$table %>% 
  pretty_lab() %>% 
  pretty_tab_sb(width = "40%")

zone_out$plot
```


<br><br><br>

Percent of observations in closure areas.
```{r}
zone_out <- 
  zone_summary(scallopMainDataTable, project = proj,
               spat = scallopWindCloseSpatTable, 
               zone.dat = "closeID",
               zone.spat = "NAME",
               fun = "percent",
               count = TRUE,
               na.rm = TRUE, dat.center = FALSE, 
               output = "tab_plot")

zone_out$table %>% 
  pretty_lab() %>% 
  pretty_tab_sb(width = "40%")

zone_out$plot
```

<br><br><br>

Percent of total revenue by closure area. 
```{r}
zone_out <- 
  zone_summary(scallopMainDataTable, project = proj,
               spat = scallopWindCloseSpatTable,
               zone.dat = "closeID",
               zone.spat = "NAME",
               var = "DOLLAR_OBSCURED",
               fun = "percent",
               count = FALSE, 
               na.rm = TRUE, dat.center = FALSE,
               output = "tab_plot")

zone_out$table %>% 
  pretty_lab() %>% 
  pretty_tab_sb(width = "70%")

zone_out$plot
```


<br><br><br>

Percent of total revenue by fleet. 
```{r}
zone_out <- 
  zone_summary(scallopMainDataTable, project = proj,
               spat = scallopWindCloseSpatTable,
               zone.dat = "closeID",
               zone.spat = "NAME",
               var = "DOLLAR_OBSCURED", group = "fleet",
               fun = "percent",
               count = FALSE, 
               na.rm = TRUE, dat.center = FALSE,
               output = "tab_plot")

zone_out$table %>% 
  pretty_lab() %>% 
  pretty_tab_sb(width = "70%")
zone_out$plot
```

<br><br><br>

Average meat catch per closure area.
```{r}
zone_out <- 
  zone_summary(scallopMainDataTable, project = proj,
               spat = scallopWindCloseSpatTable, 
               zone.dat = "closeID",
               zone.spat = "NAME",
               var = "LANDED_OBSCURED",
               fun = "mean",
               count = FALSE,
               na.rm = TRUE, dat.center = FALSE, 
               output = "tab_plot")

zone_out$table %>% 
  pretty_lab() %>% 
  pretty_tab_sb(width = "50%")

zone_out$plot
```

<br><br><br>

Average meat catch by fleet.
```{r}
zone_out <- 
  zone_summary(scallopMainDataTable, project = proj,
               spat = scallopWindCloseSpatTable, 
               zone.dat = "closeID",
               zone.spat = "NAME",
               var = "LANDED_OBSCURED", group = "fleet",
               fun = "mean",
               count = FALSE,
               na.rm = TRUE, dat.center = FALSE, 
               output = "tab_plot")

zone_out$table %>% 
  pretty_lab() %>% 
  pretty_tab_sb(width = "60%")

zone_out$plot
```

<br><br><br>

## Outliers

### POUNDS
```{r}
outlier_table(scallopMainDataTable, proj,
              x = "LANDED_OBSCURED") %>% 
  pretty_lab() %>% 
  pretty_tab()
```

```{r}
outlier_plot(scallopMainDataTable, proj,
             x = "LANDED_OBSCURED", 
             dat.remove = "none",
             x.dist = "normal",
             output.screen = TRUE)
```

```{r}
outlier_plot(scallopMainDataTable, proj,
             x = "LANDED_OBSCURED", 
             dat.remove = "mean_3SD",
             x.dist = "normal",
             output.screen = TRUE)
```

<br><br><br>

## Temporal Plots


```{r}
temp_plot(scallopMainDataTable, proj,
          var.select = "LANDED_OBSCURED",
          len.fun = "percent",
          agg.fun = "sum",
          date.var = "DATE_TRIP",
          pages = "multi")
```

<br><br><br>

```{r}
temp_plot(scallopMainDataTable, proj,
          var.select = "TRIP_LENGTH",
          len.fun = "percent",
          agg.fun = "sum",
          date.var = "DATE_TRIP",
          pages = "multi")
```

<br><br><br>

```{r}
temp_plot(scallopMainDataTable, proj,
          var.select = "DOLLAR_OBSCURED",
          len.fun = "percent",
          agg.fun = "sum",
          date.var = "DATE_TRIP",
          pages = "multi")
```

<br><br><br>

## Scatter Plots

Trip length by meat catch. 
```{r}
xy_plot(scallopMainDataTable, proj,
        var1 = "TRIP_LENGTH", var2 = "LANDED_OBSCURED",
        regress = FALSE, alpha = .3)
```


<br><br><br>

Trip length by revenue. 
```{r}
xy_plot(scallopMainDataTable, proj,
        var1 = "TRIP_LENGTH", var2 = "DOLLAR_OBSCURED",
        regress = FALSE, alpha = .3)
```


<br><br><br>

Trip length by trip cost (Winsor).
```{r}
xy_plot(scallopMainDataTable, proj,
        var1 = "TRIP_LENGTH", var2 = "TRIP_COST_WINSOR_2020_DOL",
        regress = FALSE, alpha = .3)
```

<br><br><br>

## Correlation Matrix

```{r fig.width=8.5, fig.height=6.5}
corr_outs <-
  corr_out(scallopMainDataTable, proj,
           variables = "all",
           method = "pearson", 
           show_coef = FALSE)

corr_outs$plot
corr_outs$table %>% 
  pretty_tab_sb(width = "100%")
```

<br><br><br>

## Active Vessels

vessel count by year. 
```{r}
ves_out <- 
  vessel_count(scallopMainDataTable, proj,
               v_id = "PERMIT.y",
               date = "DATE_TRIP",
               period = "year", type = "line")

ves_out$table %>% pretty_tab()
ves_out$plot 
```

<br><br><br>

vessel count by fleet. 
```{r}
ves_out <- 
  vessel_count(scallopMainDataTable, proj,
               v_id = "PERMIT.y",
               group = "fleet",
               output = "table")

ves_out$table %>% pretty_tab()
ves_out$plot 
```

<br><br><br>

vessel count by year and fleet. 
```{r}
ves_out <- 
  vessel_count(scallopMainDataTable, proj,
               v_id = "PERMIT.y",
               group = "fleet",
               date = "DATE_TRIP",
               period = "year", type = "line")

ves_out$table %>% 
  pretty_lab(cols = "PERMIT.y") %>% 
  pretty_tab_sb(width = "40%")

ves_out$plot 
```

<br><br><br>

vessel count by gearcode. 
```{r}
ves_out <- 
  vessel_count(scallopMainDataTable, proj,
               v_id = "PERMIT.y",
               group = "GEARCODE", tran = "log")

ves_out$table %>% 
  pretty_lab() %>% 
  pretty_tab()

ves_out$plot
```

<br><br><br>

## Catch

Total meat catch by year. 
```{r}
catch_out <- 
species_catch(scallopMainDataTable, proj,
              species = "LANDED_OBSCURED",
              date = "DATE_TRIP", 
              period = "year",
              fun = "sum",
              type = "line", format_lab = "decimal")

catch_out$table %>% 
  pretty_lab(cols = "LANDED_OBSCURED") %>%
  pretty_tab()

catch_out$plot
```

<br><br><br>

Total meat catch by fleet. 
```{r}
catch_out <- 
  species_catch(scallopMainDataTable, proj,
                species = "LANDED_OBSCURED",
                group = "fleet",
                fun = "sum",
                type = "bar", format_lab = "decimal")

catch_out$table %>% 
  pretty_lab(cols = "LANDED_OBSCURED") %>%
  pretty_tab()

catch_out$plot
```

<br><br><br>

Total meat catch by year and fleet. 
```{r}
catch_out <- 
  species_catch(scallopMainDataTable, proj,
                species = "LANDED_OBSCURED",
                date = "DATE_TRIP", 
                period = "year",
                group = "fleet",
                fun = "sum",
                type = "line", format_lab = "decimal")

catch_out$table %>% 
  pretty_lab(cols = "LANDED_OBSCURED") %>%
  pretty_tab_sb(width = "40%")

catch_out$plot
```

<br><br><br>

## CPUE 

Average CPUE by year.
```{r}
cpue_out <- 
  species_catch(scallopMainDataTable, proj,
                species = "CPUE",
                date = "DATE_TRIP", 
                period = "year",
                fun = "mean", type = "line")

cpue_out$table %>% 
  pretty_lab(cols = "CPUE") %>%
  pretty_tab()

cpue_out$plot
```

<br><br><br>

Average CPUE by year and fleet.
```{r}
cpue_out <- 
  species_catch(scallopMainDataTable, proj,
                species = "CPUE",
                date = "DATE_TRIP", 
                period = "year",
                group = "fleet",
                fun = "mean", type = "line")

cpue_out$table %>% 
  pretty_lab(cols = "CPUE") %>%
  pretty_tab_sb(width = "40%")

cpue_out$plot
```

<br><br><br>

## VPUE

Average VPUE by year.
```{r}
vpue_out <- 
  species_catch(scallopMainDataTable, proj,
                species = "VPUE",
                date = "DATE_TRIP", 
                period = "year",
                fun = "mean", type = "line")

vpue_out$table %>% 
  pretty_lab(cols = "VPUE") %>%
  pretty_tab()

vpue_out$plot
```

<br><br><br>

Average VPUE by year and fleet.
```{r}
vpue_out <- 
  species_catch(scallopMainDataTable, proj,
                species = "VPUE",
                date = "DATE_TRIP", 
                period = "year",
                group = "fleet",
                fun = "mean", type = "line")

vpue_out$table %>% 
  pretty_lab(cols = "VPUE") %>%
  pretty_tab_sb(width = "40%")

vpue_out$plot
```


<br><br><br>

Average VPUE by gearcode.
```{r}
vpue_out <- 
  species_catch(scallopMainDataTable, proj,
                species = "VPUE",
                group = "GEARCODE",
                fun = "mean", type = "line")

vpue_out$table %>% 
pretty_lab() %>%
  pretty_tab()

vpue_out$plot + angled_theme()
```

<br><br><br>

## Distributions


### LANDED_OBSCURED
KDE, ECDF, and CDF of meat catch. 
```{r}
density_plot(scallopMainDataTable, proj,
             var = "LANDED_OBSCURED", 
             type = "all", tran = "log")
```

<br><br>

KDE, ECDF, and CDF of meat catch by fleet. 
```{r}
density_plot(scallopMainDataTable, proj,
             var = "LANDED_OBSCURED",
             group = "fleet", position = "stack", 
             type = "all", tran = "log", pages = "multi")
```

<br><br>

KDE of meat catch by year.
```{r}
density_plot(scallopMainDataTable, proj,
             var = "LANDED_OBSCURED", 
             facet_by = "DB_LANDING_YEAR",
             filter_by = "DB_LANDING_YEAR",
             filter_value = 2007:2013,
             type = "kde", bw = 1.5, 
             tran = "log", scale = "free_y") + 
  angled_theme()

density_plot(scallopMainDataTable, proj,
             var = "LANDED_OBSCURED", 
             facet_by = "DB_LANDING_YEAR",
             filter_by = "DB_LANDING_YEAR",
             filter_value = 2014:2019,
             type = "kde", bw = 1.5, 
             tran = "log", scale = "free_y") + 
  angled_theme()
```

```{r echo = FALSE}
unlink(folderpath)
```

