R codes to determine the distance at which the number of houses with access to the canal and the proportion of urban area had the maximum effect on the three behavioral measurements: sum of active defensive behaviors, the escape latency and the emergence of the turtle after escaping.

Package installation

library(plyr)
library(dplyr)
library(writexl)
library(rptR)
library(tidyverse)
library(blmeco)
library(PerformanceAnalytics)
library(Hmisc)
library(pspearman)
library(lme4)

Setting of the directory

You need to set the directory by selecting the file where the datasets are located.

Function to linearize a correlation matrix

flattenCorrMatrix <- function(cormat, pmat) {
  ut <- upper.tri(cormat)
  data.frame(
    row = rownames(cormat)[row(cormat)[ut]],
    column = rownames(cormat)[col(cormat)[ut]],
    cor  =(cormat)[ut],
    p = pmat[ut]
    )
}

Determination of the correlations between the behaviors and the number of houses with access to the canal

Load dataset in R

df.house <- read.table("Houses.txt", header = T)

This dataset contains one line per turtle and the entry for each behavior is the mean of all the observations made of the behavior for each turtle. The buffer distances ranged from 100 m to 1000 m at 100-m increments.

Sum of active defensive behaviors (active.defenses)

Creation of the dataset

We need to generate a new dataset that exclude all turtles with no observation for the behavior.

# deletion of the NAs

df.house.ad <- df.house[complete.cases(df.house$active.defenses),]

# deletion of the columns for escape latency and emergence of the turtle after escaping

drop <- c("escape","emergence") 
df.house.ad = df.house.ad[,!(names(df.house.ad) %in% drop)]

Pearson correlations between the sum of active defenses and number of houses for each buffer

table.corr.ad <- rcorr(as.matrix(df.house.ad[,3:13]), type="pearson")

Linearization of the correlation matrix

table.cor.ad <- flattenCorrMatrix(table.corr.ad$r, table.corr.ad$P)

Transformation to only keep correlations with active defensive behaviors

cor.house.ad <- table.cor.ad %>% filter(row == "active.defenses")

# New column with the distance in meters for each buffer

cor.house.ad["buffer"] <- seq(100, 1000, by = 100)

Figure of the correlations between sum of active defensive behaviors and the number of houses with access to the canal according to the different buffer distances.

ggplot(data=cor.house.ad, aes(x=buffer, y=cor)) +
    geom_line() +
    geom_point() + 
  scale_x_continuous(breaks = c(100, 200, 300, 400, 500, 600, 700, 800, 900, 1000))

Escape latency (escape)

Creation of the dataset

We need to generate a new dataset that exclude all turtles with no observation for the behavior.

# deletion of the NAs

df.house.el <- df.house[complete.cases(df.house$escape),]

# deletion of the columns for active defensive behaviors and emergence of the turtle after escaping

drop <- c("active.defenses","emergence") 
df.house.el = df.house.el[,!(names(df.house.el) %in% drop)]

Pearson correlations between the escape latency and number of houses for each buffer

table.corr.EL <- rcorr(as.matrix(df.house.el[,3:13]), type="pearson")

Linearization of the correlation matrix

table.cor.EL <- flattenCorrMatrix(table.corr.EL$r, table.corr.EL$P)

Transformation to only keep correlations with escape latency

cor.house.EL <- table.cor.EL %>% filter(row == "escape")

# New column with the distance in meters for each buffer

cor.house.EL["buffer"] <- seq(100, 1000, by = 100)

Figure of the correlations between escape latency and the number of houses with access to the canal according to the different buffer distances.

ggplot(data=cor.house.EL, aes(x=buffer, y=cor)) +
    geom_line() +
    geom_point() +
  scale_x_continuous(breaks = c(100,200,300,400,500,600,700,800,900,1000))

Emergence of the turtle after escaping (emergence)

Creation of the dataset

We need to generate a new dataset that exclude all turtles with no observation for the behavior.

# deletion of the NAs

df.house.emergence <- df.house[complete.cases(df.house$emergence),]

# deletion of the columns for escape latency and active defensive behaviors

drop <- c("active.defenses","escape") 
df.house.emergence = df.house.emergence[,!(names(df.house.emergence) %in% drop)]

Pearson correlations between the emergence of the turtle and number of houses for each buffer

table.corr.emergence <- rcorr(as.matrix(df.house.emergence[,3:13]), type="pearson")

Linearization of the correlation matrix

table.cor.emergence <- flattenCorrMatrix(table.corr.emergence$r, table.corr.emergence$P)

Transformation of the dataset to only keep correlations with emergence of the turtle

cor.house.emergence <- table.cor.emergence %>% filter(row == "emergence")

# New column with the distance in meters for each buffer

cor.house.emergence["buffer"] <- seq(100, 1000, by = 100)

Figure of the correlations between emergence of the turtle and the number of houses with access to the canal according to the different buffer distances.

ggplot(data=cor.house.emergence, aes(x=buffer, y=cor)) +
    geom_line() +
    geom_point() +
  scale_x_continuous(breaks = c(100, 200, 300, 400, 500, 600, 700, 800, 900, 1000))

Figure of all behaviors together

Creation of a dataset with all the correlations previously calculated

cor.house <- data.frame(matrix(ncol = 4, nrow = 10))
x <- c("buffer", "active.defenses", "escape", "emergence")
colnames(cor.house) <- x
cor.house$buffer <- cor.house.ad$buffer
cor.house$active.defenses <- cor.house.ad$cor
cor.house$escape <- cor.house.EL$cor
cor.house$emergence <- cor.house.emergence$cor
black.text <- element_text(color = "black")

graph.house <- ggplot(cor.house, aes(x=buffer)) +
  geom_line( aes(y=active.defenses, color = "deepskyblue")) + 
  geom_line( aes(y=escape, color = "darkorange1")) +
  geom_line( aes(y=emergence, color = "red")) +
  geom_point(aes(y=active.defenses, color = "deepskyblue")) +
  geom_point( aes(y=escape, color = "darkorange1")) +
  geom_point( aes(y=emergence, color = "red")) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  xlab("Buffer size (m)") +
  ylab("Correlation") +
  geom_hline(yintercept=0, color = "black", size=0.5) +
  theme(axis.text = black.text) +
  labs(title="Correlations - Number of houses") +
  theme(plot.title = element_text(hjust = 0.5))+
  scale_x_continuous(limits=c(100,1000), breaks = c(100,200,300,400,500,600,700,800,900,1000)) +
  scale_y_continuous(limits=c(-0.16,0.16), breaks = c(-0.15, -0.10, -0.05, 0, 0.05, 0.10, 0.15))+
    scale_colour_manual(name = 'Behaviour traits', 
    values =c("deepskyblue"="deepskyblue","darkorange1"="darkorange1", "red"="red"), labels = c("Active defensive behaviors","Escape latency", "Emergence of the turtle after escaping"))
  
graph.house

Determination of the correlations between the behaviors and the proportion of urban area

Load dataset in R

df.urban <- read.table("Urban.txt", header = T)

This dataset contains one line per turtle and the entry for each behavior is the mean of all the observations made of the behavior for each turtle. The buffer distances ranged from 100 m to 1000 m at 100-m increments.

Sum of active defensive behaviors (active.defenses)

Creation of the dataset

We need to generate a new dataset that exclude all turtles with no observation for the behavior.

# deletion of the NAs

df.urban.ad <- df.urban[complete.cases(df.urban$active.defenses),]

# deletion of the column with data of escape latency and emergence of the turtle after escaping
drop <- c("escape","emergence") 
df.urban.ad = df.urban.ad[,!(names(df.urban.ad) %in% drop)]

Pearson correlations between the sum of active defenses and proportion of urban area for each buffer

table.corr.urban.ad <- rcorr(as.matrix(df.urban.ad[,3:13]), type="pearson")

Linearization of the correlation matrix

table.cor.urban.ad <- flattenCorrMatrix(table.corr.urban.ad$r, table.corr.urban.ad$P)

Transformation to only keep correlations with active defensive behaviors

cor.urban.ad <- table.cor.urban.ad %>% filter(row == "active.defenses")

# New column with the distance in meters of each buffer

cor.urban.ad["buffer"] <- seq(100, 1000, by = 100)

Figure of the correlations between sum of active defensive behaviors and the proportion of urban area according to the different buffer distances.

ggplot(data=cor.urban.ad, aes(x=buffer, y=cor)) +
    geom_line() +
    geom_point() + 
  scale_x_continuous(breaks = c(100, 200, 300, 400, 500, 600, 700, 800, 900, 1000))

Escape latency (escape)

Creation of the dataset

We need to generate a new dataset that exclude all turtles with no observation for the behavior.

# deletion of the NAs

df.urban.el <- df.urban[complete.cases(df.urban$escape),]

# deletion of the columns for active defensive behaviors and emergence of the turtle after escaping

drop <- c("active.defenses","emergence") 
df.urban.el = df.urban.el[,!(names(df.urban.el) %in% drop)]

Pearson correlations between the escape latency and proportion of urban area for each buffer

table.corr.urban.EL <- rcorr(as.matrix(df.urban.el[,3:13]), type="pearson")

Linearization of the correlation matrix

table.cor.urban.EL <- flattenCorrMatrix(table.corr.urban.EL$r, table.corr.urban.EL$P)

Transformation to only keep correlations with escape latency

cor.urban.EL <- table.cor.urban.EL %>% filter(row == "escape")

# New column with the distance in meters for each buffer

cor.urban.EL["buffer"] <- seq(100, 1000, by = 100)

Figure of the correlations between escape latency and the proportion of urban area according to the different buffer distances.

ggplot(data=cor.urban.EL, aes(x=buffer, y=cor)) +
    geom_line() +
    geom_point() +
  scale_x_continuous(breaks = c(100,200,300,400,500,600,700,800,900,1000))

Emergence of the turtle after escaping (emergence)

Creation of the dataset

We need to generate a new dataset that exclude all turtles with no observation for the behavior.

# deletion of the NAs

df.urban.emergence <- df.urban[complete.cases(df.urban$emergence),]

# deletion of the columns for escape latency and active defensive behaviors

drop <- c("active.defenses","escape") 
df.urban.emergence = df.urban.emergence[,!(names(df.urban.emergence) %in% drop)]

Pearson correlations between the emergence of the turtle and proportion of urban area for each buffer

table.corr.urban.emergence <- rcorr(as.matrix(df.urban.emergence[,3:13]), type="pearson")

Linearization of the correlation matrix

table.cor.urban.emergence <- flattenCorrMatrix(table.corr.urban.emergence$r, table.corr.urban.emergence$P)

Transformation of the dataset to only keep correlations with emergence of the turtle

cor.urban.emergence <- table.cor.urban.emergence %>% filter(row == "emergence")

# New column with the distance in meters for each buffer

cor.urban.emergence["buffer"] <- seq(100, 1000, by = 100)

Figure of the correlations between emergence of the turtle and the proportion of urban area according to the different buffer distances.

ggplot(data=cor.urban.emergence, aes(x=buffer, y=cor)) +
    geom_line() +
    geom_point() +
  scale_x_continuous(breaks = c(100, 200, 300, 400, 500, 600, 700, 800, 900, 1000))

Figure of all behaviors together

Creation of a dataset with all the correlations previously calculated

cor.urban <- data.frame(matrix(ncol = 4, nrow = 10))
x <- c("buffer", "active.defenses", "escape", "emergence")
colnames(cor.urban) <- x
cor.urban$buffer <- cor.urban.ad$buffer
cor.urban$active.defenses <- cor.urban.ad$cor
cor.urban$escape <- cor.urban.EL$cor
cor.urban$emergence <- cor.urban.emergence$cor
black.text <- element_text(color = "black")

graph.urban <- ggplot(cor.urban, aes(x=buffer)) +
  geom_line( aes(y=active.defenses, color = "deepskyblue")) + 
  geom_line( aes(y=escape, color = "darkorange1")) +
  geom_line( aes(y=emergence, color = "red")) +
  geom_point(aes(y=active.defenses, color = "deepskyblue")) +
  geom_point( aes(y=escape, color = "darkorange1")) +
  geom_point( aes(y=emergence, color = "red")) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  xlab("Buffer size (m)") +
  ylab("Correlation") +
  geom_hline(yintercept=0, color = "black", size=0.5) +
  theme(axis.text = black.text) +
  labs(title="Correlations - Proportion of urban areas") +
  theme(plot.title = element_text(hjust = 0.5))+
  scale_x_continuous(limits=c(100,1000), breaks = c(100,200,300,400,500,600,700,800,900,1000)) +
  scale_y_continuous(limits=c(-0.18,0.18), breaks = c(-0.15, -0.10, -0.05, 0, 0.05, 0.10, 0.15))+
    scale_colour_manual(name = 'Behaviour traits', 
    values =c("deepskyblue"="deepskyblue","darkorange1"="darkorange1", "red"="red"), labels = c("Active defensive behaviors","Escape latency", "Emergence of the turtle after escaping"))
  
graph.urban