---
title: "Poster Visualizations"
author: "Chris"
date: "2023-12-18"
output: html_document
output: 
  html_document: 
  theme: readable 
  highlight: zenburn 
    toc: true 
    toc_float: true 
    number_sections: true 
    code_folding: show 
    code_download: true 
editor: 
markdown: 
wrap: 72 
---

```{r}
#load packages
library(data.table)
library(dplyr)
library('ggplot2')
library("indicspecies")
library(kableExtra)
library(knitr)
library(RColorBrewer)
library(tidyr)
library(tidyverse)
library(vegan)
```

```{r}
#load data
algae<- read.csv("algaefull.csv")
head(algae)
```

```{r}
#pie charts for algae
#clean up the data first
```

```{r}
df <- algae[1:482, ] %>% select(-NUMBER, -ASSIGNED_NUMBER)

rockweed_section1_L <- df %>%
  filter(SECTION == 1, ZONATION == 'L') %>%
  summarise(rockweed_sum = sum(ROCKWEED, na.rm = TRUE)) 

rockweed_section1_L
```

```{r}
result <- algae %>%
  group_by(SECTION) %>%
  summarise(across(5:19, sum, na.rm = TRUE))








# remove number and assigned number columns & stop the data at line 482
df <- df[, !(names(df) %in% c("number", "assigned_number"))]
df <- df[1:482, ]

# can you sum the number of times "ulva" appears across each section and zonation?
df_ulva_sum <- df %>%
  group_by(section, zonation) %>%
  summarise(ulva_sum = sum(ulva, na.rm = TRUE), .groups = 'drop')


# Convert data types and handle missing values if any
algae <- algae %>%
  mutate(across(everything(), as.factor)) %>%
  replace(is.na(.), 0)  # Replace NA values with 0 (assuming NA means absence)

# Summarize data for each SECTION and ZONATION
summary_data <- ALGAE %>%
  group_by(SECTION, ZONATION) %>%
  summarize_all(sum) %>%
  gather(key = "ALGAE_TYPE", value = "COUNT", -SECTION, -NUMBER, -ASSIGNED_NUMBER, -ZONATION)


```

```{r}
#species richness and diversity
# Calculate species richness per section
#make sure you loaded the corrected data sets





df$species_richness <- apply(df, 1, function(x) sum(x > 0))

# Calculate overall species richness
overall_species_richness <- sum(apply(df, 2, function(x) sum(x > 0)) > 0)

# Calculate Shannon diversity index per section
df$shannon_diversity <- diversity(df)

# Calculate overall Shannon diversity index
overall_shannon_diversity <- diversity(colSums(df))

# Calculate abundance per section
df$abundance <- rowSums(df)

# Calculate overall abundance
overall_abundance <- sum(df$abundance)

# Output the results
df_richness_diversity_abundance <- df[, c("species_richness", "shannon_diversity", "abundance")]
overall_results <- data.frame(
  overall_species_richness = overall_species_richness,
  overall_shannon_diversity = overall_shannon_diversity,
  overall_abundance = overall_abundance
)

df_richness_diversity_abundance
overall_results
```

```{r}
library(vegan)

# Calculate species richness per section
df$species_richness <- apply(df, 1, function(x) sum(x > 0))

# Calculate Shannon diversity index per section
df$shannon_diversity <- diversity(df, index = "shannon")

# Calculate abundance per section
df$abundance <- rowSums(df)

# Calculate overall species richness
overall_species_richness <- sum(apply(df, 2, function(x) sum(x > 0)) > 0)

# Calculate overall Shannon diversity index
overall_shannon_diversity <- diversity(colSums(df), index = "shannon")

# Calculate overall abundance
overall_abundance <- sum(df$abundance)

# Output the results
df_richness_diversity_abundance <- df[, c("species_richness", "shannon_diversity", "abundance")]
overall_results <- data.frame(
  overall_species_richness = overall_species_richness,
  overall_shannon_diversity = overall_shannon_diversity,
  overall_abundance = overall_abundance
)

list(
  per_section = df_richness_diversity_abundance,
  overall = overall_results
)
```

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

# Calculate species richness, diversity, and abundance per section
df_stats <- df %>%
  mutate(species_richness = rowSums(. > 0),
         shannon_diversity = diversity(., index = "shannon"),
         abundance = rowSums(.))

# Calculate overall species richness, diversity, and abundance
overall_species_richness <- sum(colSums(df > 0) > 0)
overall_shannon_diversity <- diversity(colSums(df), index = "shannon")
overall_abundance <- sum(df_stats$abundance)

# Prepare data for pie chart of all the porifera counts
porifera_counts <- df %>%
  select(contains("porifera")) %>%
  colSums()

# Plot pie chart of all the porifera counts
porifera_data <- data.frame(species = names(porifera_counts), count = porifera_counts)
ggplot(porifera_data, aes(x = "", y = count, fill = species)) +
  geom_bar(width = 1, stat = "identity") +
  coord_polar(theta = "y") +
  theme_void() +
  labs(fill = "Porifera Species")

```

```{r}
#repeat porifera with cnidaria
library(ggplot2)
library(dplyr)

# Calculate species richness, diversity, and abundance per section
df_stats <- df %>%
  mutate(species_richness = rowSums(. > 0),
         shannon_diversity = diversity(., index = "shannon"),
         abundance = rowSums(.))

# Calculate overall species richness, diversity, and abundance
overall_species_richness <- sum(colSums(df > 0) > 0)
overall_shannon_diversity <- diversity(colSums(df), index = "shannon")
overall_abundance <- sum(df_stats$abundance)

# Prepare data for pie chart of the cnidaria counts
cnidaria_counts <- df %>%
  select(contains("cnidaria")) %>%
  colSums()

# Plot pie chart of the cnidaria counts
cnidaria_data <- data.frame(species = names(cnidaria_counts), count = cnidaria_counts)
ggplot(cnidaria_data, aes(x = "", y = count, fill = species)) +
  geom_bar(width = 1, stat = "identity") +
  coord_polar(theta = "y") +
  theme_void() +
  labs(fill = "Cnidaria Species")


```

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

# Calculate species richness, diversity, and abundance per section
df_stats <- df %>%
  mutate(species_richness = rowSums(. > 0),
         shannon_diversity = diversity(., index = "shannon"),
         abundance = rowSums(.))

# Calculate overall species richness, diversity, and abundance
overall_species_richness <- sum(colSums(df > 0) > 0)
overall_shannon_diversity <- diversity(colSums(df), index = "shannon")
overall_abundance <- sum(df_stats$abundance)

# Prepare data for pie chart of the species counts
species_counts <- colSums(df)

# Plot pie chart of the species counts
species_data <- data.frame(species = names(species_counts), count = species_counts)
ggplot(species_data, aes(x = "", y = count, fill = species)) +
  geom_bar(width = 1, stat = "identity") +
  coord_polar(theta = "y") +
  theme_void() +
  labs(fill = "Species")
```

```{r}
#stacked bar
library(vegan)
library(ggplot2)
library(dplyr)
library(tidyr)

# Calculate species richness (number of species present) and diversity (Shannon index)
df$species_richness <- apply(df[, -1], 1, function(x) sum(x > 0))
df$diversity <- apply(df[, -1], 1, function(x) diversity(x))

# Summarize species richness and diversity by section
richness_diversity_by_section <- df %>%
  group_by(section) %>%
  summarize(
    average_richness = mean(species_richness),
    average_diversity = mean(diversity)
  )

# Print the summarized data
print(richness_diversity_by_section)

# Prepare data for stacked bar plot
df_long <- df %>%
  gather(key = "species", value = "count", -section, -species_richness, -diversity)

# Plot stacked bar chart of species counts for each section
ggplot(df_long, aes(x = factor(section), y = count, fill = species)) +
  geom_bar(stat = "identity") +
  theme_minimal() +
  labs(x = "Section", y = "Count", fill = "Species") +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))

```

```{r}
#done by zone
# Species richness (number of species)
species_richness <- specnumber(df)

# Shannon diversity index
shannon_diversity <- diversity(df, index = "shannon")

# Simpson's diversity index
simpson_diversity <- diversity(df, index = "simpson")

# Combine the results into a data frame
diversity_measures <- data.frame(
  Species_Richness = species_richness,
  Shannon_Diversity = shannon_diversity,
  Simpson_Diversity = simpson_diversity
)

# Display the results
print(diversity_measures)
```

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

# Assuming 'df' is already loaded in the environment

# Reshape the data frame to a long format
df_long <- df %>%
  gather(key = "species", value = "count", -section)

# Create a pie chart for each section
ggplot(df_long, aes(x = factor(1), fill = species, weight = count)) +
  geom_bar(width = 1) +
  coord_polar(theta = "y") +
  facet_wrap(~ section, scales = "free_x") +
  theme_void() +
  theme(legend.position = "bottom")
```

```{r}
# Filter the data for section 1
df_section1 <- df %>% filter(section == 1)

# Drop the 'section' column as it's no longer needed
df_section1 <- df_section1 %>% select(-section)

# Reshape the data frame to a long format
df_section1_long <- df_section1 %>%
  gather(key = "species", value = "count")

# Create a pie chart for section 1
ggplot(df_section1_long, aes(x = "", y = count, fill = species)) +
  geom_bar(stat = "identity", width = 1) +
  coord_polar("y") +
  theme_void() +
  theme(legend.position = "bottom")
```

```{r}
#nmds plain
library(vegan)

# Subset the data to include only the species count columns
df_species <- df[, -1]

# Run NMDS on the species count data
nmds <- metaMDS(df_species, distance = "bray", k = 2)

# Plot the NMDS
plot(nmds$points[,1], nmds$points[,2], xlab = "NMDS1", ylab = "NMDS2", main = "NMDS of Sections")
text(nmds$points[,1], nmds$points[,2], labels = df$section, pos = 3)
```

```{r}
#nmds with color coded dots
library(vegan)
library(ggplot2)

# Subset the data to include only the species count columns
df_species <- df[, -1]

# Run NMDS on the species count data
nmds <- metaMDS(df_species, distance = "bray", k = 2)

# Convert section to a factor for color coding
df$section <- as.factor(df$section)

# Create a data frame for plotting
nmds_df <- data.frame(nmds$points, section = df$section)

# Plot the NMDS with ggplot2 and color code by section
ggplot(nmds_df, aes(x = MDS1, y = MDS2, color = section)) +
  geom_point() +
  theme_minimal() +
  labs(x = "NMDS1", y = "NMDS2", title = "NMDS of Sections") +
  scale_color_discrete(name = "Section")
```

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

# Assuming 'df' is your data frame and it's already loaded in the environment

# Remove columns beyond 'species_code'
df_simplified <- df %>%
  select(site_filename, species_code)

# Create a stacked bar plot for each 'site_filename'
ggplot(df_simplified, aes(x = site_filename, fill = species_code)) +
  geom_bar(position = "stack") +
  labs(x = "Site Filename", y = "Count", fill = "Species Code") +
  theme_minimal()
```

```{r}
ibrary(vegan)

# Remove columns beyond 'species_code'
df_simplified <- df %>%
  select(site_filename, species_code)

# Calculate species richness (number of species)
species_richness <- df_simplified %>%
  group_by(site_filename) %>%
  summarise(richness = n_distinct(species_code))

# Calculate species diversity using Shannon index
# First, create a table of counts of species_code within each site_filename
species_table <- table(df_simplified$site_filename, df_simplified$species_code)

# Calculate Shannon diversity index for each site
diversity_index <- diversity(species_table)

# Combine richness and diversity into a single data frame
diversity_data <- data.frame(site_filename = names(diversity_index),
                             richness = species_richness$richness,
                             shannon_diversity = diversity_index)

# Print the species richness and diversity for each site_filename
print(diversity_data)
```

```{r}
# Remove columns beyond 'species_code'
df_simplified <- df %>%
  select(site_filename, species_code)

# Count the occurrences of each species_code within each site_filename
species_counts <- df_simplified %>%
  group_by(site_filename, species_code) %>%
  summarise(count = n()) %>%
  ungroup()

# Create a pie chart of species_codes for each site_filename
ggplot(species_counts, aes(x = "", y = count, fill = species_code)) +
  geom_bar(stat = "identity", width = 1) +
  coord_polar("y", start = 0) +
  facet_wrap(~site_filename) +
  labs(x = NULL, y = NULL, fill = "Species Code") +
  theme_void() +
  theme(legend.position = "right")
```

```{r}
#individual plots
# Remove columns beyond 'species_code'
df_simplified <- df %>%
  select(site_filename, species_code)

# Filter for 'postpoint' site_filename
postpoint_species <- df_simplified %>%
  filter(site_filename == "postpoint")

# Count the occurrences of each species_code within 'postpoint'
species_counts <- postpoint_species %>%
  group_by(species_code) %>%
  summarise(count = n()) %>%
  ungroup()

# Create a pie chart of species_codes for 'postpoint'
ggplot(species_counts, aes(x = "", y = count, fill = species_code)) +
  geom_bar(stat = "identity", width = 1) +
  coord_polar("y", start = 0) +
  labs(x = NULL, y = NULL, fill = "Species Code") +
  theme_void() +
  theme(legend.position = "right")
```

```{r}
library(vegan)

# Calculate species richness (the number of species present)
df$species_richness <- rowSums(df > 0)

# Calculate diversity indices
diversity_indices <- data.frame(
  Shannon = diversity(df, index = "shannon"),
  Simpson = diversity(df, index = "simpson"),
  InverseSimpson = diversity(df, index = "invsimpson")
)

# Combine species richness with diversity indices
combined_results <- cbind(df$species_richness, diversity_indices)

# Display the combined results
print(combined_results)
```

```{r}
library(ggplot2)

# Sum the counts for each class
class_sums <- colSums(df)

# Convert to data frame for ggplot
class_data <- data.frame(Class = names(class_sums), Count = class_sums)

# Create the pie chart
ggplot(class_data, aes(x = "", y = Count, fill = Class)) +
  geom_bar(stat = "identity", width = 1) +
  coord_polar("y", start = 0) +
  theme_void() +
  scale_fill_discrete(name = "Class") +
  theme(legend.title = element_text(size = 12))
```
```{r}
#pie chart with case change in legend
library(ggplot2)
library(dplyr)
library(tidyr)

# Assuming 'df' is your data frame and it contains one row of counts for each group
df_long <- df %>%
  pivot_longer(cols = everything(), names_to = "Class", values_to = "Count") %>%
  mutate(Class = tools::toTitleCase(Class))

ggplot(df_long, aes(x = "", y = Count, fill = Class)) +
  geom_bar(stat = "identity", width = 1) +
  coord_polar("y", start = 0) +
  theme_void() +
  theme(legend.title = element_text(size = 12)) +
  guides(fill = guide_legend(title = "Class")) +
  labs(fill = "Class")
```

