Map: This is a comprehensive geographical map representating the physical points at which whales were tagged.


CEE Summary: In the form of a Sunburst map, this represents a summary of the number of data points of tags recorded during each CEE.


Overall Distribution of Tags: Through the use of a Calendar Heatmap, we can easily see the distribution of data over the study.


Tag Longevity: Line graphs present the distribution of dive location and series data.


Distribution of Dive Depths: Histograms allow us to see the distribution of dives done at different depths for each animal.


---
title: "Showcase"
output: 
  flexdashboard::flex_dashboard:
    storyboard: true
    social: menu
    vertical_layout: fill
    source: embed
---


```{r setup, include=FALSE}
library(flexdashboard)
```


### Map: This is a comprehensive geographical map representating the physical points at which whales were tagged.

```{r}
library(ggplot2)
library(ggspatial)
library(DT)
library(tidyverse)
library(metR)
library(data.table)
library(dygraphs)
library(BRSPackage)
library(leaflet)
library(RColorBrewer)
library(KernSmooth)

# real <- read.csv("realdataCEE.csv")
# loc3 <- read.csv("processed_loc_new.csv")
gonio <- processed_gonio_aux_locs

palette_OkabeIto <- c("#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7", "#999999")

      
        leaflet() %>% 
            addProviderTiles(providers$Esri.OceanBasemap) %>%
            setView(lng = -75.51995, lat = 36, zoom = 6) %>%
        addCircleMarkers(data = gonio, ~Longitude, ~Latitude, 
                         radius = 3,
                         color = "#044F7E",
                         stroke = FALSE,
                         fillOpacity = 0.5) 



```

***
- The locations of tag recordings off of Cape Hatteras are shown visually in this map...
- Leaflet is used as a tool to allow for interactivity.


### CEE Summary: In the form of a Sunburst map, this represents a summary of the number of data points of tags recorded during each CEE. 

```{r}
library(sunburstR)
library(htmltools)
library(d3r)
library(BRSPackage)
cee_data <- data.frame(
  level1 = c(rep("CEE_19-01", each = length(unique(cee19_01$DeployID))), 
             rep("CEE_19-02", each = length(unique(cee19_02$DeployID))),
             rep("CEE_19-03", each = length(unique(cee19_03$DeployID))),
             rep("CEE_19-04", each = length(unique(cee19_04$DeployID)))),
  level2 = c(unique(cee19_01$DeployID), unique(cee19_02$DeployID),
             unique(cee19_03$DeployID), unique(cee19_04$DeployID)),
  size =  c(cee19_01$num_fixes, cee19_02$num_fixes,
            cee19_03$num_fixes, cee19_04$num_fixes)
)
tree <- d3_nest(cee_data, value_cols = "size")

sund2b(tree, width = "100%")
```

***
- The data gathered in this visualization is solely a collection of the number of data points recorded during each Controlled Exposure Experiment (CEE) and reveals little about the data of each tag itself. 
- Starting from the center, the entire count of tags recorded is represented. As you pan your mouse outward, you can see information about the number of tags, within two different CEEs, and subsequently within whales tagged during each CEE.
- To create this map, the number of data collections was summed and sectioned into each category so that it could be visualized.



### Overall Distribution of Tags: Through the use of a Calendar Heatmap, we can easily see the distribution of data over the study.



```{r}
library(data.table)
library(ggplot2)
library(readr)
library(ggTimeSeries)
library(lubridate)
library(dplyr)
library(stringr)
library(gridExtra)
library(BRSPackage)
library(RColorBrewer)


location <- processed_gonio_aux_locs %>% 
  mutate(day = lubridate::date(date_time))

step2a <- location %>% 
  group_by(day) %>%
  summarize(n_locs =n())

all_dates_a <- data.frame(day = seq(step2a$day[1],
                                          step2a$day[nrow(step2a)], 'days'))


df_merge_a <- left_join(all_dates_a, step2a, by = 'day')

colnames(df_merge_a) <- c('DateCol','ValueCol')

all_spp <- ggplot_calendar_heatmap(
  df_merge_a,
  'DateCol',
  'ValueCol',
)


all_spp +
  scale_fill_continuous(low = '#deebf7', high = '#08306b', name = "# ARGOS\nLocations\nper Day")+
  labs(title = "All Tags")
```

***
- Taking a step further into the data, dates with more tag data are represented has having a darker shade while days with fewer will have a lighter shade.
- On a calendar, it is easier to see on which days there may be clumps of data. For example, on June 2 there are a lot of data points as this is just a few days before CEE 2. An influx of new tags along with goniometer data contribute to this increase of data points 



### Tag Longevity: Line graphs present the distribution of dive location and series data.

```{r}
library(ggplot2)
library(BRSPackage)
library(dplyr)
library(lubridate)
library(gridExtra)

library(colorblindr)
accent_OkabeIto <- palette_OkabeIto[c(1, 2, 7, 4, 5, 3, 6)]
accent_OkabeIto[1:4] <- desaturate(lighten(accent_OkabeIto[1:4], .4), .8)
accent_OkabeIto[5:7] <- darken(accent_OkabeIto[5:7], .3)


processed_gonio_aux_locs<- processed_gonio_aux_locs %>% 
  mutate(species = stringr::str_sub(DeployID, 1, 2))


dsub<- processed_gonio_aux_locs %>% 
  filter(species =='Zc') %>% 
  mutate(day = lubridate::date(date_time)) %>% 
  group_by(day) %>% 
  mutate(n_sights= n(),
         label = paste0(DeployID, '_xy'))


# # Plot x,y temporal extent
# ggplot(dsub, aes(x = day, factor(DeployID), size=n_sights))+
#   geom_point(alpha = 0.05)+
#   labs(x = "Date-time", y = 'Deployment ID')+
#   geom_vline(xintercept= as.Date("2019-05-15"))+
#   geom_vline(xintercept= as.Date("2019-06-07"))+
#   geom_vline(xintercept= as.Date("2019-08-06"))+
#   geom_vline(xintercept= as.Date("2019-08-19"))+
#   theme(text = element_text(size = 12))+
#   theme_bw()+ 
#   scale_size(breaks = c(5, 10, 15, 20, 50), range = c(1, 5))
make_names <- function(x) {
  make_names <- make.names(colnames(x))
  make_names <- gsub("\\.", "_", make_names)
  make_names <- tolower(make_names)
  colnames(x) <- make_names
  x
}

daflocs <- processed_gonio_aux_locs %>% 
  mutate(species = str_sub(DeployID, 1, 2)) %>% 
  filter(species == 'Zc') %>% 
  make_names() 


series_files <- list.files("/Users/rob/Documents/research/rrr/schick_rl/data/raw_data", pattern = glob2rx("*Series.csv"), full.names = TRUE) %>% 
  map_dfr(read_csv)%>% 
  mutate(DeployID = str_sub(DeployID, 1, 8)) %>% 
  mutate(date_time = as.POSIXct(paste(Day, as.character(Time)), format = "%d-%B-%Y %H:%M:%S")) %>%
  make_names()


series<- series_files %>% 
  mutate(species = stringr::str_sub(deployid, 1, 2))

thesub <- series %>% 
  filter(species =='Zc') %>% 
  mutate(day = lubridate::date(date_time),
         label = paste0(deployid, "_z")) 

cee_dat <- read_csv("/Users/rob/Documents/research/projects/brs_data/cee_metadata_flat.csv") %>% 
  filter(cee_id %in% c('19_01', '19_02', '19_03', '19_04')) %>% 
  mutate(date_time = lubridate::ymd_hms(paste0(date_YYYYMMDD, start_HHMMSS)))

ggplot(data = daflocs, aes(date_time, deployid, group = deployid))+
  geom_line(color = accent_OkabeIto[5], size = 2)+
  geom_line(data = series_files, color = accent_OkabeIto[2])+
  geom_vline(xintercept = cee_dat$date_time, lty = 2)+
  theme_minimal()+
  labs(x = 'Date', y = '', title = '2019 Deployment Information',
       subtitle = 'Thick line (x,y); Thin line (z)')

```

***
- **Locations:** While the calendar heatmap gives a good overview of all the data, here we depict the temporal extent of data for each individual animal. Each animal's tag filtered tag data are shown with circles, the size of which correspond to the number of sightings per individual per day. These come from satellite sags, Goniometer hits, and visual sightings. Green squares indicate the first and last day of the depth data recorded through the series data collection. Each vertical line represents the start of each of the 4 CEE's. 4 animals were exposed to multiple CEEs (082, 083, 092, and 093). 


### Distribution of Dive Depths: Histograms allow us to see the distribution of dives done at different depths for each animal.


```{r}
library(ggplot2)
library(BRSPackage)
library(dplyr)

series_plot <- series_files %>% 
  mutate(dive_status = if_else(depth > 800, 'Deep', 'Shallow')) %>% 
  filter(!(deployid %in% c('ZcTag090', 'ZcTag094'))) 

ggplot(series_plot, aes(y = -1 * depth, fill=dive_status))+
  geom_histogram(binwidth = 100)+
  labs(y = 'Depth (m)', x = 'Count',
       title = 'Dive Summaries by Individual')+
  scale_fill_brewer(2, 'Paired')+
  scale_fill_manual(values = c('#0072B2', '#bdbdbd' ))+
  theme_bw()+
  facet_wrap(~ deployid, scales = 'free_y')+
  labs(y = 'Depth (m)', x = 'Count',
       title = 'Dive Summaries by Individual', fill = 'Dive State', x = '')

```

*** 
- Past literature has determined that a dive is considered deep for a _Z. cavirostris_ once it is past 800m (Shearer et al., 2019). Subsequently, each histogram denotes an individuals tag, frequency of dive depth, and color coded using this method. 
- A point to note, the depth sensors on tags ZcTag094 and ZcTag090 failed, which leads to the odd readings for both of these animals.