KAHO_Analysis

MEGA LAB

2024-02-22

R script to analyze 3D habitat complexity and coral point-based annotations

includes data wrangling, visualizations, and statistical tests

# load packages
library(readr)
library(tidyverse)
library(ggplot2)
library(vegan)

# load data
KAHO_3D <- read_csv("Raw_Data/KAHO_3Dmetrics_df.csv") # 3D data (obtained from KAHO_Habitat.R)
KAHO_Coral <- read_csv("Raw_Data/CoralNetAnnotations.csv") # Coral data (obtained from CoralNet)

Data wrangling – Calculate % Coral Cover from raw CoralNet data

KAHO_Cover <- KAHO_Coral %>% # from our raw CoralNet data
  # create "Site" column where values are from "Name" characters before the underscore
  mutate(Site = str_extract(Name, "^[^_]+")) %>%
  # group data based on the Site and Labels
  group_by(Site, Label) %>%
  # for each site, sum up all values for each label 
  count() %>%
  # widens data frame (increasing n of columns & decreasing n of rows)
  # column names will come from "Label" and values from "n"
  # if count is NA, fill with 0
  pivot_wider(names_from = Label, values_from = n, values_fill = 0) %>%
  # remove non-coral labels (for this analyses only focusing on coral cover)
  select(-c("*TURFH","*TURFR", "Unk","Sand", "CCA", "TAPE", "*RDMA")) %>%
  group_by(Site) %>%
  # for each site, perform the following calculations on a row-based manner
  rowwise() %>%
  # for each numeric column (coral species), calculate % cover
  mutate(across(where(is.numeric), ~ (.x / 1000) * 100)) %>%
  # calculate overall % coral cover by summing up % cover of each species 
  mutate(coral_cover = sum(c_across(where(is.numeric)))) %>%
  ungroup()

Data wrangling – 3D

# keep only 1cm resolution
KAHO_3D <- KAHO_3D[KAHO_3D$fac == 1,]
# clean up the site names by keeping only characters before the 1st underscore
KAHO_3D <- KAHO_3D %>% mutate(site = sub("_.*", "", site))

Visualize complexity metrics by site

ggplot(KAHO_3D, aes(x = reorder(site, -mean_vrm), y=mean_vrm)) +
  geom_bar(aes(fill=site), stat = "identity", position = "dodge") + 
  theme_classic()

ggplot(KAHO_3D, aes(x = reorder(site, -mean_slope), y=mean_slope)) +
  geom_bar(aes(fill=site), stat = "identity", position = "dodge") + 
  theme_classic()

ggplot(KAHO_3D, aes(x = reorder(site, -mean_profile_curvature), y=mean_profile_curvature)) +
  geom_bar(aes(fill=site), stat = "identity", position = "dodge") + 
  theme_classic()

Visualize complexity metrics distributions

# Convert data to long format with variables of interest
KAHO_3D_long <- pivot_longer(KAHO_3D, cols = c(mean_slope, mean_vrm, 
  mean_profile_curvature, mean_plan_curvature, surf_complex), names_to = "variable")
# Create a facetted density plot
ggplot(KAHO_3D_long, aes(x = value, fill = variable)) +
  geom_density(alpha = 0.5) + facet_wrap(~ variable, scales = "free") +
  theme_minimal() + theme(legend.position = "none")

Visualize % coral cover by site

ggplot(KAHO_Cover, aes(x = reorder(Site, -coral_cover), y=coral_cover)) +
  geom_bar(aes(fill=Site), stat = "identity", position = "dodge") + 
  theme_classic()

ggplot(KAHO_Cover, aes(x = reorder(Site, -PLOB), y=PLOB)) +
  geom_bar(aes(fill=Site), stat = "identity", position = "dodge") + 
  theme_classic()

ggplot(KAHO_Cover, aes(x = reorder(Site, -PCOM), y=PCOM)) +
  geom_bar(aes(fill=Site), stat = "identity", position = "dodge") + 
  theme_classic()

Visualize coral cover distributions

# Convert data to long format
KAHO_Cover_long <- pivot_longer(KAHO_Cover, cols = -Site,
                                names_to = "variable", values_to = "value")
# Create a facetted density plot
ggplot(KAHO_Cover_long, aes(x = value, fill = variable)) +
  geom_density(alpha = 0.5) + facet_wrap(~ variable, scales = "free") +
  theme_minimal() + theme(legend.position = "none")

Visualize associations between complexity & coral

## combine 3D & Coral data into one dataframe ##
KAHO <- KAHO_Cover %>% full_join(KAHO_3D, by = c("Site" = "site")) 

KAHO %>% ggplot(aes(x = coral_cover, y = mean_vrm)) +
  geom_point(size = 2) +
  geom_smooth(method = "lm", se = FALSE) + theme_classic() 
## `geom_smooth()` using formula = 'y ~ x'

KAHO %>% ggplot(aes(x = coral_cover, y = mean_profile_curvature)) +
  geom_point(size = 2) +
  geom_smooth(method = "lm", se = FALSE) + theme_classic() 
## `geom_smooth()` using formula = 'y ~ x'

Statistical analysis = simple linear regression - complexity ~ coral

summary(lm(mean_vrm ~ coral_cover, data = KAHO))
## 
## Call:
## lm(formula = mean_vrm ~ coral_cover, data = KAHO)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.018742 -0.012320 -0.008822  0.018622  0.023308 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)   
## (Intercept) 0.0585855  0.0125168   4.681  0.00339 **
## coral_cover 0.0014526  0.0004232   3.433  0.01392 * 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.01866 on 6 degrees of freedom
## Multiple R-squared:  0.6626, Adjusted R-squared:  0.6064 
## F-statistic: 11.78 on 1 and 6 DF,  p-value: 0.01392
summary(lm(mean_profile_curvature ~ coral_cover, data = KAHO))
## 
## Call:
## lm(formula = mean_profile_curvature ~ coral_cover, data = KAHO)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.6688 -1.8224 -0.6926  1.2759  5.2245 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)  
## (Intercept) -2.71177    1.90685  -1.422   0.2048  
## coral_cover  0.14145    0.06446   2.194   0.0707 .
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.843 on 6 degrees of freedom
## Multiple R-squared:  0.4452, Adjusted R-squared:  0.3527 
## F-statistic: 4.815 on 1 and 6 DF,  p-value: 0.07065

Multiple Linear Regressions

vrm <- lm(mean_vrm ~ PLOB + PCOM + PMEAN, data = KAHO)
sjPlot::plot_model(vrm, p.shape = TRUE, show.legend = TRUE, show.p = TRUE)

curvature <- lm(mean_profile_curvature ~ PLOB + PCOM + PMEAN, data = KAHO)
sjPlot::plot_model(curvature, p.shape = TRUE, show.legend = TRUE, show.p = TRUE)

for questions, and