# README: Analysis code for
# "Asynchronous population trends stabilize mesopredatory coral reef fish communities in the face of global change — Oikos (2026)
# Contact: ruchakarkarey@gmail.com
This folder contains the full R analysis pipeline behind the manuscript,
from raw survey data through to the published figures. Scripts are numbered
in the order they should be run.
## Folder structure expected by the scripts
Set your working directory to this folder, then create/populate:
```
./
├── 01_community_matrices.R
├── 02_biodiversity_indices.R
├── 03_stability_indices.R
├── 04_combine_abiotic_biotic.R
├── 05_piecewise_sem.R
├── 06_manuscript_figures.R
├── data/
│ ├── raw/ <- place raw input files here (see below)
│ │ ├── raw_data.csv # raw fish community data
│ │ ├── traits.RDS # functional traits data
│ │ ├── Red_DHW.RData # raw Degree Heating Weeks data
│ │ ├── Total_Gravity_of_Coral_Reefs_1.0.shp: It is a global polygon layer of tropical and sub-tropical coral reefs at 10 km resolution.
│ │ ├── Total_Gravity_of_Coral_Reefs_1.0.dbf
│ │ ├── Total_Gravity_of_Coral_Reefs_1.0.shx
│ │ ├── Total_Gravity_of_Coral_Reefs_1.0.xml (metadata file for a shapefile)
│ │ ├── Total_Gravity_of_Coral_Reefs_1.0.prj
│ └── derived/ <- created automatically; intermediate outputs
└── Results/ <- created automatically; figures and tables
```
`data/derived/` and `Results/` are created automatically by the scripts
### Data dictionaries
# raw_data.csv
Site x transect level fish abundance data
| Variable |
Description |
DatasetID |
ID of the dataset in the SCOREREEFS database |
bio_reg |
Biogeographic region assigned to the dataset |
newloc |
Location (country / province within country) |
newSite |
Name of the reef site |
Lat |
Latitude (decimal degrees) |
Long |
Longitude (decimal degrees) |
clust_1km |
1-km spatial cluster assigned to the site, based on latitude/longitude |
Sampling_area |
Area (m²) of the original replicate: transect or point count. Density is standardised to 100.m² across replicates |
Replicate |
Replicate number |
Zone |
Reef zone: crest, slope, flat, lagoon, or NA |
Depth |
Reef depth (m); NA where not recorded |
Year |
Sampling year |
Date |
Sampling date, where available |
Observer |
Observer ID |
Family |
Fish family |
Genus |
Fish genus |
Species |
Fish species |
Genus_Species |
Species name (Genus_Species) |
Density |
Abundance per 100.m² |
traits.RDS
Functional traits for each species, keyed to the Genus_Species column in raw_data.csv.
| Variable |
Type |
Description / codes |
species |
— |
Species name, matches Genus_Species in raw_data.csv |
size_list |
Nominal |
Body size class (cm): 1 = 10–20, 2 = 21–30, 3 = 31–40, 4 = 41–50, 5 = 51–60, 6 = 61–80. Species assigned as per species level mean size. |
Mobility |
Nominal |
Mobility on a reef coded as 1 = Mobile (limited to a spot on reef), 2 = Sedentary (mobile within reef), 3 = Very mobile (mobile across reefs) |
Activity |
Nominal |
Active time of day coded as 1 = Diurnal, 2 = Crepuscular, 3 = Nocturnal |
Schooling |
Nominal |
Level of gregariousness coded as: 1 = Solitary individuals, 2 = Paired individuals, 3 = Groups <10 individuals, 4 = Groups of 10–50 individuals, 5 = Groups of 50+ individuals |
Position |
Nominal |
Benthic habitat/ coral reef affinity: 1 = Benthic, 2 = Benthopelagic, 3 = Pelagic |
newdiet |
Ordinal |
Dietary habit: Macroinvertivore, Microinvertivore, Crustacivore, Sessile invertivore, Piscivore |
Res_DHW.RData
Maximum and mean Degree Heating Weeks (DHW) for each spatial cluster, keyed to the clust_1km column in raw_data.csv. For each surveyed year, DHW values were extracted using a 100-km buffer around each cluster and averaged.
| Variable |
Description |
clust_1km |
Spatial cluster assigned to the site |
Lat |
Latitude (decimal degrees) |
Long |
Longitude (decimal degrees) |
Year/years |
Year of sampling |
maxdhw |
Maximum DHW for the cluster in that year |
meandhw |
Mean DHW for the cluster in that year |
## Pipeline overview
| Script | Purpose | Key inputs | Key outputs |
|---|---|---|---|
| `01_community_matrices.R` | Clean raw survey data, assign reef zones, build a mean-density site x species community matrix | `raw_data.csv` | `comm.matrix.zones.csv` |
| `02_biodiversity_indices.R` | Compute Hill-number taxonomic & functional diversity (TD, FD) per site/cluster/year | `traits.RDS`, output of 01 | `list.community.zone.rds`, `hill.biodivindices.RDS` |
| `03_stability_indices.R` | Compute community synchrony, community stability, species population stability; join to biodiversity indices | `raw_data.csv`, outputs of 02 | `biotic_zone.RDS` |
| `04_combine_abiotic_biotic.R` | Summarise biotic data and join abiotic covariates (max DHW, human gravity) | output of 03, `Res_DHW.RData`, gravity shapefile | `biotic_abiotic.csv` |
| `05_piecewise_sem.R` | Standardised predictors; fit the piecewise SEM | output of 04 | `psemdata_standardised.csv`|
| `06_manuscript_figures.R` | Fit final models and produce Figure 2 (map + model predictions) and Figure 3 (SEM path coefficients) | output of 05 | `Figure_2.pdf`, `Figure_3.pdf`, `LMEM_summary.html` |
All intermediate `data/derived/` files are also written out
## Required R packages
CRAN packages: `tidyverse` (dplyr, tidyr, ggplot2, readr, purrr, etc.),
`reshape2`, `tibble`, `mFD`, `FD`, `codyn`, `nlme`, `piecewiseSEM`,
`sjPlot`, `sf`, `rstatix`, `ggrepel`, `viridis`, `maps`, `patchwork`.
GitHub-only package:
- `ecofolio` (used for the community synchrony calculation in
`03_stability_indices.R`). Install with:
```r
# install.packages("devtools")
devtools::install_github("seananderson/ecofolio")
```
We recommend recording exact package versions with `sessionInfo()` (already
done automatically at the end of `06_manuscript_figures.R`, saved to
`Results/sessionInfo.txt`)
## Data availability note
This code expects `rawdata.csv`, `traits.RDS`, the Degree Heating Weeks data, and
the human-gravity shapefile to be deposited alongside this code (or cited
to their original source, in the case of any third-party layer such as the
gravity dataset).