Today you’re going to combine everything from today’s sessions (joining and reshaping) with the wrangling and visualization skills from Day 6. You’ll bring together two real datasets from Mo’orea to ask an ecological question: is coral cover correlated with the abundance of herbivorous fish?
Background
Both datasets come from the Moorea Coral Reef Long Term Ecological Research (MCR LTER) site in French Polynesia. Since 2005, researchers have surveyed the same six fixed sites around the island of Moorea every year — the fringing reef (shallow, close to shore), the forereef (deeper, on the outer slope), and, at some sites, the backreef (the shallow, semi-enclosed zone between the fringing reef and the barrier reef).
Coral cover. At each site and habitat, divers photograph a series of quadrats along permanent transects. Every point in each photo is classified into a coral genus, an algae type, sand, or another substrate category, and the percent of the quadrat covered by each category is calculated.
Fish surveys. Along those same transects, divers conduct visual surveys, counting and identifying every fish within a fixed swath, and estimating each fish’s length (used to calculate biomass). Each fish is also assigned a coarse trophic role — for example, herbivorous “primary consumers” that graze on algae, versus “secondary consumers” and “piscivores” that eat other animals.
The coral columns you’ll use most are:
Site: one of six fixed LTER sites around Moorea (LTER_1–LTER_6)Habitat:Fringing,Forereef, orBackreefDepth: survey depth, in meters (some habitats were surveyed at more than one depth)Date: survey year and month, formatted"YYYY-MM"Quad40: an ID for each individual quadrat surveyed along a transect — rows sharing the sameSite,Habitat,Depth,Date, andQuad40describe the same quadratTaxonomy_Substrate_or_Functional_Group: the coral genus, or a non-coral category (Sand,CTB,Macroalgae,Non-coralline Crustose Algae,Unknown or Other) identified in a quadratPercent_Cover: the percent of a quadrat covered by that category
The fish columns you’ll use most are:
Site,Habitat: same meaning as aboveYear: survey year, as a numberTaxonomy: fish speciesBiomass: estimated biomass of that observation, calculated from count and lengthCoarse_Trophic: trophic role —"Primary Consumer"(herbivore),"Secondary Consumer","Piscivore_primarily", or"Planktivore"
Read data
Download the data moorea_coral.csv, moorea_fish.csv.
Inside your day 7 folder, create a subfolder called data, and put both CSVs in it.
Create an R script in your day 7 folder called reef-joins.R.
Add a code chunk containing the following code:
library(tidyverse)
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.
Wrangle, join, and visualize
Exercise 1: Wrangle the coral data
- 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.
When comparing a column to multiple possible values, use the %in% operator instead of a series of | clauses.
Here are two examples of how to use %in%. Run these at the console and compare the outputs.
c("apple", "banana", "creamsicle", "danish", "eclair") %in% c("apple", "creamsicle", "eclair")
!c("apple", "banana", "creamsicle", "danish", "eclair") %in% c("apple", "creamsicle", "eclair")- Use
mutate(),str_sub(), andas.numeric()to pull the four-digit year out ofDate(which is formatted"YYYY-MM") into a new column calledYear. - 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, habitat, and depth. Call the new columnmean_coral_cover.
- First, sum
- Arrange the result by year, site, and depth. Store it as
coral_summary.
Exercise 2: Wrangle 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. - Arrange the result by year, site, and habitat. Store it as
fish_summary.
Exercise 3: Join the summaries
- Use
inner_join()to combinecoral_summaryandfish_summary, matching on site, habitat, and year. Store the result asreef_joined. - Compare the number of rows in
reef_joinedto the number of rows incoral_summaryandfish_summary. Are they the same? Why or why not? (Hint: think about which habitats show up in each dataset.)
Exercise 4: Reshape
- From
reef_joined,select()justSite,Habitat,Year, andmean_coral_cover. - Use
pivot_wider()to spreadHabitatinto its own columns, so each row is one site/year combination with a column for every habitat’s mean coral cover. - Add a column that calculates the difference in coral cover between two habitats of your choice.
- Create a histogram to visualize the distribution of the difference in coral cover between the habitats.
Exercise 5: Visualize
Using reef_joined, create a scatterplot with mean coral cover on the x-axis and herbivorous fish biomass on the y-axis. Choose descriptive axis titles.
That’s a lot of points! One way to clean up your figure is to break it out into small multiples. Try adding a facet_wrap() to your figure.