---
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(readxl)
library(Rmisc)
library(dplyr)
library(ggplot2)

```

```{r import data, include=FALSE}
#Define the path
Path = "/Volumes/data-1/Data2/MRI/fMRI/projects/marmoset_awake_bruker/AZ/Marmosets_Mirror/Eye-Tracking Analysis"
  
DataEye = read_xlsx(paste0(Path, "/Amplitude_and_Frequency_Data.xlsx"))

```

```{r Plot common theme, include = F, echo = F, warning = F}

#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 Saccade Count, include = T, echo = T, warning = F}

#Divide the total amount of saccades per condition by the total duration of the condition,
#in this case 216 seconds, to obtain the number of saccades per second (frequency)
Count_Summary = DataEye %>% 
  mutate(Saccade_Counts = Saccade_Counts/216) 

#Wilcoxon test to compare the median of the two conditions
wilcox.test(Count_Summary$Saccade_Counts[Count_Summary$Condition == "Empty Hand"], 
            Count_Summary$Saccade_Counts[Count_Summary$Condition == "Grasping Hand"], 
            paired=TRUE) 

#Average the saccades frequency across monkeys
Count_Summary = Count_Summary %>% 
  summarySEwithin(measurevar = "Saccade_Counts", withinvars = ("Condition"))

#Plot the saccades frequency
Saccades_Count = Count_Summary %>% 
  ggplot(aes(y = Saccade_Counts, x = Condition, fill = Condition)) +
  geom_col(width = 0.6, color = "black") +
  geom_errorbar(aes(ymin = Saccade_Counts-se, ymax=Saccade_Counts+se), 
                size = 0.7, width = 0.2, color='black') +
  scale_fill_manual(values = my_colors[4:5]) +
  labs(y = "Saccades/second", title = "Saccades Frequency") +
  commonTheme

```


```{r Saccade_Amplitude, include = TRUE}

#Create the dataframe for amplitude analysis
Ampli_Summary = DataEye 

#Wilcoxon test to compare the median of the two conditions
wilcox.test(Ampli_Summary$Saccade_Amplitude[Ampli_Summary$Condition == "Empty Hand"], 
            Ampli_Summary$Saccade_Amplitude[Ampli_Summary$Condition == "Grasping Hand"], 
            paired=TRUE) 

#Average the saccades frequency across monkeys
Ampli_Summary = Ampli_Summary %>% 
  summarySEwithin(measurevar = "Saccade_Amplitude", withinvars = ("Condition"))

#Plot the saccades amplitude
Saccades_Ampli = Ampli_Summary %>% 
  ggplot(aes(y=Saccade_Amplitude, x=Condition, fill=Condition)) +
  geom_col(width = 0.6, color = "black") +
  geom_errorbar(aes(ymin=Saccade_Amplitude-se, ymax=Saccade_Amplitude+se), 
                size=0.7, width=0.2, color='black') +
  scale_fill_manual(values = my_colors[4:5]) +
  labs(y = "Visual degrees", title = "Saccades Amplitude") +
  commonTheme

```

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

GraphsI = ggarrange(Saccades_Count, Saccades_Ampli,
                    ncol = 2, common.legend = T, legend = "bottom")


print(GraphsI)

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

```