Today you’ll revisit the coral/fish reef question from Day 7, this time refining two things from the first time around: parsing Date as a real date instead of pulling the year out with str_sub(), and taking control of how Site and Habitat are ordered in your plots instead of leaving R to sort them alphabetically.
Read data
Download the data moorea_coral.csv, moorea_fish.csv.
Inside your day 8 folder, create a subfolder called data, and put both CSVs in it.
Create an R script in your day 8 folder called reef-factors-dates.R.
Add a code chunk containing the following code:
library(tidyverse)
library(lubridate)
moorea_coral <- read_csv(
"data/moorea_coral.csv",
na = c("", "NA", "ND") # This vector tells read_csv() which values to interpret as missing data
)
moorea_fish <- read_csv(
"data/moorea_fish.csv",
na = c("", "NA", "ND")
)Use glimpse() on each to see what you’re working with.
Part 1: Dates
Exercise 1: Parse real dates
- Create a vector called
non_coralcontaining the five non-coral category labels:"Sand","CTB","Macroalgae","Non-coralline Crustose Algae", and"Unknown or Other". Filtermoorea_coralto exclude any row whoseTaxonomy_Substrate_or_Functional_Groupis innon_coral, and to keep only rows whereDepthis less than 17. Dateis stored as text formatted"YYYY-MM". Usemutate()to turn it into an actual date column. Then useyear()on that date column to create aYearcolumn.
Compare the two approaches at the console:
str_sub("2017-04", 1, 4) |> as.numeric()
ym("2017-04") |> year()Both get you a year — but only the second one gives you back something that’s still a date, so you can keep asking it date questions.
Exercise 2: Summarize coral cover
Each quadrat (identified by Quad40) can contain several coral genera, so summarizing in one step would average across genera within a quadrat instead of adding them up. Summarize in two steps instead:
- First, sum
Percent_Coverby year, site, habitat, depth, andQuad40to get the total coral cover in each quadrat. Call the new columnquadrat_cover. - Then, summarize the mean of
quadrat_coverby year, site, and habitat. Call the new columnmean_coral_cover. - Arrange the result by year, site, and habitat. Store it as
coral_summary.
Exercise 3: Wrangle and join the fish data
- Filter
moorea_fishto rows whereCoarse_Trophicis"Primary Consumer"— the herbivorous, algae-grazing fish. - Summarize the total biomass (sum of
Biomass) by site, habitat, and year. Call the new columntotal_biomass. Store it asfish_summary. - Use
inner_join()to combinecoral_summaryandfish_summary, matching on site, habitat, and year. Store the result asreef_joined.
Part 2: Factors
Exercise 4: Order habitats ecologically
The coral surveys only cover two habitats, Forereef and Fringing. Backreef shows up in the fish data, but never in moorea_coral, so it always drops out of reef_joined during the inner_join() (the same reason your row counts didn’t match in Exercise 3). By default, those two remaining habitats sort alphabetically: Forereef, Fringing — putting the deeper habitat first. Use fct_relevel() to put them in shallow-to-deep order instead: "Fringing", "Forereef".
mutate()reef_joined$Habitatinto a factor with that level order.- Remake last week’s habitat comparison:
select()Site,Habitat,Year, andmean_coral_cover, thenpivot_wider()to spreadHabitatinto its own columns. - Add a column calculating the difference in coral cover between the two habitats, and plot its distribution as a histogram — same as Day 7, but now the column order in your wide data (and any faceting you do downstream) follows reef structure instead of the alphabet.
Exercise 5: Order sites by coral cover
Right now Site sorts as LTER_1, LTER_2, …, LTER_6 — an arbitrary label order that says nothing about the reef. Use fct_reorder() to reorder Site’s levels by mean_coral_cover instead.
- Using
reef_joined, create a scatterplot with mean coral cover on the x-axis and herbivorous fish biomass on the y-axis,facet_wrap()-ed bySite. - Reorder
Sitewithfct_reorder(Site, mean_coral_cover)before faceting, and compare the two versions. With sites ordered by coral cover, does a pattern with fish biomass become easier to see?
fct_reorder() needs a summary value per level, so it works best right before a plot — reorder inside the mutate() that feeds your ggplot() call, rather than saving the reordered factor back into reef_joined for later use.