Start by making sure you have all the necessary packages installed and loaded.

If needed, set your working directory using setwd()

install.packages('tinytex')
## Error in `contrib.url()`:
## ! trying to use CRAN without setting a mirror

The Assignment

Your goal for this assignment is to find a dataset online that includes species abundances across different plots, sites, quadrats, or regions. You will conduct diversity analyses on the dataset to compare diversity among these areas.

Your dataset must include at least 3 sites/plots, 10 species, and abundance values (counts).

Data processing tips

Depending on how your raw data is formatted online, here are some commands to process it into a clean site-by-species matrix for R.

If each row is an individual organism (e.g., individual animal sightings): Count the individuals per species at each site.

If species are listed as separate column names (Wide Format): Pivot the site columns into a long format first before building your matrix.

If you have duplicate site/species rows (e.g., surveyed across multiple dates): Sum the abundances together across time.

Convert any long data frame to a vegan-ready community matrix:

Load and explore your dataset

1. Describe your dataset

What dataset did you choose? What study is it from? What data does the data contain (species, sites, etc)? How was the data collected?

[The dataset I chose to use was from a long-term fish monitoring project, taking place from 1964-to present. It takes place in the Elkhorn Slough, integrating research from from many researchers and institutions such as: Moss Landing Marine Laboratories, Elkhorn Slough National Estuarine Research Reserve, and (historical data) Monterey Bay National Marine Sanctuary. The data includes an abundance of variables: Data, Year, Month, Season, Sub-Region, Area, Method, #_Fisherman, Species, Methods, and many more. However, I will be specifically looking at the Region, Count of Fish captured, and Species. The data was collected through various methods, being identified, measured, and released. Some of these methods including: Seine, Gill Net, Trawl, Hook and Line, Passive Traps, Diver, Fish-reported, BRUV, and other unspecificed methods. The data was captured across 29 sites.

2. What are you hoping to test? What is your hypothesis?

[With prior knowledge that Estuaries support high levels of Biodiversity, I want to see if that is true with the Elkhorn Slough when compared to other Regions such as just the harbor (salt water) and the Old Salinas River Channel (Freshwater). Estuaries have brackish water where the fresh and salt water mix, creating a transition zone with less salinity than the ocean. My hypothesis is that the Elkhorn Slough will support greater species diversity due to its high nutrients availability and presence of transition zones, allowing for a greater number of species to be present.

Read in your data

NOTE: Your data may not be in .csv format. If it is an excel file or .tsv, you can open it and save it instead as a .csv. You can also use alternative commands to load in these other data formats, if you prefer (e.g. read_xlsx), but note that this could complicate the code given in the example. If you are new to R, get your data into .csv format.

# Load the dataset
fishdata <- read.csv("Fish_Survey.csv")

cleanfishdata <- fishdata %>%
  select (Region, Species, Count = X._Individuals) %>%
  filter (Species != "Nothing" , Region != "NA")

cleanfishdata$Count[is.na(cleanfishdata$Count)] <-0

Take a closer look

Reformat your data

4. Clean and wrangle your data, describing why you are conducting the cleaning steps you are taking.

Hint: it will be helpful to have 3 objects: your clean dataset in long format, your clean data in wide format, and your clean dataset set-up as a community data matrix.

[First, we need to load the whole data set, where it includes Dates, times, and additional information that was collected and displayed in the raw data set. Next, we want to rename the data set, to make sure we know that it has been filtered out. Here, we rename the data cleanfishdata. Next, we want to select only the desire data, which consists of Region, Species, and the X_Individuals (rename it to count). After, we will filter out the species that states “nothing” and region that states “NA”. Finally, we want to give the count values “NA” a value of 0. ]

# Load the dataset
fishdata <- read.csv("Fish_Survey.csv")
cleanfishdata <- fishdata %>%
  select (Region, Species, Count = X._Individuals) %>%
  filter (Species != "Nothing" , Region != "NA")
cleanfishdata$Count[is.na(cleanfishdata$Count)] <-0

#Full Clean Fish Data

fishdata <- read.csv("Fish_Survey.csv")

cleanfishdata <- fishdata %>%
  select (Region, Species, Count = X._Individuals) %>%
  filter (Species != "Nothing" , Region != "NA")

cleanfishdata$Count[is.na(cleanfishdata$Count)] <-0

Calculating basic diversity metrics

5. Calculate and compare diversity metrics among at least 3 plots/sites/areas/etc.

What do you observe? Does this align with what you expect from your plots/data above? Hint: there were 3 different diversity metrics in lab, with several ways to measure two of them.

[The Gamma diversity is 173. This is what I would expect, meaning there is a total of 173 unique species across all 6 Regions where the fish were caught. The Alpha Diversity for each region is:

Region Alpha Diversity
Elkhorn Slough 97
Harbor 96
Offshore 49
Old Salinas River Channel 11
Bennett Slough 44

The Whattakers beta diversity has a value of 3.4949, meaning that the species variation across the 6 regions is significant. The highest alpha diversity is Elkhorn Slough, which is what I was expecting and stated in my hypothesis. However, the diversity isn’t as significantly larger when compared to Harbor, which was one less in alpha diversity. The Old Salinas River Channel, I was expecting there to be a small alpha diversity value as it is only freshwater species. ]

Gamma Diversity

length(cleanfishdata$Species)
## [1] 6426
gamma_diversity <- length(unique(cleanfishdata$Species))
gamma_diversity
## [1] 173

Alpha Diversity

region_fish <- cleanfishdata %>%
  group_by(Region, Species) %>%
  summarize(Count = sum(Count), .groups = "drop")

alpha_zones <- region_fish %>%
  filter(Count > 0) %>%
  group_by(Region) %>%
  summarize(alpha_diversity = n_distinct(Species))

alpha_zones %>% 
  filter(Region %in% c("Bennett Slough", "Elkhorn Slough", "Harbor", "Offshore", "Pajaro River", "Old Salinas River Channel"))

Getting the fish_matrix to help calculate the Whittaker’s Beta Diversity

fish_matrix <- region_fish %>%
  pivot_wider(names_from = Species, values_from = Count, values_fill = list(count = 0)) %>%
  tibble::column_to_rownames("Region")
fish_matrix[is.na(fish_matrix)] <- 0

Whittaker’s Beta Diversity

alpha_per_region <- specnumber(fish_matrix)
alpha_bar <- mean(alpha_per_region)
beta_whittaker <- gamma_diversity / alpha_bar
beta_whittaker
## [1] 3.494949

Calculating diversity comparisons

6. Calculate and compare Bray-Curtis and Jaccard distances for your data (all species and all plots). Create an NMDS plot with all data for both Bray-Curtis and Jaccard distances for your data. What do you observe?

[When comparing the Jaccard Distance, we see that Elkhorn Slough and the Harbor are the most similar out of the Regions. They have a value of .58, or 58%, meaning 42% of the fish communties between the two regions is different. The closer the value is to 0 means the more identical. According to Bray-Curtis shows us that Elkhorn Slough and Harbor shared the most similar fish assembalaged, about 59% of the same abundance. Observing the NMDS plot, we see that harbor and Elkhorn Slough have the most distinct/seprerate fish communities, while the the other locations are hard to visually seprerate. These were the two Regions that had the lowest values when calculating. The other values are stated below. For Jaccard, the second most similarity was between the Elkhorn and Bennett Slough, valued at .63, or 63%, meaning that 37% of the species found at each location were the same. The second for Bray-curtis was Elkhorn and Bennett Slough once again, 46%. Looking at the Rank abundance curve, the Elkhorn Slough has a sharp decline with the highest count, but a few species dominant. The Harbor has a gradual decline, meaning a greater species distribution and diversity. Offshore and Bennet Slough have a steep drop with few a species dominating, but with a smaller total count. ]

Jaccard Distance

jaccard_dist <- vegdist(fish_matrix, method = "jaccard", binary = TRUE)
## Warning in vegdist(fish_matrix, method = "jaccard", binary = TRUE): you have empty rows: their dissimilarities may be
##                  meaningless in method "jaccard"
jaccard_dist
##                           Bennett Slough Elkhorn Slough    Harbor  Offshore
## Elkhorn Slough                 0.6310680                                   
## Harbor                         0.6407767      0.5808824                    
## Offshore                       0.7600000      0.7731092 0.7391304          
## Old Salinas River Channel      0.8541667      0.9200000 0.9300000 0.9090909
## Pajaro River                   1.0000000      1.0000000 1.0000000 1.0000000
##                           Old Salinas River Channel
## Elkhorn Slough                                     
## Harbor                                             
## Offshore                                           
## Old Salinas River Channel                          
## Pajaro River                              1.0000000

Bray-Curtis Distance (Sorenson Beta Diversity)

sorensen_dist <- vegdist(fish_matrix, method = "bray", binary = TRUE)
## Warning in vegdist(fish_matrix, method = "bray", binary = TRUE): you have empty rows: their dissimilarities may be
##                  meaningless in method "bray"
sorensen_dist
##                           Bennett Slough Elkhorn Slough    Harbor  Offshore
## Elkhorn Slough                 0.4609929                                   
## Harbor                         0.4714286      0.4093264                    
## Offshore                       0.6129032      0.6301370 0.5862069          
## Old Salinas River Channel      0.7454545      0.8518519 0.8691589 0.8333333
## Pajaro River                   1.0000000      1.0000000 1.0000000 1.0000000
##                           Old Salinas River Channel
## Elkhorn Slough                                     
## Harbor                                             
## Offshore                                           
## Old Salinas River Channel                          
## Pajaro River                              1.0000000

Rank Abundance Curve

rank_df <- region_fish %>%
  group_by(Region) %>%
  arrange(Region, desc(Count)) %>%
  mutate(rank = row_number())

Rank Abundance Curve Visualization:

ggplot(rank_df, aes(x = rank, y = log(Count + 1), color = Region)) +
  geom_line(linewidth = 1) +
  geom_point(size = 2) +
  labs(title = "Rank Abundance Curves by Region",
       x = "Species Abundance Rank",
       y = "Log(Abundance + 1)",
       color = "Zone") +
  theme_minimal()

Pre NMDS Plots (Distance and Ordination)

library(dplyr)
library(tidyr)

quadrat_matrix <- cleanfishdata %>%
  group_by(Region, Species) %>%
  mutate(entry_id = row_number()) %>%
  ungroup() %>%
  pivot_wider(id_cols = c(Region, entry_id),
              names_from = Species, values_from = Count, values_fill = 0)

quadrat_metadata <- quadrat_matrix %>% select(Region, entry_id)
quadrat_species  <- quadrat_matrix %>% select(-Region, -entry_id)

keep <- rowSums(quadrat_species) > 0
quadrat_species  <- quadrat_species[keep, ]
quadrat_metadata <- quadrat_metadata[keep, ]

dim(quadrat_species)  
## [1] 834 173

NMDS Plots

# Create an NMDS plot using metaMDS()
set.seed(123) # For reproducible ordination results
nmds_result <- metaMDS(quadrat_species, distance = "bray", trace = FALSE)
## Warning in decostand(x, "max", 2, na.rm = na.rm): result contains NaN, perhaps due to impossible mathematical
##                  operation
## Warning in decostand(x, "total", 1, na.rm = na.rm): result contains NaN, perhaps due to impossible mathematical
##                  operation
## Warning in metaMDS(quadrat_species, distance = "bray", trace = FALSE): WA
## scores were not calculated due to missing values
# Enforce ecological tide order on the factor levels FIRST
# This is to ensure the legend/color assingment is correct
quadrat_metadata$Region <- factor(
  quadrat_metadata$Region, 
  levels = c("Bennett Slough", "Elkhorn Slough", "Harbor", "Offshore", "Pajaro River", "Old Salinas River Channel")
)

# Plot canvas
plot(nmds_result, type = "n", main = "NMDS Ordination of Region Quadrats")
## species scores not available
# Points
points(nmds_result, display = "sites", col = as.numeric(quadrat_metadata$Region), pch = 16)

# Legend
legend("topright", legend = levels(quadrat_metadata$Region), col = 1:5, pch = 16)

Calculating various alpha diversity metrics

7. Calculate Simpson Index, Simpson’s Evenness, and Shannon Index values for your data. What do you observe?

[Here are the values below:

Shannon Simpson Simpson_Evenness
Bennett Slough 2.132599 0.8171611 0.12430219
Elkhorn Slough 2.149970 0.7887025 0.04879034
Harbor 2.735866 0.8695133 0.07982936
Offshore 2.474560 0.8615128 0.14736503
Old Salinas River Channel 1.429591 0.6985675 0.30159019
Pajaro River 0.000000 1.0000000 Inf

For shannon index, the most diverse Region would be the Harbor. The least diverse region would be the Old Salinas River Channel, with a value of 1.429591. The Simpson index shows us that across the Regions, there is a high abundance of many different species. The Harbor has the highest value at 0.8695133 , with Offshore and Bennett Slough behind. When looking at Simpson evenness, none of the values are close to the value of 1, implying that there are a few species that dominate the Region. Overall, the Harbor was the most diverse overall, with many species spread across the region. Offshore had a high diversity, close behind Harbor. Elkhorn Slough had many species, but there were species that dominated the count. Bennet Slough was moderately diverse, while Old Salinas was spread evenely, athlough they lacked in species diversity. ]

Simpson Index

simpson_index <- diversity(fish_matrix, index = "simpson")

Simpson’s Eveness

richness <- specnumber(fish_matrix)
simpson_evenness <- (1 / (1 - simpson_index)) / richness

Shannon Index

# Shannon Index
shannon_index <- diversity(fish_matrix, index = "shannon")

Diversity Summary

diversity_summary <- data.frame(
  Shannon = shannon_index,
  Simpson = simpson_index,
  Simpson_Evenness = simpson_evenness
)

diversity_summary

Wrapping up

8. What overall conclusions can you draw from your analysis of this dataset? Was your hypothesis correct?

When looking at the data, we can conclude that the alpha diversity is highest within the Elkhorn Slough. However, we cannot assume there were more species spread evenly across the region. The count was high, but we find out that was due to fish species reducing the evenness of the region. The Harbor followed in Alpha Diversity, having 1 less species compared to Elkhorn. However, there was a high diversity and equal evenness amongst this region. So therefore, the Harbor supported a healthier and stronger species diversity. There wasn’t a few fish species dominating the region, allowing a greater spread of fish to be present. Therefore, my hypothesis that the Elkhorn Slough will support greater species diversity due to its high nutrients availability, and combination of two water to form a brakish water, was incorrect. Specialized species need to be present in this water, as salinity levels make it uninhabitable for certain species. Salinity level might change depending on the location, but this special adaptation that species have, allow them too dominate and be more present in the Region. The Simpson and Shannon calculation display this. Another point to consider that may have influence the results is the types of capture methods performed in each area. Some methods may allow for greater catches, or heavilyt argeted. Regardless, my hypothesis was incorrect and I gained some valuable information.