---
title: "Seal Project Visualizations"
author: "Chris"
date: "2023-07-28"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r}
#Use library to call your installed packages for use
library(data.table)
library(dplyr)
library('ggplot2')
library("indicspecies")
library(kableExtra)
library(knitr)
library(RColorBrewer)
library(tidyr)
library(tidyverse)
library(vegan)


```

# Files Used

seal.csv - Histogram of seal behavior

```{r}

# Read the data from the CSV file
data <- read.csv("seal.csv")

# Convert the DATE OF OBSERVATION column to Date format
data$DATE.OF.OBSERVATION <- as.Date(data$DATE.OF.OBSERVATION, format = "%m/%d/%Y")

# Calculate the total behavior frequency per day
total_behavior_frequency <- aggregate(OCCURENCE ~ DATE.OF.OBSERVATION, data, sum)

# Plot the histogram of behaviors per day
ggplot(data, aes(x = DATE.OF.OBSERVATION, y = OCCURENCE, fill = BEHAVIOUR)) +
  geom_bar(stat = "identity", position = "dodge") +
  theme_minimal() +
  labs(x = "Date of Observation", y = "Behavior Frequency", title = "Histogram of Behaviors Per Day")

# Plot the histogram of total behavior frequency across all days
ggplot(total_behavior_frequency, aes(x = DATE.OF.OBSERVATION, y = OCCURENCE)) +
  geom_bar(stat = "identity", fill = "blue") +
  theme_minimal() +
  labs(x = "Date of Observation", y = "Total Behavior Frequency", title = "Histogram of Total Behavior Frequency")


```

# Grouped Bar

I don't like the histogram of frequency, so I wanted to try this instead

```{r}

# Convert the DATE OF OBSERVATION column to Date format
data$DATE.OF.OBSERVATION <- as.Date(data$DATE.OF.OBSERVATION, format = "%m/%d/%Y")


# Plot the stacked bar plot for daily frequency of each behavior
ggplot(data, aes(x = DATE.OF.OBSERVATION, y = OCCURENCE, fill = BEHAVIOUR)) +
  geom_bar(stat = "identity") +
  theme_minimal() +
  labs(x = "Date of Observation", y = "Behavior Frequency", title = "Daily Frequency of Each Behavior") +
  scale_fill_discrete(name = "Behavior")

```

# Behavior differences by age group
```{r}

data<- read.csv("play.csv")

# Create a new column to represent play behavior as a factor
data$Play <- factor(data$Play, levels = c("Y", "N"), labels = c("Play", "Non-Play"))

# Plot the stacked bar plot for play versus non-play behavior
ggplot(data, aes(x = Life_Stage, fill = Play)) +
  geom_bar(position = "fill") +
  theme_minimal() +
  labs(x = "Life Stage", y = "Proportion", title = "Play Behavior vs. Non-Play Behavior") +
  scale_fill_discrete(name = "Behavior")

```
# Behavior Pie Chart
```{r}

seal<- read.csv("seal2.csv")

# Change the legend order of the categories
#custom_order <- c("Other", "Human Disturbance", "Land + Water", "Water", "Land")

# Define custom colors for each category
custom_colors <- c("Land" = "seagreen", 
                   "Water" = "royalblue", 
                   "Other" = "lightgray", 
                   "Human Disturbance" = "coral", 
                   "Land + Water" = "purple3")

behavior_summary <- aggregate(Occurrence ~ Category, data = seal, sum)

# Reorder the Category levels to match your desired order
behavior_summary$Category <- factor(behavior_summary$Category, levels = c("Land", "Water", "Land + Water", "Human Disturbance", "Other"))

# Calculate the percentage for each category
behavior_summary$Percentage <- behavior_summary$Occurrence / sum(behavior_summary$Occurrence) * 100

# Create the pie chart with custom colors and percentages at the end of pie slices
ggplot(behavior_summary, aes(x = "", y = Occurrence, fill = Category)) +
  geom_bar(stat = "identity", width = 1) +
  coord_polar("y") +
   geom_text(aes(x = 1.15, label = paste0(round(Percentage), "%")), 
            position = position_stack(vjust = 0.5), 
            size = 3, 
            fontface = "bold",
            color = "black",
            show.legend = FALSE) + # Percentage at the end of pie slices
  scale_fill_manual(values = custom_colors) +  # Use custom colors
  theme_void() +
  labs(title = "Proportion of Seal Behaviors by Category",
       fill = "Category") +
  theme(legend.position = "right")


```

