---
title: "CMR file for Juveniles"
author: "Amelie Fargevieille"
date: "7/23/2020"
output: html_document
---


```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE,warning=FALSE)
library("tidyverse",quietly=TRUE)
library("openxlsx",quietly=TRUE)
```

This script intends to create the .txt containing CMR information to test phenotypic selection from juveniles, especially if SVL and/or body condition before first winter relate to survival to next season. We need to include individuals define in October as "individuals of the year". We define the threshold to be 40 mm for females and 50 mm for males. The file should include all females <40mm and males <50mm in October of each year and their recaptures in the subsequent years. We also need to include their SVL and mass in October of the year they were born.   


Importing data
```{r}
db = read.xlsx("C:/Users/akf0020/OneDrive - Auburn University/PhenotypicSelection/RawData/AnolisDB_06192020.xlsx", sheet = 1, detectDates = TRUE)
```


Selecting variables of interest, selecting sagrei only from Matanzas islands only, selecting only new and recap individuals
```{r}
dbr = db %>%
  dplyr::select(1:6, 8, 9, 13, 16:17, 28)%>%
  filter(Island %in% c("F", "H", "K", "L", "M", "N") & Species %in% "SAGREI") %>%
  mutate(Island = replace(Island, Island == "F", "F4")) %>% 
  mutate(Island = replace(Island, Island == "H", "M3")) %>%
  mutate(Island = replace(Island, Island == "K", "F2")) %>%
  mutate(Island = replace(Island, Island == "L", "M5")) %>%
  mutate(Island = replace(Island, Island == "M", "F3")) %>%
  mutate(Island = replace(Island, Island == "N", "M4"))
```

Removing individuals with non-confirmed sex

```{r}
dbr = dbr %>%
  filter(!Sex %in% c("?", "M?", "F?", "."))
dbr = dbr %>%
  filter(!is.na(Sex))
dbr$SVL = as.numeric(dbr$SVL)
dbr$Mass = as.numeric(dbr$Mass)
dbr$DlapArea = as.numeric(dbr$DlapArea)
```

```{r}
dbr$Trip=factor(dbr$Trip,c("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22"))
```

```{r}
en<-reshape2::dcast(dbr, ID~Trip)
colnames(en)=c("ID","Trip1","Trip2","Trip3","Trip4","Trip5","Trip6","Trip7","Trip8","Trip9","Trip10","Trip11","Trip12","Trip13","Trip14","Trip15","Trip16","Trip17","Trip18","Trip19","Trip20","Trip21","Trip22")
```

```{r}
en1 = en %>%
 filter_at(vars(Trip4, Trip7, Trip10, Trip14, Trip18), any_vars(. == 1))
```


I need to select trips from October to exclude any individual bigger than 40 and 50 mm.

```{r}
trip4 = filter(dbr, Trip == "4") %>%
  filter(if(Sex == "M") SVL <50 else SVL <40) %>%
  dplyr::select(ID, Island, Sex, SVL, Mass)
trip7 = filter(dbr, Trip == "7") %>%
  filter(if(Sex == "M") SVL <50 else SVL <40) %>%
  dplyr::select(ID, Island, Sex, SVL, Mass)
trip10 = filter(dbr, Trip == "10") %>%
  filter(if(Sex == "M") SVL <50 else SVL <40) %>%
  dplyr::select(ID, Island, Sex, SVL, Mass)
trip14 = filter(dbr, Trip == "14") %>%
  filter(if(Sex == "M") SVL <50 else SVL <40) %>%
  dplyr::select(ID, Island, Sex, SVL, Mass)
trip18 = filter(dbr, Trip == "18") %>%
  filter(if(Sex == "M") SVL <50 else SVL <40) %>%
  dplyr::select(ID, Island, Sex, SVL, Mass)
```

Trip4
```{r}
t4 = en1 %>%
  filter(Trip4 == 1) %>%
  filter_at(vars(Trip1, Trip2, Trip3), (all_vars(. == 0)))
```

```{r}
t4.1 = t4 %>%
  mutate(Y2013 = apply(t4[ , c("Trip5", "Trip6", "Trip7")] == 1, 1, any)) %>%
  mutate(Y2014 = apply(t4[ , c("Trip8", "Trip9", "Trip10")] == 1, 1, any)) %>%
  mutate(Y2015 = apply(t4[ , c("Trip11", "Trip12", "Trip13", "Trip14")] == 1, 1, any)) %>%
  mutate(Y2016 = apply(t4[ , c("Trip15", "Trip16", "Trip17", "Trip18")] == 1, 1, any)) %>%
  mutate(Y2017 = apply(t4[ , c("Trip19", "Trip20", "Trip21", "Trip22")] == 1, 1, any)) %>%
  mutate(Y2013, ifelse(Y2013 == TRUE, 1, 0)) %>%
  mutate(Y2014, ifelse(Y2014 == TRUE, 1, 0)) %>%
  mutate(Y2015, ifelse(Y2015 == TRUE, 1, 0)) %>%
  mutate(Y2016, ifelse(Y2016 == TRUE, 1, 0)) %>%
  mutate(Y2017, ifelse(Y2017 == TRUE, 1, 0)) %>%
  dplyr::select(1, 5, 29:33)
colnames(t4.1) = c("ID", "Y2012", "Y2013", "Y2014", "Y2015", "Y2016", "Y2017")
```



```{r}
t4.2 = right_join(t4.1, trip4, by = "ID")
```

Trip7
```{r}
t7 = en1 %>%
  filter(Trip7 == 1) %>%
  filter_at(vars(Trip1, Trip2, Trip3, Trip4, Trip5), (all_vars(. == 0)))
```

```{r}
t7.1 = t7 %>%
  mutate(Y2014 = apply(t7[ , c("Trip8", "Trip9", "Trip10")] == 1, 1, any)) %>%
  mutate(Y2015 = apply(t7[ , c("Trip11", "Trip12", "Trip13", "Trip14")] == 1, 1, any)) %>%
  mutate(Y2016 = apply(t7[ , c("Trip15", "Trip16", "Trip17", "Trip18")] == 1, 1, any)) %>%
  mutate(Y2017 = apply(t7[ , c("Trip19", "Trip20", "Trip21", "Trip22")] == 1, 1, any)) %>%
  mutate(Y2014, ifelse(Y2014 == TRUE, 1, 0)) %>%
  mutate(Y2015, ifelse(Y2015 == TRUE, 1, 0)) %>%
  mutate(Y2016, ifelse(Y2016 == TRUE, 1, 0)) %>%
  mutate(Y2017, ifelse(Y2017 == TRUE, 1, 0)) %>%
  dplyr::select(1, 5, 8, 28:31)
colnames(t7.1) = c("ID", "Y2012", "Y2013", "Y2014", "Y2015", "Y2016", "Y2017")
```


```{r}
t7.2 = right_join(t7.1, trip7, by = "ID")
```


Trip10
```{r}
t10 = en1 %>%
  filter(Trip10 == 1) %>%
  filter_at(vars(Trip1, Trip2, Trip3, Trip4, Trip5, Trip6, Trip7, Trip8), (all_vars(. == 0)))
```

```{r}
t10.1 = t10 %>%
  mutate(Y2015 = apply(t10[ , c("Trip11", "Trip12", "Trip13", "Trip14")] == 1, 1, any)) %>%
  mutate(Y2016 = apply(t10[ , c("Trip15", "Trip16", "Trip17", "Trip18")] == 1, 1, any)) %>%
  mutate(Y2017 = apply(t10[ , c("Trip19", "Trip20", "Trip21", "Trip22")] == 1, 1, any)) %>%
  mutate(Y2015, ifelse(Y2015 == TRUE, 1, 0)) %>%
  mutate(Y2016, ifelse(Y2016 == TRUE, 1, 0)) %>%
  mutate(Y2017, ifelse(Y2017 == TRUE, 1, 0)) %>%
  dplyr::select(1, 5, 8, 11, 27:29)
colnames(t10.1) = c("ID", "Y2012", "Y2013", "Y2014", "Y2015", "Y2016", "Y2017")
```


```{r}
t10.2 = right_join(t10.1, trip10, by = "ID")
```

Trip14
```{r}
t14 = en1 %>%
  filter(Trip14 == 1) %>%
  filter_at(vars(Trip1, Trip2, Trip3, Trip4, Trip5, Trip6, Trip7, Trip8, Trip9, Trip10, Trip11, Trip12), (all_vars(. == 0)))
```

```{r}
t14.1 = t14 %>%
  mutate(Y2016 = apply(t14[ , c("Trip15", "Trip16", "Trip17", "Trip18")] == 1, 1, any)) %>%
  mutate(Y2017 = apply(t14[ , c("Trip19", "Trip20", "Trip21", "Trip22")] == 1, 1, any)) %>%
  mutate(Y2016, ifelse(Y2016 == TRUE, 1, 0)) %>%
  mutate(Y2017, ifelse(Y2017 == TRUE, 1, 0)) %>%
  dplyr::select(1, 5, 8, 11, 15, 26:27)
colnames(t14.1) = c("ID", "Y2012", "Y2013", "Y2014", "Y2015", "Y2016", "Y2017")
```


```{r}
t14.2 = right_join(t14.1, trip14, by = "ID")
```


Trip18
```{r}
t18 = en1 %>%
  filter(Trip18 == 1) %>%
  filter_at(vars(Trip1, Trip2, Trip3, Trip4, Trip5, Trip6, Trip7, Trip8, Trip9, Trip10, Trip11, Trip12, Trip13, Trip14, Trip15, Trip16), (all_vars(. == 0)))
```

```{r}
t18.1 = t18 %>%
  mutate(Y2017 = apply(t18[ , c("Trip19", "Trip20", "Trip21", "Trip22")] == 1, 1, any)) %>%
  mutate(Y2017, ifelse(Y2017 == TRUE, 1, 0))  %>%
  dplyr::select(1, 5, 8, 11, 15, 19, 25)
colnames(t18.1) = c("ID", "Y2012", "Y2013", "Y2014", "Y2015", "Y2016", "Y2017")
```


```{r}
t18.2 = right_join(t18.1, trip18, by = "ID")
```

```{r}
juv = do.call("rbind", list(t4.2, t7.2, t10.2, t14.2, t18.2))
```
#SVL and mass distribution


```{r}
svl4 = ggplot(t4.2, aes(x = SVL)) + geom_density() +
  labs(title = "Trip 4") +
  facet_wrap(. ~ Sex, labeller = labeller(Sex = c("F" = "Female", "M" = "Male"))) +
  scale_x_continuous(name = "SVL", limits = c(15, 50), breaks = seq(15, 50, 5)) +
  theme(plot.margin = margin(t = 0.25, b = 0.25, r = 0.25, l = 0.25, "cm"),
        plot.title = element_text(size = 16, color = "black", family = "Arial", hjust = 0.5),
        panel.background=element_blank(),
        panel.border = element_blank(),
        panel.grid = element_blank(),
        strip.background = element_blank(),
        strip.text = element_text(size = 12, color = "black", family = "Arial", face = "bold"),
        text = element_text(size=10,color="black", family = "Arial"),
        axis.line = element_line(color = "black", size = 0.5),
        axis.ticks.x = element_blank(),
        axis.ticks.y = element_line(color = "black", size = 0.5),
        axis.text.x = element_text(size=10, color="black", family = "Arial"),
        axis.text.y = element_text(size=10, color="black", family = "Arial"),
        axis.title.y = element_text(size=12, color="black", family = "Arial"),
        legend.position = c(0.9, 0.9),
        legend.justification = c(0.5, 0.5),
        legend.box.background = element_rect(color = "transparent", fill = "transparent"),
        legend.box.margin = margin(t = 0.25, r =0.25, b = 0.25, l = 0.25, unit = "cm"),
        legend.key = element_blank())
svl4
```


```{r}
svl7 = ggplot(t7.2, aes(x = SVL)) + geom_density() +
  labs(title = "Trip 7") +
  facet_wrap(. ~ Sex, labeller = labeller(Sex = c("F" = "Female", "M" = "Male"))) +
  scale_x_continuous(name = "SVL", limits = c(15, 50), breaks = seq(15, 50, 5)) +
  theme(plot.margin = margin(t = 0.25, b = 0.25, r = 0.25, l = 0.25, "cm"),
        plot.title = element_text(size = 16, color = "black", family = "Arial", hjust = 0.5),
        panel.background=element_blank(),
        panel.border = element_blank(),
        panel.grid = element_blank(),
        strip.background = element_blank(),
        strip.text = element_text(size = 12, color = "black", family = "Arial", face = "bold"),
        text = element_text(size=10,color="black", family = "Arial"),
        axis.line = element_line(color = "black", size = 0.5),
        axis.ticks.x = element_blank(),
        axis.ticks.y = element_line(color = "black", size = 0.5),
        axis.text.x = element_text(size=10, color="black", family = "Arial"),
        axis.text.y = element_text(size=10, color="black", family = "Arial"),
        axis.title.y = element_text(size=12, color="black", family = "Arial"),
        legend.position = c(0.9, 0.9),
        legend.justification = c(0.5, 0.5),
        legend.box.background = element_rect(color = "transparent", fill = "transparent"),
        legend.box.margin = margin(t = 0.25, r =0.25, b = 0.25, l = 0.25, unit = "cm"),
        legend.key = element_blank())
svl7
```


```{r}
svl10 = ggplot(t10.2, aes(x = SVL)) + geom_density() +
  labs(title = "Trip 10") +
  facet_wrap(. ~ Sex, labeller = labeller(Sex = c("F" = "Female", "M" = "Male"))) +
  scale_x_continuous(name = "SVL", limits = c(15, 50), breaks = seq(15, 50, 5)) +
  theme(plot.margin = margin(t = 0.25, b = 0.25, r = 0.25, l = 0.25, "cm"),
        plot.title = element_text(size = 16, color = "black", family = "Arial", hjust = 0.5),
        panel.background=element_blank(),
        panel.border = element_blank(),
        panel.grid = element_blank(),
        strip.background = element_blank(),
        strip.text = element_text(size = 12, color = "black", family = "Arial", face = "bold"),
        text = element_text(size=10,color="black", family = "Arial"),
        axis.line = element_line(color = "black", size = 0.5),
        axis.ticks.x = element_blank(),
        axis.ticks.y = element_line(color = "black", size = 0.5),
        axis.text.x = element_text(size=10, color="black", family = "Arial"),
        axis.text.y = element_text(size=10, color="black", family = "Arial"),
        axis.title.y = element_text(size=12, color="black", family = "Arial"),
        legend.position = c(0.9, 0.9),
        legend.justification = c(0.5, 0.5),
        legend.box.background = element_rect(color = "transparent", fill = "transparent"),
        legend.box.margin = margin(t = 0.25, r =0.25, b = 0.25, l = 0.25, unit = "cm"),
        legend.key = element_blank())
svl10
```

```{r}
svl14 = ggplot(t14.2, aes(x = SVL)) + geom_density() +
  labs(title = "Trip 14") +
  facet_wrap(. ~ Sex, labeller = labeller(Sex = c("F" = "Female", "M" = "Male"))) +
  scale_x_continuous(name = "SVL", limits = c(15, 50), breaks = seq(15, 50, 5)) +
  theme(plot.margin = margin(t = 0.25, b = 0.25, r = 0.25, l = 0.25, "cm"),
        plot.title = element_text(size = 16, color = "black", family = "Arial", hjust = 0.5),
        panel.background=element_blank(),
        panel.border = element_blank(),
        panel.grid = element_blank(),
        strip.background = element_blank(),
        strip.text = element_text(size = 12, color = "black", family = "Arial", face = "bold"),
        text = element_text(size=10,color="black", family = "Arial"),
        axis.line = element_line(color = "black", size = 0.5),
        axis.ticks.x = element_blank(),
        axis.ticks.y = element_line(color = "black", size = 0.5),
        axis.text.x = element_text(size=10, color="black", family = "Arial"),
        axis.text.y = element_text(size=10, color="black", family = "Arial"),
        axis.title.y = element_text(size=12, color="black", family = "Arial"),
        legend.position = c(0.9, 0.9),
        legend.justification = c(0.5, 0.5),
        legend.box.background = element_rect(color = "transparent", fill = "transparent"),
        legend.box.margin = margin(t = 0.25, r =0.25, b = 0.25, l = 0.25, unit = "cm"),
        legend.key = element_blank())
svl14
```

```{r}
svl18 = ggplot(t18.2, aes(x = SVL)) + geom_density() +
  labs(title = "Trip 18") +
  facet_wrap(. ~ Sex, labeller = labeller(Sex = c("F" = "Female", "M" = "Male"))) +
  scale_x_continuous(name = "SVL", limits = c(15, 50), breaks = seq(15, 50, 5)) +
  theme(plot.margin = margin(t = 0.25, b = 0.25, r = 0.25, l = 0.25, "cm"),
        plot.title = element_text(size = 16, color = "black", family = "Arial", hjust = 0.5),
        panel.background=element_blank(),
        panel.border = element_blank(),
        panel.grid = element_blank(),
        strip.background = element_blank(),
        strip.text = element_text(size = 12, color = "black", family = "Arial", face = "bold"),
        text = element_text(size=10,color="black", family = "Arial"),
        axis.line = element_line(color = "black", size = 0.5),
        axis.ticks.x = element_blank(),
        axis.ticks.y = element_line(color = "black", size = 0.5),
        axis.text.x = element_text(size=10, color="black", family = "Arial"),
        axis.text.y = element_text(size=10, color="black", family = "Arial"),
        axis.title.y = element_text(size=12, color="black", family = "Arial"),
        legend.position = c(0.9, 0.9),
        legend.justification = c(0.5, 0.5),
        legend.box.background = element_rect(color = "transparent", fill = "transparent"),
        legend.box.margin = margin(t = 0.25, r =0.25, b = 0.25, l = 0.25, unit = "cm"),
        legend.key = element_blank())
svl18
```

```{r}
mass4 = ggplot(t4.2, aes(x = Mass)) + geom_density() +
  labs(title = "Trip 4") +
  facet_wrap(. ~ Sex, labeller = labeller(Sex = c("F" = "Female", "M" = "Male"))) +
  scale_x_continuous(name = "Mass", limits = c(0, 2.5), breaks = seq(0, 2.5, 0.5)) +
  theme(plot.margin = margin(t = 0.25, b = 0.25, r = 0.25, l = 0.25, "cm"),
        plot.title = element_text(size = 16, color = "black", family = "Arial", hjust = 0.5),
        panel.background=element_blank(),
        panel.border = element_blank(),
        panel.grid = element_blank(),
        strip.background = element_blank(),
        strip.text = element_text(size = 12, color = "black", family = "Arial", face = "bold"),
        text = element_text(size=10,color="black", family = "Arial"),
        axis.line = element_line(color = "black", size = 0.5),
        axis.ticks.x = element_blank(),
        axis.ticks.y = element_line(color = "black", size = 0.5),
        axis.text.x = element_text(size=10, color="black", family = "Arial"),
        axis.text.y = element_text(size=10, color="black", family = "Arial"),
        axis.title.y = element_text(size=12, color="black", family = "Arial"),
        legend.position = c(0.9, 0.9),
        legend.justification = c(0.5, 0.5),
        legend.box.background = element_rect(color = "transparent", fill = "transparent"),
        legend.box.margin = margin(t = 0.25, r =0.25, b = 0.25, l = 0.25, unit = "cm"),
        legend.key = element_blank())
mass4
```


```{r}
mass7 = ggplot(t7.2, aes(x = Mass)) + geom_density() +
  labs(title = "Trip 7") +
  facet_wrap(. ~ Sex, labeller = labeller(Sex = c("F" = "Female", "M" = "Male"))) +
  scale_x_continuous(name = "Mass", limits = c(0, 2.5), breaks = seq(0, 2.5, 0.5)) +
  theme(plot.margin = margin(t = 0.25, b = 0.25, r = 0.25, l = 0.25, "cm"),
        plot.title = element_text(size = 16, color = "black", family = "Arial", hjust = 0.5),
        panel.background=element_blank(),
        panel.border = element_blank(),
        panel.grid = element_blank(),
        strip.background = element_blank(),
        strip.text = element_text(size = 12, color = "black", family = "Arial", face = "bold"),
        text = element_text(size=10,color="black", family = "Arial"),
        axis.line = element_line(color = "black", size = 0.5),
        axis.ticks.x = element_blank(),
        axis.ticks.y = element_line(color = "black", size = 0.5),
        axis.text.x = element_text(size=10, color="black", family = "Arial"),
        axis.text.y = element_text(size=10, color="black", family = "Arial"),
        axis.title.y = element_text(size=12, color="black", family = "Arial"),
        legend.position = c(0.9, 0.9),
        legend.justification = c(0.5, 0.5),
        legend.box.background = element_rect(color = "transparent", fill = "transparent"),
        legend.box.margin = margin(t = 0.25, r =0.25, b = 0.25, l = 0.25, unit = "cm"),
        legend.key = element_blank())
mass7
```


```{r}
mass10 = ggplot(t10.2, aes(x = Mass)) + geom_density() +
  labs(title = "Trip 10") +
  facet_wrap(. ~ Sex, labeller = labeller(Sex = c("F" = "Female", "M" = "Male"))) +
  scale_x_continuous(name = "Mass", limits = c(0, 2.5), breaks = seq(0, 2.5, 0.5)) +
  theme(plot.margin = margin(t = 0.25, b = 0.25, r = 0.25, l = 0.25, "cm"),
        plot.title = element_text(size = 16, color = "black", family = "Arial", hjust = 0.5),
        panel.background=element_blank(),
        panel.border = element_blank(),
        panel.grid = element_blank(),
        strip.background = element_blank(),
        strip.text = element_text(size = 12, color = "black", family = "Arial", face = "bold"),
        text = element_text(size=10,color="black", family = "Arial"),
        axis.line = element_line(color = "black", size = 0.5),
        axis.ticks.x = element_blank(),
        axis.ticks.y = element_line(color = "black", size = 0.5),
        axis.text.x = element_text(size=10, color="black", family = "Arial"),
        axis.text.y = element_text(size=10, color="black", family = "Arial"),
        axis.title.y = element_text(size=12, color="black", family = "Arial"),
        legend.position = c(0.9, 0.9),
        legend.justification = c(0.5, 0.5),
        legend.box.background = element_rect(color = "transparent", fill = "transparent"),
        legend.box.margin = margin(t = 0.25, r =0.25, b = 0.25, l = 0.25, unit = "cm"),
        legend.key = element_blank())
mass10
```

```{r}
mass14 = ggplot(t14.2, aes(x = Mass)) + geom_density() +
  labs(title = "Trip 14") +
  facet_wrap(. ~ Sex, labeller = labeller(Sex = c("F" = "Female", "M" = "Male"))) +
  scale_x_continuous(name = "Mass", limits = c(0, 2.5), breaks = seq(0, 2.5, 0.5)) +
  theme(plot.margin = margin(t = 0.25, b = 0.25, r = 0.25, l = 0.25, "cm"),
        plot.title = element_text(size = 16, color = "black", family = "Arial", hjust = 0.5),
        panel.background=element_blank(),
        panel.border = element_blank(),
        panel.grid = element_blank(),
        strip.background = element_blank(),
        strip.text = element_text(size = 12, color = "black", family = "Arial", face = "bold"),
        text = element_text(size=10,color="black", family = "Arial"),
        axis.line = element_line(color = "black", size = 0.5),
        axis.ticks.x = element_blank(),
        axis.ticks.y = element_line(color = "black", size = 0.5),
        axis.text.x = element_text(size=10, color="black", family = "Arial"),
        axis.text.y = element_text(size=10, color="black", family = "Arial"),
        axis.title.y = element_text(size=12, color="black", family = "Arial"),
        legend.position = c(0.9, 0.9),
        legend.justification = c(0.5, 0.5),
        legend.box.background = element_rect(color = "transparent", fill = "transparent"),
        legend.box.margin = margin(t = 0.25, r =0.25, b = 0.25, l = 0.25, unit = "cm"),
        legend.key = element_blank())
mass14
```

```{r}
mass18 = ggplot(t18.2, aes(x = Mass)) + geom_density() +
  labs(title = "Trip 18") +
  facet_wrap(. ~ Sex, labeller = labeller(Sex = c("F" = "Female", "M" = "Male"))) +
  scale_x_continuous(name = "Mass", limits = c(0, 2.5), breaks = seq(0, 2.5, 0.5)) +
  theme(plot.margin = margin(t = 0.25, b = 0.25, r = 0.25, l = 0.25, "cm"),
        plot.title = element_text(size = 16, color = "black", family = "Arial", hjust = 0.5),
        panel.background=element_blank(),
        panel.border = element_blank(),
        panel.grid = element_blank(),
        strip.background = element_blank(),
        strip.text = element_text(size = 12, color = "black", family = "Arial", face = "bold"),
        text = element_text(size=10,color="black", family = "Arial"),
        axis.line = element_line(color = "black", size = 0.5),
        axis.ticks.x = element_blank(),
        axis.ticks.y = element_line(color = "black", size = 0.5),
        axis.text.x = element_text(size=10, color="black", family = "Arial"),
        axis.text.y = element_text(size=10, color="black", family = "Arial"),
        axis.title.y = element_text(size=12, color="black", family = "Arial"),
        legend.position = c(0.9, 0.9),
        legend.justification = c(0.5, 0.5),
        legend.box.background = element_rect(color = "transparent", fill = "transparent"),
        legend.box.margin = margin(t = 0.25, r =0.25, b = 0.25, l = 0.25, unit = "cm"),
        legend.key = element_blank())
mass18
```

```{r fig.width = 10, fig.height = 20, fig.align = "center"}
library(cowplot)
plot_svl = ggdraw() +
  draw_plot(svl4, x = 0, y = 0.8, width = 0.5, height = 0.2, hjust = 0) +
  draw_plot(svl7, x = 0, y = 0.6, width = 0.5, height = 0.2, hjust = 0) +
  draw_plot(svl10, x = 0, y = 0.4, width = 0.5, height = 0.2, hjust = 0) +
  draw_plot(svl14, x = 0, y = 0.2, width = 0.5, height = 0.2, hjust = 0) +
  draw_plot(svl18, x = 0, y = 0, width = 0.5, height = 0.2, hjust = 0) +
  draw_plot(mass4, x = 0.5, y = 0.8, width = 0.5, height = 0.2, hjust = 0) +
  draw_plot(mass7, x = 0.5, y = 0.6, width = 0.5, height = 0.2, hjust = 0) +
  draw_plot(mass10, x = 0.5, y = 0.4, width = 0.5, height = 0.2, hjust = 0) +
  draw_plot(mass14, x = 0.5, y = 0.2, width = 0.5, height = 0.2, hjust = 0) +
  draw_plot(mass18, x = 0.5, y = 0, width = 0.5, height = 0.2, hjust = 0) 
save_plot("~/Desktop/Auburn/PhenotypicSelection/Juv_massSVL_distribution.png", plot_svl, ncol = 1, nrow = 1, base_height = 15, base_width = 7.5)
```


```{r}
svl_juv = ggplot(juv, aes(x = SVL)) + geom_density() +
  labs(title = "Across time") +
  facet_wrap(. ~ Sex, labeller = labeller(Sex = c("F" = "Female", "M" = "Male"))) +
  scale_x_continuous(name = "SVL", limits = c(15, 80), breaks = seq(20, 70, 10)) +
  theme(plot.margin = margin(t = 0.25, b = 0.25, r = 0.25, l = 0.25, "cm"),
        plot.title = element_text(size = 16, color = "black", family = "Arial", hjust = 0.5),
        panel.background=element_blank(),
        panel.border = element_blank(),
        panel.grid = element_blank(),
        strip.background = element_blank(),
        strip.text = element_text(size = 12, color = "black", family = "Arial", face = "bold"),
        text = element_text(size=10,color="black", family = "Arial"),
        axis.line = element_line(color = "black", size = 0.5),
        axis.ticks.x = element_blank(),
        axis.ticks.y = element_line(color = "black", size = 0.5),
        axis.text.x = element_text(size=10, color="black", family = "Arial"),
        axis.text.y = element_text(size=10, color="black", family = "Arial"),
        axis.title.y = element_text(size=12, color="black", family = "Arial"),
        legend.position = c(0.9, 0.9),
        legend.justification = c(0.5, 0.5),
        legend.box.background = element_rect(color = "transparent", fill = "transparent"),
        legend.box.margin = margin(t = 0.25, r =0.25, b = 0.25, l = 0.25, unit = "cm"),
        legend.key = element_blank())
mass_juv = ggplot(juv, aes(x = Mass)) + geom_density() +
  labs(title = "Across time") +
  facet_wrap(. ~ Sex, labeller = labeller(Sex = c("F" = "Female", "M" = "Male"))) +
  scale_x_continuous(name = "Mass", limits = c(0, 2.5), breaks = seq(0, 2.5, 0.5)) +
  theme(plot.margin = margin(t = 0.25, b = 0.25, r = 0.25, l = 0.25, "cm"),
        plot.title = element_text(size = 16, color = "black", family = "Arial", hjust = 0.5),
        panel.background=element_blank(),
        panel.border = element_blank(),
        panel.grid = element_blank(),
        strip.background = element_blank(),
        strip.text = element_text(size = 12, color = "black", family = "Arial", face = "bold"),
        text = element_text(size=10,color="black", family = "Arial"),
        axis.line = element_line(color = "black", size = 0.5),
        axis.ticks.x = element_blank(),
        axis.ticks.y = element_line(color = "black", size = 0.5),
        axis.text.x = element_text(size=10, color="black", family = "Arial"),
        axis.text.y = element_text(size=10, color="black", family = "Arial"),
        axis.title.y = element_text(size=12, color="black", family = "Arial"),
        legend.position = c(0.9, 0.9),
        legend.justification = c(0.5, 0.5),
        legend.box.background = element_rect(color = "transparent", fill = "transparent"),
        legend.box.margin = margin(t = 0.25, r =0.25, b = 0.25, l = 0.25, unit = "cm"),
        legend.key = element_blank())
```


```{r fig.width = 10, fig.height = 5, fig.align = "center"}
plot_juv = ggdraw() +
  draw_plot(svl_juv, x = 0, y = 0, width = 0.5, height = 1, hjust = 0) +
  draw_plot(mass_juv, x = 0.5, y = 0, width = 0.5, height = 1, hjust = 0) 
save_plot("~/Desktop/Auburn/PhenotypicSelection/Juv_massSVL_general_distribution.png", plot_juv, ncol = 1, nrow = 1, base_height = 5, base_width = 10)
```

I need to standardize SVL values + integrate body condition values. I should do that by trip (because we're interesting in variation in phenotypic selection across years so we should have every distribution for each year.
```{r}
scale_this <- function(x){
  (x - mean(x, na.rm=TRUE)) / sd(x, na.rm=TRUE)
}
```

```{r}
lm4.2 <- nlme::gls(log(Mass)~log(SVL), na.action = na.omit, data = t4.2)
res4.2 <- as.data.frame(lm4.2$residuals)
t4.3<-cbind(t4.2, res4.2[match(rownames(t4.2), rownames(res4.2)), ])

t4.3 <- 
  t4.3 %>%
  group_by(Sex) %>%
  mutate(SVLst = scale_this(SVL),
         Massst = scale_this(Mass))
t4.3$sample = 1
t4.3 = t4.3[,c(2:7, 15, 8:9,  13, 12) ]

colnames(t4.3) = c("Y2012","Y2013","Y2014","Y2015","Y2016","Y2017", "sample","island", "sex", "SVL", "BC")
```

```{r}
lm7.2 <- nlme::gls(log(Mass)~log(SVL), na.action = na.omit, data = t7.2)
res7.2 <- as.data.frame(lm7.2$residuals)
t7.3<-cbind(t7.2, res7.2[match(rownames(t7.2), rownames(res7.2)), ])

t7.3 <- 
  t7.3 %>%
  group_by(Sex) %>%
  mutate(SVLst = scale_this(SVL),
         Massst = scale_this(Mass))
t7.3$sample = 1
t7.3 = t7.3[,c(2:7, 15, 8:9,  13, 12) ]

colnames(t7.3) = c("Y2012","Y2013","Y2014","Y2015","Y2016","Y2017", "sample","island", "sex", "SVL", "BC")
```


```{r}
lm10.2 <- nlme::gls(log(Mass)~log(SVL), na.action = na.omit, data = t10.2)
res10.2 <- as.data.frame(lm10.2$residuals)
t10.3<-cbind(t10.2, res10.2[match(rownames(t10.2), rownames(res10.2)), ])

t10.3 <- 
  t10.3 %>%
  group_by(Sex) %>%
  mutate(SVLst = scale_this(SVL),
         Massst = scale_this(Mass))
t10.3$sample = 1
t10.3 = t10.3[,c(2:7, 15, 8:9,  13, 12) ]

colnames(t10.3) = c("Y2012","Y2013","Y2014","Y2015","Y2016","Y2017", "sample","island", "sex", "SVL", "BC")
```

```{r}
lm14.2 <- nlme::gls(log(Mass)~log(SVL), na.action = na.omit, data = t14.2)
res14.2 <- as.data.frame(lm14.2$residuals)
t14.3<-cbind(t14.2, res14.2[match(rownames(t14.2), rownames(res14.2)), ])

t14.3 <- 
  t14.3 %>%
  group_by(Sex) %>%
  mutate(SVLst = scale_this(SVL),
         Massst = scale_this(Mass))
t14.3$sample = 1
t14.3 = t14.3[,c(2:7, 15, 8:9,  13, 12) ]

colnames(t14.3) = c("Y2012","Y2013","Y2014","Y2015","Y2016","Y2017", "sample","island", "sex", "SVL", "BC")
```

```{r}
lm18.2 <- nlme::gls(log(Mass)~log(SVL), na.action = na.omit, data = t18.2)
res18.2 <- as.data.frame(lm18.2$residuals)
t18.3<-cbind(t18.2, res18.2[match(rownames(t18.2), rownames(res18.2)), ])

t18.3 <- 
  t18.3 %>%
  group_by(Sex) %>%
  mutate(SVLst = scale_this(SVL),
         Massst = scale_this(Mass))
t18.3$sample = 1
t18.3 = t18.3[,c(2:7, 15, 8:9,  13, 12) ]

colnames(t18.3) = c("Y2012","Y2013","Y2014","Y2015","Y2016","Y2017", "sample","island", "sex", "SVL", "BC")
```


Pooling dataframes in one file
```{r}
juv3 = do.call("rbind", list(t4.3, t7.3, t10.3, t14.3, t18.3))
```

Before saving the file, I need to see how many individuals there are per trip and per island (some islands have not been monitored the whole time)

```{r}
table(juv2$Island, juv2$Y2012)
```
```{r}
table(juv2$Island, juv2$Y2013)
```
```{r}
table(juv2$Island, juv2$Y2014)
```

```{r}
table(juv2$Island, juv2$Y2015)
```

```{r}
table(juv2$Island, juv2$Y2016)
```

```{r}
table(juv2$Island, juv2$Y2017)
```
```{r}
table(t4.2$Island, t4.2$Y2012)
```

```{r}
table(t7.2$Island, t7.2$Y2013)
```

```{r}
table(t10.2$Island, t10.2$Y2014)
```

```{r}
table(t14.2$Island, t14.2$Y2015)
```

```{r}
table(t18.2$Island, t18.2$Y2016)
```

I need to divide the final file in two different files, since sampling on islands M4 and M5 stopped in 2015 and F3 in 2016
```{r}
juvLMN = juv3 %>%
  filter(island %in% c("M4", "M5", "F3")) %>%
  dplyr::select(c(1:3, 7:11))
juvFKH = juv3 %>%
  filter(island %in% c("F4", "F2", "M3"))
```

Renaming files to fit CMR format then saving files in .txt
```{r}
colnames(juvLMN) = c("H:Y2012", "H:Y2013", "H:Y2014", "S:sample", "$COV:island", "$COV:sex", "COV:SVL", "COV:BC")
colnames(juvFKH) = c("H:Y2012", "H:Y2013", "H:Y2014", "H:Y2015", "H:Y2016", "H:Y2017", "S:sample", "$COV:island", "$COV:sex", "COV:SVL", "COV:BC")
juvLMN = na.omit(juvLMN)
juvFKH = na.omit(juvFKH)
write.table(juvLMN, "~/Desktop/Auburn/PhenotypicSelection/juv_LMN.txt", sep = "\t", quote = FALSE, col.names = TRUE, row.names = FALSE) 
write.table(juvFKH, "~/Desktop/Auburn/PhenotypicSelectionjuv_FHK.txt", sep = "\t", quote = FALSE, col.names = TRUE, row.names = FALSE) 
```
