Complete this markdown for today’s lab. You may work with your classmates, but each person needs to fill out their own markdown file. At the end of class, you can submit either this compiled document as a word or pdf file, or a separate document with your figures and answers.
Recall, we tested for the effects of location and seine size on diversity. Before we start, write out some hypotheses for your data.
Hypothesis: Species diversity will differ between St. John’s and Waterfront because the two locations have different environmental conditions.
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
library(tidyr)
library(emmeans)
## Welcome to emmeans.
## Caution: You lose important information if you filter this package's results.
## See '? untidy'
In this section, you will need to read in and summarize your data.
Data <- read.csv("SeiningData (1).csv") # Loads the seining dataset into R
Data$Location <- trimws(Data$Location) # Removes extra spaces from location names
head(Data) # Displays the first six rows to check that the data loaded correctly
## Location Seine_Num Organism Number
## 1 St. John's 1 Silverside 4
## 2 St. John's 1 Silverside 1
## 3 St. John's 1 Silverside 5
## 4 St. John's 1 Killifish 1
## 5 St. John's 2 Mummichog 1
## 6 St. John's 2 Blue Crab 1
Job 1: Calculate the parts needed for the Shannon index. Name your new dataframe Diversity_data.
Hint: 1) Remember that you can use group_by() to group your variables
before a calculation, 2) Last lab you used summarize. This time (and
this time only) you’ll use reframe() because you want multiple rows to
reference the same Total_Ind value. You can write the code with
summarize, and just replace summarize with reframe to make the
deprecation warning go away. When you’re done, you should have a
dataframe with a column for Location, Seine Size, Total Individuals,
Proportion of Individuals (pi), and the pi*logpi (pilogpi).
\[
H = -\Sigma p_i ln p_i
\]
All_seines <- Data %>% distinct(Location, Seine_Num) # Saves all seine samples, including ones with zero organisms
Species_counts <- Data %>% # Starts organizing the seining data
filter(!is.na(Organism), !is.na(Number), Number > 0) %>% # Removes missing organisms and zero catches
group_by(Location, Seine_Num, Organism) %>% # Groups the data by location, seine number, and species
summarise(Number = sum(Number), .groups = "drop") # Adds together the number of individuals for each species
Diversity_data <- Species_counts %>% # Starts calculating the Shannon diversity components
group_by(Location, Seine_Num) %>% # Groups organisms within each seine sample
mutate(Total_ind = sum(Number)) %>% # Calculates the total number of individuals in each seine
mutate(Prop_ind = Number / Total_ind) %>% # Calculates the proportion of individuals belonging to each species
mutate(p_logp = Prop_ind * log(Prop_ind)) %>% # Calculates the proportion multiplied by its natural logarithm
ungroup() # Removes grouping from the completed dataframe
print(Diversity_data) # Displays the diversity calculations
## # A tibble: 19 × 7
## Location Seine_Num Organism Number Total_ind Prop_ind p_logp
## <chr> <int> <chr> <int> <int> <dbl> <dbl>
## 1 St. John's 1 Killifish 1 11 0.0909 -0.218
## 2 St. John's 1 Silverside 10 11 0.909 -0.0866
## 3 St. John's 2 Blue Crab 1 3 0.333 -0.366
## 4 St. John's 2 Mummichog 1 3 0.333 -0.366
## 5 St. John's 2 Silverside 1 3 0.333 -0.366
## 6 St. John's 3 Silverside 20 20 1 0
## 7 St. John's 4 Silverside 7 7 1 0
## 8 St. John's 5 Mummichog 1 4 0.25 -0.347
## 9 St. John's 5 Silverside 3 4 0.75 -0.216
## 10 Waterfront 3 Blue Crab 1 12 0.0833 -0.207
## 11 Waterfront 3 Mummichog 3 12 0.25 -0.347
## 12 Waterfront 3 Shrimp 1 12 0.0833 -0.207
## 13 Waterfront 3 Silverside 7 12 0.583 -0.314
## 14 Waterfront 4 Shrimp 17 27 0.630 -0.291
## 15 Waterfront 4 Shripped Killfish 1 27 0.0370 -0.122
## 16 Waterfront 4 Silverside 9 27 0.333 -0.366
## 17 Waterfront 5 Killifish 2 8 0.25 -0.347
## 18 Waterfront 5 Shrimp 1 8 0.125 -0.260
## 19 Waterfront 5 Silverside 5 8 0.625 -0.294
Job 2: Summarize the following for each location so that you can make a figure
Diversity_data_Summary <- Diversity_data %>% # Starts summarizing diversity for each seine
group_by(Location, Seine_Num) %>% # Groups the data by location and seine number
summarise(Richness = n_distinct(Organism), # Counts the different species collected
ShannonH = -sum(p_logp), # Calculates Shannon diversity
Evenness = ShannonH / Richness, # Calculates evenness using the assignment's formula
.groups = "drop") # Removes grouping from the summary
Diversity_data_Summary <- All_seines %>% # Starts with all seine samples, including empty ones
left_join(Diversity_data_Summary, by = c("Location", "Seine_Num")) %>% # Combines all seines with their diversity values
mutate(Richness = coalesce(Richness, 0L)) # Gives empty seines a richness of zero
print(Diversity_data_Summary) # Displays the completed diversity summary table
## Location Seine_Num Richness ShannonH Evenness
## 1 St. John's 1 2 0.3046361 0.1523180
## 2 St. John's 2 3 1.0986123 0.3662041
## 3 St. John's 5 2 0.5623351 0.2811676
## 4 St. John's 3 1 0.0000000 0.0000000
## 5 St. John's 4 1 0.0000000 0.0000000
## 6 Waterfront 1 0 NA NA
## 7 Waterfront 2 0 NA NA
## 8 Waterfront 3 4 1.0751393 0.2687848
## 9 Waterfront 4 3 0.7795536 0.2598512
## 10 Waterfront 5 3 0.9002561 0.3000854
Note that we cannot divide by 0, so seines with 0 species cannot be counted (this occured twice at the waterfront). You should report this.
Two seines at Waterfront had no species collected. These samples were counted as zero for species richness but were left out of the Shannon diversity and evenness calculations because we cannot divide by zero. So we just ignored the values.(NA)
Summarize both richness and the Shannon index. Name your dataframe Diversity_plotdata
You can ignore NAs using na.rm=TRUE (e.g.Mean_ShannonH = mean(ShannonH,na.rm=TRUE) ), but you must report and discuss this.
Diversity_plotdata <- Diversity_data_Summary %>% # Starts summarizing the diversity data for each location
group_by(Location) %>% # Groups the seine samples by location
summarise(Mean_Richness = mean(Richness, na.rm = TRUE), # Calculates average species richness
SE_Richness = sd(Richness, na.rm = TRUE) / sqrt(sum(!is.na(Richness))), # Calculates standard error for richness
Mean_ShannonH = mean(ShannonH, na.rm = TRUE), # Calculates average Shannon diversity
SE_ShannonH = sd(ShannonH, na.rm = TRUE) / sqrt(sum(!is.na(ShannonH))), # Calculates standard error for Shannon diversity
Mean_Evenness = mean(Evenness, na.rm = TRUE), # Calculates average evenness
SE_Evenness = sd(Evenness, na.rm = TRUE) / sqrt(sum(!is.na(Evenness))), # Calculates standard error for evenness
n_Richness = sum(!is.na(Richness)), # Counts the number of valid richness samples
n_ShannonH = sum(!is.na(ShannonH)), # Counts the number of valid Shannon diversity samples
n_Evenness = sum(!is.na(Evenness)), # Counts the number of valid evenness samples
.groups = "drop") # Removes grouping from the completed summary
print(Diversity_plotdata) # Display as a table
## # A tibble: 2 × 10
## Location Mean_Richness SE_Richness Mean_ShannonH SE_ShannonH Mean_Evenness
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 St. John's 1.8 0.374 0.393 0.205 0.160
## 2 Waterfront 2 0.837 0.918 0.0858 0.276
## # ℹ 4 more variables: SE_Evenness <dbl>, n_Richness <int>, n_ShannonH <int>,
## # n_Evenness <int>
I have given you the code for an anova with species richness, you will need to do the same for Shannon’s H and Evenness.
mod1 <- lm(Richness ~ Location, data = Diversity_data_Summary) # Tests whether species richness differs between locations
anova(mod1) # Displays the ANOVA results for species richness
## Analysis of Variance Table
##
## Response: Richness
## Df Sum Sq Mean Sq F value Pr(>F)
## Location 1 0.1 0.1 0.0476 0.8327
## Residuals 8 16.8 2.1
mod2 <- lm(ShannonH ~ Location, data = Diversity_data_Summary) # Tests whether Shannon diversity differs between locations
anova(mod2) # Displays the ANOVA results for Shannon diversity
## Analysis of Variance Table
##
## Response: ShannonH
## Df Sum Sq Mean Sq F value Pr(>F)
## Location 1 0.51719 0.51719 3.4967 0.1107
## Residuals 6 0.88744 0.14791
mod3 <- lm(Evenness ~ Location, data = Diversity_data_Summary) # Tests whether evenness differs between locations
anova(mod3) # Displays the ANOVA results for evenness
## Analysis of Variance Table
##
## Response: Evenness
## Df Sum Sq Mean Sq F value Pr(>F)
## Location 1 0.025362 0.025362 1.3915 0.2828
## Residuals 6 0.109353 0.018226
Generate a figure for each metric (you may include them in one multi-panel figure) and write a caption that includes the following:
Labels for both axes, including units if applicable
Number of replicates in each group (n) if applicable
Any abbreviations used
What error bars represent (e.g. ± SE) and what any colors/shapes etc. mean
A brief takehome point
Then write a short results section that references yoru figures, is supported by statistical analysis, (e.g. P < 0.001). As always, lead with the biology, and use stats to support. E.g. Species richness was 10 times higher in the upland marsh compared to the lowland marsh (P=0.025).
Diversity_long <- Diversity_plotdata %>% # Starts preparing the diversity averages for graphing
pivot_longer(cols = starts_with("Mean_"), names_to = "Metric", values_to = "Mean") %>% # Combines the three diversity metrics into one column
mutate(Metric = sub("Mean_", "", Metric)) %>% # Removes Mean_ from the metric names
mutate(SE = case_when(Metric == "Richness" ~ SE_Richness, Metric == "ShannonH" ~ SE_ShannonH, TRUE ~ SE_Evenness)) %>% # Matches each diversity metric with its standard error
mutate(n = case_when(Metric == "Richness" ~ n_Richness, Metric == "ShannonH" ~ n_ShannonH, TRUE ~ n_Evenness)) # Matches each metric with its sample size
ggplot(Diversity_long, aes(x = Location, y = Mean, fill = Location)) + # Sets up the graph using location and mean diversity
geom_col() + # Creates bars showing average diversity at each location
geom_errorbar(aes(ymin = pmax(0, Mean - SE), ymax = Mean + SE), width = 0.2) + # Adds standard error bars
geom_text(aes(label = paste0("n = ", n)), vjust = -0.7) + # Shows the number of samples above each bar
facet_wrap(~Metric, scales = "free_y") + # Creates separate panels for richness, Shannon diversity, and evenness
labs(x = "Sampling Location", y = "Mean Diversity Value", title = "Species Diversity by Location") + # Adds the graph title and axis labels
theme_minimal() + # Gives the graph a clean appearance
theme(legend.position = "none") + # Removes the extra legend
scale_y_continuous(expand = expansion(mult = c(0, 0.25))) # Adds room above the bars for sample sizes
Figure 1: This graph compares species richness, Shannon
diversity, and evenness at St. John’s and Waterfront. Pink bars
represent St. John’s, and blue bars represent Waterfront. Error bars
show standard error (SE). Each location had five samples for richness,
but only three Waterfront samples were used for Shannon diversity and
evenness. Overall, Waterfront had higher average diversity.
Results: Species diversity was higher at Waterfront compared to St. John’s (Figure 1). Waterfront had slightly more species on average (2.0 vs. 1.8), but the difference was not significant (p = 0.833). Shannon diversity (p = 0.111) and evenness (p = 0.283) were also higher at Waterfront, but neither difference was statistically significant. Overall, the two locations had similar diversity based on our statistical tests.
Questions
Statistics:
2)What insights (if any) did using multiple metrics for species diversity provide? Using multiple metrics helped us understand species diversity better. Richness showed how many species were collected, Shannon diversity and evenness showed how the organisms were distributed. Waterfront had higher averages, but the differences were not statistically significant.
We took a lot of measurements of water quality. In this section of the lab, I want you to do something interesting. You can use either a regression or anova design, but be sure to choose the appropriate one. Here, provide
A figure with appropriate caption
An appropriate statistical analysis
A paragraph written in the style of a results section properly referencing your figure and reporting your results.
#Water Quality
Water <- read.csv("WaterQualityData (1).csv") # Loads the water quality dataset
Water$Location <- trimws(Water$Location) # Removes extra spaces from location names
Water_groupmeans <- Water %>% # Starts organizing the water quality data
group_by(Group, Location) %>% # Groups measurements by student group and location
summarise(Salinity = mean(Salinity, na.rm = TRUE), .groups = "drop") # Calculates average salinity for each group
Water_summary <- Water_groupmeans %>% # Starts summarizing salinity by location
group_by(Location) %>% # Groups the data by sampling location
summarise(Mean_Salinity = mean(Salinity), # Calculates the average salinity
SE_Salinity = sd(Salinity) / sqrt(n()), # Calculates the standard error
.groups = "drop") # Removes grouping from the summary
water_mod <- lm(Salinity ~ Location + Group, data = Water_groupmeans) # Tests location differences while accounting for student group
anova(water_mod) # Displays the ANOVA results
## Analysis of Variance Table
##
## Response: Salinity
## Df Sum Sq Mean Sq F value Pr(>F)
## Location 2 425.64 212.819 482.0956 1.707e-05 ***
## Group 2 2.27 1.136 2.5723 0.1913
## Residuals 4 1.77 0.441
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
ggplot(Water_summary, aes(x = Location, y = Mean_Salinity, fill = Location)) + # Sets up the salinity graph
geom_col() + # Creates bars showing average salinity
geom_errorbar(aes(ymin = pmax(0, Mean_Salinity - SE_Salinity), ymax = Mean_Salinity + SE_Salinity), width = 0.2) + # Adds standard error bars
labs(x = "Sampling Location", y = "Mean Salinity (dataset units)", title = "Salinity by Location") + # Adds axis labels and a graph title
theme_minimal() + # Makes the graph easier to read
theme(legend.position = "none") # Removes the unnecessary legend
Figure 2:This graph shows the average salinity at Schafer, St. John’s, and Waterfront. The different colors represent each location, and the error bars show standard error (SE). Each location had three samples (n = 3). Schafer had the lowest salinity, while Waterfront had the highest.