# Figure S1: isotope variables biplots and Spearman tests


library(tidyr)
library(ggplot2)
library(patchwork)

df_iso_a <- df %>%
  filter(`Age bin` == "a") %>%
  select(d13C,d15N,d34S)


spearman_label <- function(data, x, y) {
  d <- data[!is.na(data[[x]]) & !is.na(data[[y]]), ]
  ct <- cor.test(d[[x]], d[[y]], method = "spearman", exact = FALSE)
  
  sprintf("Spearman ρ = %.2f\np = %s",
          unname(ct$estimate),
          format.pval(ct$p.value, digits = 2, eps = 1e-3))
}





lab1 <- spearman_label(df_iso_a, "d13C", "d15N")
lab2 <- spearman_label(df_iso_a, "d13C", "d34S")
lab3 <- spearman_label(df_iso_a, "d34S", "d15N")



p1<-ggplot(df_iso_a,aes(d13C,d15N))+
  geom_point(
    pch = 21,
    color = "black",
    fill  = "lightgrey",
    stroke = 0.4,
    alpha = 0.6,
    size=3
  )+ geom_smooth(method = lm,colour="red",linewidth = 1)+
  annotate("text", x = Inf, y = Inf, label = lab1,
           hjust = 1.05, vjust = 1.15, size = 3.5)+
  theme_classic()

p2<-cn<-ggplot(df_iso_a,aes(d13C,d34S))+
  geom_point(
    pch = 21,
    color = "black",
    fill  = "lightgrey",
    stroke = 0.4,
    alpha = 0.6,
    size=3
  )+ geom_smooth(method = lm,colour="red",linewidth = 1)+
  annotate("text", x = Inf, y = Inf, label = lab2,
           hjust = 1.05, vjust = 1.15, size = 3.5)+
  theme_classic()

p3<-ggplot(df_iso_a,aes(d34S,d15N))+
  geom_point(
    pch = 21,
    color = "black",
    fill  = "lightgrey",
    stroke = 0.4,
    alpha = 0.6,
    size=3
  )+ geom_smooth(method = lm,colour="red",linewidth = 1)+
  annotate("text", x = Inf, y = Inf, label = lab3,
           hjust = 1.05, vjust = 1.15, size = 3.5)+
  theme_classic()


p1 + p2+p3            # side by side

