---
title: "DataVisualization"
author: "Aymeric Collart"
date: "`r Sys.Date()`"
output:
  pdf_document: default
  html_document: default
---

# 1. Prepare the environment

```{r}
load(file = "CHC9012_PCIbexDataPrepared.RData")
```

```{r}
library(ggplot2)
library(dplyr)
library(Rmisc)
```

# 2. Cleaning the dataset

```{r}
## 2.1 Select the participants with a human-like behavior
PartMeanRT <- aggregate(RT ~ ID_1_TimeReceived,
                        data = data3,
                        FUN = mean)

data_OtherPart <- subset(PartMeanRT, RT > 1000)
data_OtherPart_FullData <- droplevels(subset(data3,
                                             ID_1_TimeReceived %in% data_OtherPart$ID_1_TimeReceived))

plot(data_OtherPart_FullData$RT ~ data_OtherPart_FullData$ID_1_TimeReceived, outline = FALSE)
plot(data_OtherPart_FullData$Value ~ data_OtherPart_FullData$Condition)

## 2.2 Remove outlier trials

### 2.2.1 Absolute values

data_OtherPart_CleanAbsolute <- subset(data_OtherPart_FullData, RT > 200 & RT < 20000)

### 2.2.2 Relative values

# Calculate the mean per Participant and Condition
data_OtherPart_CleanAbsolute$m_rt <- ave(data_OtherPart_CleanAbsolute$RT, 
                                         data_OtherPart_CleanAbsolute$ID_1_TimeReceived, 
                                         data_OtherPart_CleanAbsolute$Condition, 
                                         FUN = mean)

# 2. Calculate the SD per Participant and Condition
data_OtherPart_CleanAbsolute$sd_rt <- ave(data_OtherPart_CleanAbsolute$RT, 
                                          data_OtherPart_CleanAbsolute$ID_1_TimeReceived, 
                                          data_OtherPart_CleanAbsolute$Condition, 
                                          FUN = sd)

# 3. Subset the data to remove outliers
data_OtherPart_CleanAbsoluteRelative <- subset(data_OtherPart_CleanAbsolute, 
                                               RT >= (m_rt - 2.5 * sd_rt) & 
                                                 RT <= (m_rt + 2.5 * sd_rt))

### 2.2.3 Calculate the number of trials removed

FullData_NbTrials <- nrow(data_OtherPart_FullData)
TrimmedData_NbTrials <- nrow(data_OtherPart_CleanAbsoluteRelative)

# Number of trimmed trials
NbTrimmedTrials <- FullData_NbTrials - TrimmedData_NbTrials
NbTrimmedTrials

# Proportion (%)
NbTrimmedTrials*100/FullData_NbTrials
```


# 3. Visualization of the clean results

## 3.1. Acceptability data

### 3.1.1 Fancy boxplots

```{r}
# Create the canvas
ggplot(data_OtherPart_CleanAbsoluteRelative)

# Include what to plot (the "aesthetics")
ggplot(data_OtherPart_CleanAbsoluteRelative,
       aes(x = Condition,
           y = Value))

# Include how to plot
ggplot(data_OtherPart_CleanAbsoluteRelative,
       aes(x = Condition,
           y = Value)) + 
  geom_boxplot()

# Add colors
ggplot(data_OtherPart_CleanAbsoluteRelative,
       aes(x = Condition,
           y = Value,
           fill = Condition)) + 
  geom_boxplot(fill = "skyblue")

# Add title and legend, change the theme
ggplot(data_OtherPart_CleanAbsoluteRelative,
       aes(x = Condition,
           y = Value,
           fill = Condition)) + 
  geom_boxplot(fill = "skyblue",
               width = 0.5) +
  labs(title = "Acceptability judgments by Grammaticality",
       x = "",
       y = "Rating (1-7 Likert Scale)") +
  theme_minimal(base_size = 14) +
  theme(legend.position = "none") 


# Control the y axis
ggplot(data_OtherPart_CleanAbsoluteRelative,
       aes(x = Condition,
           y = Value,
           fill = Condition)) + 
  geom_boxplot(fill = "skyblue",
               width = 0.5) +
  labs(title = "Acceptability judgments by Grammaticality",
       x = "",
       y = "Rating (1-7 Likert Scale)") +
  theme_minimal(base_size = 14) +
  theme(legend.position = "none") +
scale_y_continuous(limits = c(0.5, 7.5),
    breaks = 1:7)

```

### 3.1.2 Bar plots

```{r}
JudgSummary <- summarySE(data_OtherPart_CleanAbsoluteRelative,
                         measurevar = "Value",
                         groupvars = "Condition")
```

```{r}
# Create the canvas
ggplot(JudgSummary)

# Include what to plot (the "aesthetics")
ggplot(JudgSummary,
       aes(x = Condition,
           y = Value,
           fill = Condition))

# Include how to plot
ggplot(JudgSummary,
       aes(x = Condition,
           y = Value,
           fill = Condition)) +
  geom_col(width = 0.5, alpha = 0.9)

# Include error bars
ggplot(JudgSummary,
       aes(x = Condition,
           y = Value,
           fill = Condition)) +
  geom_col(width = 0.5, alpha = 0.9) +
  geom_errorbar(aes(ymin = Value - ci,
                    ymax = Value + ci), 
                width = 0.15,
                size = 0.7) 

# Include text
ggplot(JudgSummary,
       aes(x = Condition,
           y = Value,
           fill = Condition)) +
  geom_col(width = 0.5, alpha = 0.9) +
  geom_errorbar(aes(ymin = Value - ci,
                    ymax = Value + ci), 
                width = 0.15,
                size = 0.7) +
  geom_text(aes(label = round(Value, 2)), 
            vjust = 4,
            color = "white",
            size = 5,
            fontface = "bold")

# Control the y axis
ggplot(JudgSummary,
       aes(x = Condition,
           y = Value,
           fill = Condition)) +
  geom_col(width = 0.5, alpha = 0.9) +
  geom_errorbar(aes(ymin = Value - ci,
                    ymax = Value + ci), 
                width = 0.15,
                size = 0.7) +
  geom_text(aes(label = round(Value, 2)), 
            vjust = 4,
            color = "white",
            size = 5,
            fontface = "bold") +
  scale_y_continuous(limits = c(1, 7),
                     breaks = 1:7,
                     oob = scales::rescale_none)
```

```{r}
# Add information about individual items

JudgSummaryItem <- summarySE(data_OtherPart_CleanAbsoluteRelative,
                         measurevar = "Value",
                         groupvars = c("Condition", "ItemNo"))

ggplot(JudgSummary,
       aes(x = Condition,
           y = Value,
           fill = Condition)) +
  geom_col(width = 0.5, alpha = 0.9) +
  geom_errorbar(aes(ymin = Value - ci,
                    ymax = Value + ci), 
                width = 0.15,
                size = 0.7) +
  geom_text(aes(label = round(Value, 2)), 
            vjust = 4,
            color = "white",
            size = 5,
            fontface = "bold") +
  scale_y_continuous(limits = c(1, 7),
                     breaks = 1:7,
                     oob = scales::rescale_none) +
  geom_jitter(data = JudgSummaryItem,
              aes(color = Condition),
              width = 0.2,
              height = 0.1,
              alpha = 0.3,
              size = 2.5)


# Change the *shade* of the columns, the *color* of the text, and the theme to "minimal"
ggplot(JudgSummary,
       aes(x = Condition,
           y = Value,
           fill = Condition)) +
  geom_col(width = 0.5, alpha = 0.3) +
  geom_errorbar(aes(ymin = Value - ci,
                    ymax = Value + ci), 
                width = 0.15,
                size = 0.7) +
  geom_text(aes(label = round(Value, 2)), 
            vjust = 4,
            color = "black",
            size = 5,
            fontface = "bold") +
  scale_y_continuous(limits = c(1, 7),
                     breaks = 1:7,
                     oob = scales::rescale_none) +
  geom_jitter(data = JudgSummaryItem,
              aes(color = Condition),
              width = 0.2,
              height = 0.1,
              alpha = 0.5,
              size = 2.5) +
  theme_minimal(base_size = 14)
```


## 3.1 Visualizing the background information

## 3.2 Reaction time

### 3.2.1 Fancy boxplots

```{r}
ggplot(data_OtherPart_CleanAbsoluteRelative,
       aes(x = Condition,
           y = RT,
           fill = Condition)) + 
  geom_boxplot(fill = "skyblue",
               width = 0.5) +
  labs(title = "RT by Grammaticality",
       x = "",
       y = "Reaction time (in millisecond)") +
  theme_minimal(base_size = 14) +
  theme(legend.position = "none") +
scale_y_continuous(limits = c(0, 20000),
    breaks = seq(0, 20000, by = 4000))
```

### 3.2.2 Bar plots

```{r}
RTSummary <- summarySE(data_OtherPart_CleanAbsoluteRelative,
                       measurevar = "RT",
                       groupvars = "Condition")
```

```{r}
ggplot(RTSummary,
       aes(x = Condition,
           y = RT,
           fill = Condition)) +
  geom_col(width = 0.5, alpha = 0.9) +
  geom_errorbar(aes(ymin = RT - ci,
                    ymax = RT + ci), 
                width = 0.15,
                size = 0.7) +
  geom_text(aes(label = round(RT)), 
            vjust = 8,
            color = "white",
            size = 5,
            fontface = "bold") +
  scale_y_continuous(limits = c(4000, 7000),
                     breaks = seq(4000, 7000, by = 1000),
                     oob = scales::rescale_none)
```

```{r}
# Add information about individual items
RTSummaryItem <- summarySE(data_OtherPart_CleanAbsoluteRelative,
                         measurevar = "RT",
                         groupvars = c("Condition", "ItemNo"))

# Plot the figure
ggplot(RTSummary,
       aes(x = Condition,
           y = RT,
           fill = Condition)) +
  geom_col(width = 0.5, alpha = 0.9) +
  geom_errorbar(aes(ymin = RT - ci,
                    ymax = RT + ci), 
                width = 0.15,
                size = 0.7) +
  geom_text(aes(label = round(RT)), 
            vjust = 8,
            color = "white",
            size = 5,
            fontface = "bold") +
  geom_jitter(data = RTSummaryItem,
              aes(color = Condition),
              width = 0.2,
              height = 0.1,
              alpha = 0.3,
              size = 2.5)

# Change the *shade* of the columns, the *color* of the text, and the theme to "minimal"
ggplot(RTSummary,
       aes(x = Condition,
           y = RT,
           fill = Condition)) +
  geom_col(width = 0.5, alpha = 0.3) +
  geom_errorbar(aes(ymin = RT - ci,
                    ymax = RT + ci), 
                width = 0.15,
                size = 0.7) +
  geom_text(aes(label = round(RT)), 
            vjust = 8,
            color = "black",
            size = 5,
            fontface = "bold") +
  scale_y_continuous(limits = c(3000, 9000),
                     breaks = seq(3000, 9000, by = 2000),
                     oob = scales::rescale_none) +
  geom_jitter(data = RTSummaryItem,
              aes(color = Condition),
              width = 0.2,
              height = 0.1,
              alpha = 0.5,
              size = 2.5) +
  theme_minimal(base_size = 14)
```

## 3.3 Look at the background information

```{r}
# Summarize the background information by participant
ParticipantBackground <- data_OtherPart_CleanAbsoluteRelative %>%
  distinct(ID_1_TimeReceived, .keep_all = TRUE) %>%
  select(ID_1_TimeReceived, UserAge, UserDegree)

ParticipantBackground$UserDegree <- factor(ParticipantBackground$UserDegree, 
  levels = c("High school", "BA", "MA", "Ph.D.") )
```

```{r}
# Plot the age of the participants
ggplot(ParticipantBackground, aes(x = "", y = UserAge)) +
  geom_boxplot(
    width = 0.4, 
    fill = "#4a90e2",
    color = "black",
    alpha = 0.7
  ) +
  theme_classic(base_size = 14) +
  labs(x = "",
       y = "Age") +
  theme(legend.position = "none")
```

```{r}
# Plot the degree of the participants
ggplot(ParticipantBackground,
       aes(x = UserDegree,
           fill = UserDegree)) +
  geom_bar(width = 0.5,
           color = "black",
           alpha = 0.8,
           show.legend = FALSE) +
  geom_text(stat = "count",
            aes(label = after_stat(count)),
            vjust = -0.5,
            size = 5) +
  labs(x = "",
       y = "Number of participants") +
  scale_fill_brewer(palette = "Pastel1") +
  theme_classic(base_size = 14) +
  scale_y_continuous(expand = expansion(mult = c(0, 0.15)))
```

```{r}
# Plot two types of information at the same time
ggplot(ParticipantBackground,
       aes(x = UserDegree,
           y = UserAge,
           fill = UserDegree)) +
  geom_jitter(width = 0.1,
              height = 0,
              alpha = 0.5,
              size = 2.5,
              color = "dimgray") +
  geom_boxplot(width = 0.4,
               alpha = 0.7,
               outlier.shape = NA) +
  labs(x = "Highest degree",
       y = "Age") +
  scale_fill_brewer(palette = "Pastel1") +
  theme_classic(base_size = 14) +
  theme(legend.position = "none")
```