Enter your group’s measurements in the shared class Excel
spreadsheet. The Chla (ug/L) column calculates
chlorophyll a–like pigments automatically from the absorbance
measurements, filtered volume, and ethanol extraction volume (normally 5
mL). After all groups finish, export the Class Data
sheet as ENV160_HABs_data_2026.csv and put it in the same
folder as this R Markdown file.
Change only my_site below to the exact name of your
sampling site: Cedar Creek,
GV Stormwater Pond, Grand River, or
Grand River Lake. Run the chunks in order!!!
library(tidyverse)
my_site <- "Grand River Lake" # CHANGE to your site's exact name
file_name <- "ENV160_HABs_data_2026.csv"
site_options <- c("Cedar Creek", "GV Stormwater Pond", "Grand River", "Grand River Lake")
stopifnot(my_site %in% site_options)
class_data <-read.csv("~/Desktop/ENV 160/Week 12/ENV 160 Fall 2026 HABs_final_data.csv")
str(class_data)
## 'data.frame': 48 obs. of 10 variables:
## $ Group : int 1 1 1 1 1 1 1 1 1 1 ...
## $ Site : chr "Cedar Creek" "Cedar Creek" "Cedar Creek" "Cedar Creek" ...
## $ Treatment : chr "Control" "Control" "Control" "Nitrogen" ...
## $ Replicate : int 1 2 3 1 2 3 1 2 3 1 ...
## $ Volume.Filtered.mL : int 70 70 70 70 70 70 70 70 70 70 ...
## $ Volume.ethanol.added.mL: int 5 5 5 5 5 5 5 5 5 5 ...
## $ X665.ABS : num 0.62 0.68 0.27 0.54 0 0.24 1.64 2.12 0 0.18 ...
## $ X750.ABS : num 0.58 0.6 0.23 0.52 0 0.22 1.63 2.06 0 0.18 ...
## $ Notes : chr "" "these values were ran wihtout the ethanol being zerod" "these values were ran wihtout the ethanol being zerod" "these values were ran wihtout the ethanol being zerod" ...
## $ Chla..ug.L. : num 79.6 159.1 79.6 39.8 0 ...
# Use the calculated Chla column exported from Excel.
site_data <- class_data %>%
filter(Site == my_site) %>%
mutate(
Treatment = factor(Treatment,
levels = c("Control", "Nitrogen", "Phosphorus", "Nitrogen + Phosphorus")),
Chla = as.numeric(`Chla..ug.L.`)
)
if (nrow(site_data) == 0) stop("No rows found for this site. Check my_site spelling.")
if (all(is.na(site_data$Chla))) stop("No chlorophyll values found. Export the completed Excel sheet as CSV after formulas calculate.")
print(site_data %>% select(Site, Treatment, Replicate, Chla))
## Site Treatment Replicate Chla
## 1 Grand River Lake Control 1 19.89
## 2 Grand River Lake Control 2 159.14
## 3 Grand River Lake Control 3 19.89
## 4 Grand River Lake Nitrogen 1 0.00
## 5 Grand River Lake Nitrogen 2 0.00
## 6 Grand River Lake Nitrogen 3 59.68
## 7 Grand River Lake Phosphorus 1 19.89
## 8 Grand River Lake Phosphorus 2 0.00
## 9 Grand River Lake Phosphorus 3 19.89
## 10 Grand River Lake Nitrogen + Phosphorus 1 39.79
## 11 Grand River Lake Nitrogen + Phosphorus 2 39.79
## 12 Grand River Lake Nitrogen + Phosphorus 3 0.00
site_summary <- site_data %>%
group_by(Treatment, .drop = FALSE) %>%
summarise(
n = sum(!is.na(Chla)),
mean_Chla = if (all(is.na(Chla))) NA_real_ else mean(Chla, na.rm = TRUE),
sd_Chla = if (sum(!is.na(Chla)) >= 2) sd(Chla, na.rm = TRUE) else NA_real_,
.groups = "drop"
)
print(site_summary)
## # A tibble: 4 × 4
## Treatment n mean_Chla sd_Chla
## <fct> <int> <dbl> <dbl>
## 1 Control 3 66.3 80.4
## 2 Nitrogen 3 19.9 34.5
## 3 Phosphorus 3 13.3 11.5
## 4 Nitrogen + Phosphorus 3 26.5 23.0
This graph uses the same style as the original class code: pastel Set2 treatment colors, black bar outlines, mean ± 1 SD with black caps, and white jittered circles showing all three replicates.
# Use the spreadsheet's calculated Chla values (do not recalculate them).
plot_data <- site_data %>% filter(!is.na(Chla), !is.na(Treatment))
if (nrow(plot_data) == 0) stop("Enter chlorophyll values before plotting.")
# Highest value, including the tops of SD error bars, for a readable axis.
plot_max <- max(c(plot_data$Chla,
site_summary$mean_Chla + site_summary$sd_Chla), na.rm = TRUE)
ggplot() +
# Main bars: mean chlorophyll a
geom_col(
data = site_summary,
aes(x = Treatment, y = mean_Chla, fill = Treatment),
width = 0.7, color = "black", alpha = 0.9, na.rm = TRUE
) +
# Standard deviation vertical lines
geom_linerange(
data = site_summary,
aes(x = Treatment,
ymin = mean_Chla - sd_Chla,
ymax = mean_Chla + sd_Chla),
linewidth = 1.3, color = "black", na.rm = TRUE
) +
# SD caps
geom_errorbar(
data = site_summary,
aes(x = Treatment,
ymin = mean_Chla - sd_Chla,
ymax = mean_Chla + sd_Chla),
width = 0.25, linewidth = 1.2, color = "black", na.rm = TRUE
) +
# Individual replicate measurements (white circles)
geom_jitter(
data = plot_data,
aes(x = Treatment, y = Chla),
width = 0.12, height = 0, size = 2.5,
shape = 21, fill = "white", color = "black"
) +
scale_fill_brewer(palette = "Set2") +
scale_y_continuous(
limits = c(0, NA),
expand = expansion(mult = c(0, 0.05))
) +
expand_limits(y = plot_max * 1.05) +
labs(
x = "Treatment",
y = "Chlorophyll a (µg/L)",
title = paste("Chlorophyll a by Treatment –", my_site,
"(Mean ± 1 SD with Individual Values)"),
fill = "Treatment"
) +
theme_minimal(base_size = 13) +
theme(
plot.title = element_text(hjust = 0.5, face = "bold"),
axis.title = element_text(face = "bold"),
legend.position = "none"
)
The four treatments form a 2 × 2 experiment: nitrogen absent/present and phosphorus absent/present. The interaction tests whether the effect of one nutrient depends on the other. Each treatment should have three replicates. Report the p-values, but interpret them cautiously because replication is limited.
anova_data <- site_data %>%
filter(!is.na(Chla), !is.na(Treatment)) %>%
mutate(
N_added = factor(if_else(Treatment %in% c("Nitrogen", "Nitrogen + Phosphorus"), "Yes", "No")),
P_added = factor(if_else(Treatment %in% c("Phosphorus", "Nitrogen + Phosphorus"), "Yes", "No"))
)
if (nrow(anova_data) >= 8 && n_distinct(anova_data$Treatment) == 4 &&
all(table(anova_data$Treatment) >= 2)) {
model <- aov(Chla ~ N_added * P_added, data = anova_data)
summary(model)
} else {
cat("Not enough completed replicates for a two-way ANOVA. Check data entry.")
}
## Df Sum Sq Mean Sq F value Pr(>F)
## N_added 1 824 824 0.397 0.546
## P_added 1 1616 1616 0.778 0.404
## N_added:P_added 1 2671 2671 1.286 0.290
## Residuals 8 16621 2078
2025 Data Analysis for Comparison
#install necessary packages
library(tidyverse)
#import the data set as a csv file by clicking 'Environment', 'Import Dataset', 'From Text base' then finding the file on your computer. Once you select the file, leave all settings the same except for the headers option, swelect 'yes' for headers. Once you click import, the data will open in a new tab.
#select 'History' , select the line of of the reads ENV160_HABs_data <_ read.csv and your file path, then click 'to source'. This will move the line of code to your script where your cursor is at.
ENV160_HABs_data <- read.csv("~/Desktop/ENV 160/Week 12/ENV160_HABs_data_F25.csv")
#run the code to create a matrix with the name "ENV160_HABs_data", on a mac do this by pressing command + enter with your cursor on the line of code, on a PC press
#summarize the data, notice the data classification as 'character' for treatment.
summary(ENV160_HABs_data)
## Name Treatment Volume.Filtered.mL
## Length:34 Length:34 Min. :63.00
## Class :character Class :character 1st Qu.:66.25
## Mode :character Mode :character Median :68.00
## Mean :67.44
## 3rd Qu.:69.00
## Max. :69.00
## Volume.ethanol.added.mL X665.ABS X750.ABS
## Min. :5 Min. :0.00100 Min. :0.00200
## 1st Qu.:5 1st Qu.:0.01975 1st Qu.:0.01975
## Median :5 Median :0.06000 Median :0.06450
## Mean :5 Mean :0.09918 Mean :0.11812
## 3rd Qu.:5 3rd Qu.:0.16175 3rd Qu.:0.07075
## Max. :5 Max. :0.47400 Max. :0.74000
str(ENV160_HABs_data)
## 'data.frame': 34 obs. of 6 variables:
## $ Name : chr "Ben" "Ben" "Ben" "Coy" ...
## $ Treatment : chr "Control" "Phosphorus" "Nitrogen + Phosphorus" "Nitrogen" ...
## $ Volume.Filtered.mL : int 67 67 66 66 63 66 65 68 69 69 ...
## $ Volume.ethanol.added.mL: int 5 5 5 5 5 5 5 5 5 5 ...
## $ X665.ABS : num 0.014 0.015 0.173 0.022 0.066 0.189 0.057 0.063 0.064 0.366 ...
## $ X750.ABS : num 0.069 0.071 0.027 0.067 0.019 0.065 0.017 0.008 0.022 0.296 ...
# Convert Treatment from a character/string to a factor (categorical variable)
ENV160_HABs_data <- ENV160_HABs_data %>%
mutate(Treatment = factor(Treatment))
#check to see if treatment changed to a factor
str(ENV160_HABs_data)
## 'data.frame': 34 obs. of 6 variables:
## $ Name : chr "Ben" "Ben" "Ben" "Coy" ...
## $ Treatment : Factor w/ 4 levels "Control","Nitrogen",..: 1 4 3 2 4 3 1 2 4 3 ...
## $ Volume.Filtered.mL : int 67 67 66 66 63 66 65 68 69 69 ...
## $ Volume.ethanol.added.mL: int 5 5 5 5 5 5 5 5 5 5 ...
## $ X665.ABS : num 0.014 0.015 0.173 0.022 0.066 0.189 0.057 0.063 0.064 0.366 ...
## $ X750.ABS : num 0.069 0.071 0.027 0.067 0.019 0.065 0.017 0.008 0.022 0.296 ...
Calculate chlorophyll a
ENV160_HABs_data <- ENV160_HABs_data %>%
mutate(
# Convert filtered volume from mL to Liters
vol_filtered_L = Volume.Filtered.mL / 1000,
# Extract volume in mL
vol_extract_mL = Volume.ethanol.added.mL,
# Corrected absorbance (turbidity-corrected)
abs_corr = X665.ABS - X750.ABS,
# Calculate raw chlorophyll a (µg/L) using new correction factor
chl_a_raw = (abs_corr * 27.85 * vol_extract_mL) / (1 * vol_filtered_L),
# Turn any negative chlorophyll a values into zeros
chl_a_ugL = ifelse(chl_a_raw < 0, 0, chl_a_raw)
)
# Confirm calculation worked
ENV160_HABs_data %>%
select(Name, Treatment, X665.ABS, X750.ABS, abs_corr, chl_a_raw, chl_a_ugL) %>%
head()
## Name Treatment X665.ABS X750.ABS abs_corr chl_a_raw chl_a_ugL
## 1 Ben Control 0.014 0.069 -0.055 -114.30970 0.0000
## 2 Ben Phosphorus 0.015 0.071 -0.056 -116.38806 0.0000
## 3 Ben Nitrogen + Phosphorus 0.173 0.027 0.146 308.03788 308.0379
## 4 Coy Nitrogen 0.022 0.067 -0.045 -94.94318 0.0000
## 5 Coy Phosphorus 0.066 0.019 0.047 103.88492 103.8849
## 6 Coy Nitrogen + Phosphorus 0.189 0.065 0.124 261.62121 261.6212
Summarize chlorophyll a data and build a bar plot
#look at the average and standard deviation (variability) around our chlorophyll a data
chl_summary <- ENV160_HABs_data %>%
group_by(Treatment) %>%
summarise(
mean_chl = mean(chl_a_ugL, na.rm = TRUE),
sd_chl = sd(chl_a_ugL, na.rm = TRUE),
n = n(),
se_chl = sd_chl / sqrt(n)
)
chl_summary
## # A tibble: 4 × 5
## Treatment mean_chl sd_chl n se_chl
## <fct> <dbl> <dbl> <int> <dbl>
## 1 Control 20.1 35.6 8 12.6
## 2 Nitrogen 25.3 41.5 8 14.7
## 3 Nitrogen + Phosphorus 257. 145. 9 48.4
## 4 Phosphorus 73.6 93.8 9 31.3
ggplot() +
# Main bars (mean chlorophyll a)
geom_col(
data = chl_summary,
aes(x = Treatment, y = mean_chl, fill = Treatment),
width = 0.7,
color = "black",
alpha = 0.9
) +
# Standard deviation vertical lines
geom_linerange(
data = chl_summary,
aes(
x = Treatment,
ymin = mean_chl - sd_chl,
ymax = mean_chl + sd_chl
),
linewidth = 1.3,
color = "black"
) +
# SD caps
geom_errorbar(
data = chl_summary,
aes(
x = Treatment,
ymin = mean_chl - sd_chl,
ymax = mean_chl + sd_chl
),
width = 0.25,
linewidth = 1.2,
color = "black"
) +
# Individual data points (jittered circles)
geom_jitter(
data = ENV160_HABs_data,
aes(x = Treatment, y = chl_a_ugL),
width = 0.12, # horizontal jitter
height = 0, # no vertical jitter
size = 2.5,
shape = 21,
fill = "white",
color = "black"
) +
# Make sure y-axis includes ALL data values
expand_limits(y = max(ENV160_HABs_data$chl_a_ugL, na.rm = TRUE) * 1.05) +
scale_y_continuous(limits = c(0, NA), expand = c(0, 0)) +
# Colors
scale_fill_brewer(palette = "Set2") +
labs(
x = "Treatment",
y = "Chlorophyll a (µg/L)",
title = "Chlorophyll a by Treatment (Mean ± 1 SD with Individual Values)",
fill = "Treatment"
) +
theme_minimal(base_size = 13) +
theme(
plot.title = element_text(hjust = 0.5, face = "bold"),
axis.title = element_text(face = "bold"),
legend.position = "none"
)
## Warning: Removed 3 rows containing missing values or values outside the scale range
## (`geom_segment()`).
Statistical analysis (ANOVA) to determine treatment effect on
chlorophyll a
ENV160_HABs_data <- ENV160_HABs_data %>%
mutate(
# Nitrogen added if the word "Nitrogen" appears in the Treatment label
N_added = if_else(str_detect(Treatment, "Nitrogen"), "Yes", "No"),
# Phosphorus added if the word "Phosphorus" appears in the Treatment label
P_added = if_else(str_detect(Treatment, "Phosphorus"), "Yes", "No"),
# Make them factors with a clear baseline
N_added = factor(N_added, levels = c("No", "Yes")),
P_added = factor(P_added, levels = c("No", "Yes"))
)
# Check the 2x2 design – now should include all 4 cells
table(ENV160_HABs_data$N_added, ENV160_HABs_data$P_added)
##
## No Yes
## No 8 9
## Yes 8 9
# Keep only the factorial design treatments
anova_data <- ENV160_HABs_data %>%
filter(Treatment %in% c("Control",
"Nitrogen",
"Phosphorus",
"Nitrogen + Phosphorus"))
#Two-way ANOVa model
anova_model <- aov(chl_a_ugL ~ N_added * P_added, data = anova_data)
# View ANOVA table
summary(anova_model)
## Df Sum Sq Mean Sq F value Pr(>F)
## N_added 1 83987 83987 9.699 0.004034 **
## P_added 1 172067 172067 19.871 0.000107 ***
## N_added:P_added 1 67021 67021 7.740 0.009250 **
## Residuals 30 259779 8659
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1