---
title: "Eye_Tracking"
author: "AZ"
date: "2022-08-31"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r Libraries, warning = FALSE}

library(ggplot2)
library(dplyr)
library(afex)
library(emmeans)
library(ggpubr)

```

```{r import data, include=FALSE}

#Define path
Path = "/Volumes/data-1/Data2/MRI/fMRI/projects/marmoset_awake_bruker/AZ/Marmosets_Mirror/Eye-Tracking Analysis"

DataEye = read.csv2(paste0(Path, "/Tracking_Data.csv"), 
                    sep = ",", na.strings = "", col.names = c("Condition", "Epoch", "Monkey", "Window", "Proportion"))

DataEye = DataEye %>% 
  mutate(Condition = as.factor(Condition),
         Epoch = as.factor(Epoch),
         Monkey = as.factor(Monkey),
         Window = as.factor(Window),
         Proportion = as.numeric(Proportion),
         Proportion = ifelse(is.na(Proportion), 0, Proportion))

levels(DataEye$Condition) = c("Empty Hand", "Grasping Hand")
levels(DataEye$Epoch) = c("Post-Hand", "Pre-Hand", "Reaching", "Withdrawing")

DataEye$Epoch = ordered(DataEye$Epoch, levels = c("Pre-Hand", "Reaching", "Withdrawing", "Post-Hand"))
```

```{r Common theme for the plot, include = TRUE}

#Graph common theme
commonTheme= list(theme_bw(), theme(axis.line = element_line(colour = "black", linewidth = 1),
                                    panel.grid.major = element_blank(),
                                    panel.grid.minor = element_blank(),
                                    panel.border = element_blank(),
                                    panel.background = element_blank(),
                                    axis.ticks.x = element_blank(),
                                    legend.title = element_blank(),
                                    legend.position = "right",
                                    plot.title = element_text(size = 16, face = "bold", hjust = 0.55),
                                    plot.title.position = "plot",
                                    axis.title.x = element_text(size = 14, face = "bold"),
                                    axis.title.y = element_text(size=14, face = "bold"),
                                    axis.text.x = element_text(size=12),
                                    axis.text.y = element_text(size=12)))
                                    
pd = position_dodge(width = 0.75)

#Palette
my_colors <- c("#ABDDA4", "#E6F598", "#FEE08B", "orange", "firebrick", "red")
```

```{r Tracking Analysis, include = T, warning = F, echo = F}

#Analysis regarding the eye-tracking focused on the hand
Tracking_Hand = DataEye[DataEye$Window == "hand",]

Tracking_Hand_ANOVA = aov_ez("Monkey", "Proportion", Tracking_Hand, 
                    within = c("Condition", "Epoch"), anova_table = list(es ="pes"))
knitr::kable(nice(Tracking_Hand_ANOVA, intercept = T))

emmeans(Tracking_Hand_ANOVA, pairwise ~ Epoch, model = "multivariate", adjust = "bonferroni") 
emmeans(Tracking_Hand_ANOVA, pairwise ~ Condition | Epoch, model = "multivariate", adjust = "bonferroni")
emmeans(Tracking_Hand_ANOVA, pairwise ~ Epoch | Condition, model = "multivariate", adjust = "bonferroni") 

#Analysis regarding the eye-tracking focused on the target
Tracking_Target = DataEye[DataEye$Window == "food",]

Tracking_Target_ANOVA = aov_ez("Monkey", "Proportion", Tracking_Target, 
                    within = c("Condition", "Epoch"), anova_table = list(es ="pes"))
knitr::kable(nice(Tracking_Target_ANOVA, intercept = T))

emmeans(Tracking_Target_ANOVA, pairwise ~ Epoch, model = "multivariate", adjust = "bonferroni") 
emmeans(Tracking_Target_ANOVA, pairwise ~ Condition | Epoch, model = "multivariate", adjust = "bonferroni")
emmeans(Tracking_Target_ANOVA, pairwise ~ Epoch | Condition, model = "multivariate", adjust = "bonferroni") 

```

```{r Tracking Plot Hand, include = T, warning = F, echo = F}

Hand_Plot = Tracking_Hand %>% 
  ggplot(aes(y = Proportion, x = Epoch, fill = Condition)) +
  geom_boxplot() +
  geom_point(position = pd) +
  scale_y_continuous(limits = c(0, 0.30)) +
  scale_fill_manual(values = my_colors[4:5]) +
  stat_summary(fun.y = mean, geom = "point", shape = 23, size = 4, position = pd) +
  labs(y = "Proportion Eyes Open", x = "Epoch", title = "Eyes on Hand") +
  geom_segment(x = 2.75, y = 0.25, xend = 3.25, yend = 0.25, colour = "black", size = 1.5 ) +
  geom_text(x = 3, y = 0.26, label = "*", size = 6) +
  commonTheme

```

```{r Tracking Plot Target, include = T, warning = F, echo = F}

Target_Plot = Tracking_Target %>% 
  ggplot(aes(y = Proportion, x = Epoch, fill = Condition)) +
  geom_boxplot() +
  geom_point(position = pd) +
  scale_y_continuous(limits = c(0, 0.30)) +
  scale_fill_manual(values = my_colors[4:5]) +
  stat_summary(fun.y = mean, geom = "point", shape = 23, size = 4, position = pd) +
  labs(y = "Proportion Eyes Open", x = "Epoch", title = "Eyes on Target") +
  geom_segment(x = 1.75, y = 0.29, xend = 2.25, yend = 0.29, colour = "black", size = 1.5 ) +
  geom_text(x = 2, y = 0.30, label = "*", size = 6) +
  commonTheme

```

```{r Combined Graph, include=TRUE, fig.height= 5, fig.width= 9, eval=TRUE}

GraphsI = ggarrange(Hand_Plot, Target_Plot + rremove("ylab"),
                    ncol = 2, common.legend = T, legend = "bottom")


print(GraphsI)

#Save the plots as JPEG images
ggsave(filename = paste0(Path, "/Tracking_Plots.jpeg"), plot = GraphsI, width = 24, height = 12, units = "cm")

```