1 Preparation

library(tidyverse)
library(RColorBrewer)
source(file.path("R", "substr_Event.R"))

# plot setting
ymax <- 8.3
ymin <- -6

1.1 Load and clean the data

df.binerp <- read_csv(file.path("data", "E205_BinERP_AllResp_Avg.csv"))

df.binerp.clean <- {
  df.binerp %>% 
    gather(TimePoint, amplitude, -c(Component, Hemisphere, Event)) %>% 
    substr_Event() %>% 
    mutate(Hemisphere = tolower(Hemisphere), 
           TimePoint = as.numeric(TimePoint))
}

str(df.binerp.clean)
## tibble[,8] [9,600 × 8] (S3: tbl_df/tbl/data.frame)
##  $ Component : chr [1:9600] "N170" "N170" "N170" "N170" ...
##  $ Hemisphere: chr [1:9600] "left" "left" "left" "left" ...
##  $ Event     : chr [1:9600] "NF2+" "NF7+" "NH2+" "NH7+" ...
##  $ TimePoint : num [1:9600] -200 -200 -200 -200 -200 -200 -200 -200 -200 -200 ...
##  $ amplitude : num [1:9600] 0.18237 0.23342 0.21936 0.31294 0.00557 ...
##  $ Type      : Factor w/ 2 levels "intact","scrambled": 1 1 1 1 2 2 2 2 1 1 ...
##  $ Category  : Factor w/ 2 levels "face","house": 1 1 2 2 1 1 2 2 1 1 ...
##  $ Duration  : Factor w/ 2 levels "17","200": 2 1 2 1 2 1 2 1 2 1 ...

2 ERP plots for all

2.1 Load and clean the data

df.binerp <- read_csv(file.path("data", "E205_BinERP_AllResp_Avg.csv"))

df.binerp.clean <- {
  df.binerp %>% 
    gather(TimePoint, amplitude, -c(Component, Hemisphere, Event)) %>% 
    substr_Event() %>% 
    mutate(Hemisphere = tolower(Hemisphere), 
           TimePoint = as.numeric(TimePoint))
}

str(df.binerp.clean)
## tibble[,8] [9,600 × 8] (S3: tbl_df/tbl/data.frame)
##  $ Component : chr [1:9600] "N170" "N170" "N170" "N170" ...
##  $ Hemisphere: chr [1:9600] "left" "left" "left" "left" ...
##  $ Event     : chr [1:9600] "NF2+" "NF7+" "NH2+" "NH7+" ...
##  $ TimePoint : num [1:9600] -200 -200 -200 -200 -200 -200 -200 -200 -200 -200 ...
##  $ amplitude : num [1:9600] 0.18237 0.23342 0.21936 0.31294 0.00557 ...
##  $ Type      : Factor w/ 2 levels "intact","scrambled": 1 1 1 1 2 2 2 2 1 1 ...
##  $ Category  : Factor w/ 2 levels "face","house": 1 1 2 2 1 1 2 2 1 1 ...
##  $ Duration  : Factor w/ 2 levels "17","200": 2 1 2 1 2 1 2 1 2 1 ...
df.binerp.N170 <- filter(df.binerp.clean, Component == "N170")

2.2 Plots

N170.Plot.205 <- {
  ggplot(data = df.binerp.N170, aes(y = amplitude, x = TimePoint, color = Duration, linetype = Category)) + # set the data, varialbes for x and y axises, and the variable for dividing data
    geom_line(size = .9) +  # plot the lines
    scale_linetype_manual(values=c("solid", "dashed")) +
    scale_color_manual(values=c("#F8766D", "#C77CFF")) +
    facet_grid(Type + Hemisphere ~ ., switch = "x") +  # show the left and right hemisphere separately
    theme_classic(base_size = 22) +  # set the theme as classical  
    scale_y_reverse(limits = c(ymax, ymin), breaks = seq(-6, 6, 3)) +  # remove the space between columns and x axis
    coord_cartesian(xlim = c(-200, 500)) +  # the limit for y axis 
    scale_x_continuous(breaks = seq(-200, 500, by = 100)) +  # the marks for x axis
    labs(title = "", 
         x = "Time points (ms)", 
         y = expression(paste("Amplitude (", mu, "V)")), 
         color = "Duration", 
         linetype = "Category") +  # set the names for main, x and y axises   Bin ERP Plots for the N170 (E1)
    # theme(plot.title = element_text(size = 22), axis.title = element_text(size = 20), axis.text = element_text(size = 20)) + # the size of the texts in plot   
    # theme(legend.title=element_blank()) +  # remove legend title
    theme(strip.text = element_text(face="bold", size=22, lineheight=5.0), 
          strip.background = element_rect(fill="white", colour="white", size=1), 
          strip.placement = "outside", 
          legend.position = "bottom", 
          legend.title = element_text(size = 18), 
          legend.margin=margin(0,0,0,0),
          legend.box.margin=margin(-10,-10,-5,-10),
          legend.key.width = unit(.9, "cm")
          ) +
    geom_hline(yintercept = 0, linetype = 5, alpha = 0.5) +  # add line for y=0
    geom_vline(xintercept = 0, linetype = 5, alpha = 0.5) +  # add line for x=0
    geom_rect(aes(xmin = 96 - 0.1, xmax = 136 + 0.1, ymin = ymin + 0.05, ymax = ymax - 0.05),
              fill = "transparent", color = "grey70", size = 1, linetype = "dashed", show.legend= FALSE) + # add the time window for N170
    geom_rect(aes(xmin = 152 - 0.1, xmax = 192 + 0.1, ymin = ymin + 0.05, ymax = ymax - 0.05),
              fill = "transparent", color = "grey70", size = 1, show.legend= FALSE) # add the time window for N170
}

N170.Plot.205 

# tmpfn <- "E205_ERP_plots"
# res_value = 350
# res_rela = 90
# w = 800/res_rela*res_value
# h = 1076/res_rela*res_value
# 
# png(file= paste0(tmpfn, ".png") ,width=w,height=h, res = res_value)
# 
# #plot
# N170.Plot.205
# 
# dev.off()
duration_color <- c(brewer.pal(9, 'Blues')[8], brewer.pal(12, "Paired")[6]) 

# arrange facet to one row
N170_Plot_205 <- {
  ggplot(data = df.binerp.N170, aes(y = amplitude, x = TimePoint, color = Duration, linetype = Category)) + # set the data, varialbes for x and y axises, and the variable for dividing data
    geom_line(size = .9) +  # plot the lines
    scale_linetype_manual(values=c("solid", "dashed")) +
    scale_color_manual(values=duration_color) +
    facet_wrap(~ Hemisphere + Type, nrow = 1, labeller = label_wrap_gen(multi_line=FALSE)) +  # show the left and right hemisphere separately
    theme_classic(base_size = 20) +  # set the theme as classical  
    scale_y_reverse(limits = c(ymax, ymin), breaks = seq(-6, 6, 3)) +  # remove the space between columns and x axis
    coord_cartesian(xlim = c(-200, 500)) +  # the limit for y axis 
    scale_x_continuous(breaks = seq(-200, 500, by = 100)) +  # the marks for x axis
    labs(x = "Time points (ms)", 
         y = expression(paste("Amplitudes (", mu, "V)")), 
         color = "Duration", 
         linetype = "Category") +  # set the names for main, x and y axises   Bin ERP Plots for the N170 (E1)
    # theme(plot.title = element_text(size = 22), axis.title = element_text(size = 20), axis.text = element_text(size = 20)) + # the size of the texts in plot   
    # theme(legend.title=element_blank()) +  # remove legend title
    theme(strip.text = element_text(size=22, lineheight=5.0), 
          strip.background = element_rect(fill="white", colour="white", size=1), 
          strip.placement = "outside", 
          # legend.position = "bottom", 
          # legend.title = element_text(size = 20), 
          legend.margin=margin(0,0,0,0),
          legend.box.margin=margin(-10,-10,-5,-10),
          # legend.text=element_text(size=19),
          legend.key.width = unit(.9, "cm"),
          legend.position = c(.49, .8),
          legend.title=element_text(size=14), 
          legend.text=element_text(size=14),
          legend.spacing.y = unit(-0.005, "cm")
          ) +
    guides(linetype = guide_legend(title.position = "left", nrow = 1),
           color = guide_legend(title.position = "left", nrow = 1)) +
    geom_hline(yintercept = 0, linetype = 5, alpha = 0.5) +  # add line for y=0
    geom_vline(xintercept = 0, linetype = 5, alpha = 0.5) +  # add line for x=0
    geom_rect(aes(xmin = 96 - 0.1, xmax = 136 + 0.1, ymin = ymin + 0.05, ymax = ymax - 0.05),
              fill = "transparent", color = "grey70", size = 1, linetype = "dashed", show.legend= FALSE) + # add the time window for N170
    geom_rect(aes(xmin = 152 - 0.1, xmax = 192 + 0.1, ymin = ymin + 0.05, ymax = ymax - 0.05),
              fill = "transparent", color = "grey70", size = 1, show.legend= FALSE) # add the time window for N170
}

# ggsave('N170_Plot_205.pdf', N170_Plot_205, width = 22, height = 3.5)

N170_Plot_205 

duration_color <- c(brewer.pal(9, 'Blues')[8], brewer.pal(12, "Paired")[6]) 

# arrange facet to one row
N170_Plot_205 <- {
  ggplot(data = df.binerp.N170, aes(y = amplitude, x = TimePoint, color = Duration, linetype = Category)) + # set the data, varialbes for x and y axises, and the variable for dividing data
    geom_line(size = .9) +  # plot the lines
    scale_linetype_manual(values=c("solid", "dashed")) +
    scale_color_manual(values=duration_color) +
    facet_wrap(~ Hemisphere + Type, nrow = 1, labeller = label_wrap_gen(multi_line=FALSE)) +  # show the left and right hemisphere separately
    theme_classic(base_size = 20) +  # set the theme as classical  
    scale_y_reverse(limits = c(ymax, ymin), breaks = seq(-6, 6, 3)) +  # remove the space between columns and x axis
    coord_cartesian(xlim = c(-200, 300)) +  # the limit for y axis 
    scale_x_continuous(breaks = seq(-200, 300, by = 100)) +  # the marks for x axis
    labs(x = "Time points (ms)", 
         y = expression(paste("Amplitudes (", mu, "V)")), 
         color = "Duration", 
         linetype = "Category") +  # set the names for main, x and y axises   Bin ERP Plots for the N170 (E1)
    # theme(plot.title = element_text(size = 22), axis.title = element_text(size = 20), axis.text = element_text(size = 20)) + # the size of the texts in plot   
    # theme(legend.title=element_blank()) +  # remove legend title
    theme(strip.text = element_text(size=22, lineheight=5.0), 
          strip.background = element_rect(fill="white", colour="white", size=1), 
          strip.placement = "outside", 
          # legend.position = "bottom", 
          # legend.title = element_text(size = 20), 
          legend.margin=margin(0,0,0,0),
          legend.box.margin=margin(-10,-10,-5,-10),
          # legend.text=element_text(size=19),
          legend.key.width = unit(.9, "cm"),
          legend.position = c(.52, .88),
          legend.title=element_text(size=14), 
          legend.text=element_text(size=14),
          legend.spacing.y = unit(-0.005, "cm")
          ) +
    guides(linetype = guide_legend(title.position = "left", nrow = 1),
           color = guide_legend(title.position = "left", nrow = 1)) +
    geom_hline(yintercept = 0, linetype = 5, alpha = 0.5) +  # add line for y=0
    geom_vline(xintercept = 0, linetype = 5, alpha = 0.5) +  # add line for x=0
    geom_rect(aes(xmin = 96 - 0.1, xmax = 136 + 0.1, ymin = ymin + 0.05, ymax = ymax - 0.05),
              fill = "transparent", color = "grey70", size = 1, linetype = "dashed", show.legend= FALSE) + # add the time window for N170
    geom_rect(aes(xmin = 152 - 0.1, xmax = 192 + 0.1, ymin = ymin + 0.05, ymax = ymax - 0.05),
              fill = "transparent", color = "grey70", size = 1, show.legend= FALSE) # add the time window for N170
}

# ggsave('N170_Plot_205.pdf', N170_Plot_205, width = 22, height = 3.5)

N170_Plot_205 

# arrange facet to one row
N170_Plot_205_bw <- {
  ggplot(data = df.binerp.N170, aes(y = amplitude, x = TimePoint, color = Duration, linetype = Category)) + # set the data, varialbes for x and y axises, and the variable for dividing data
    geom_line(size = .9) +  # plot the lines
    scale_linetype_manual(values=c("solid", "dashed")) +
    scale_color_manual(values=c("gray70", "black")) +
    facet_wrap(~ Hemisphere + Type, nrow = 1, labeller = label_wrap_gen(multi_line=FALSE)) +  # show the left and right hemisphere separately
    theme_classic(base_size = 20) +  # set the theme as classical  
    scale_y_reverse(limits = c(ymax, ymin), breaks = seq(-6, 6, 3)) +  # remove the space between columns and x axis
    coord_cartesian(xlim = c(-200, 300)) +  # the limit for y axis 
    scale_x_continuous(breaks = seq(-200, 300, by = 100)) +  # the marks for x axis
    labs(x = "Time points (ms)", 
         y = expression(paste("Amplitudes (", mu, "V)")), 
         color = "Duration", 
         linetype = "Category") +  # set the names for main, x and y axises   Bin ERP Plots for the N170 (E1)
    # theme(plot.title = element_text(size = 22), axis.title = element_text(size = 20), axis.text = element_text(size = 20)) + # the size of the texts in plot   
    # theme(legend.title=element_blank()) +  # remove legend title
    theme(strip.text = element_text(size=22, lineheight=5.0), 
          strip.background = element_rect(fill="white", colour="white", size=1), 
          strip.placement = "outside", 
          # legend.position = "bottom", 
          # legend.title = element_text(size = 20), 
          legend.margin=margin(0,0,0,0),
          legend.box.margin=margin(-10,-10,-5,-10),
          # legend.text=element_text(size=19),
          legend.key.width = unit(.9, "cm"),
          legend.position = c(.52, .88),
          legend.title=element_text(size=14), 
          legend.text=element_text(size=14),
          legend.spacing.y = unit(-0.005, "cm")
          ) +
    guides(linetype = guide_legend(title.position = "left", nrow = 1),
           color = guide_legend(title.position = "left", nrow = 1)) +
    geom_hline(yintercept = 0, linetype = 5, alpha = 0.5) +  # add line for y=0
    geom_vline(xintercept = 0, linetype = 5, alpha = 0.5) +  # add line for x=0
    geom_rect(aes(xmin = 96 - 0.1, xmax = 136 + 0.1, ymin = ymin + 0.05, ymax = ymax - 0.05),
              fill = "transparent", color = "grey70", size = 1, linetype = "dashed", show.legend= FALSE) + # add the time window for N170
    geom_rect(aes(xmin = 152 - 0.1, xmax = 192 + 0.1, ymin = ymin + 0.05, ymax = ymax - 0.05),
              fill = "transparent", color = "grey70", size = 1, show.legend= FALSE) # add the time window for N170
}

# ggsave('N170_Plot_205_bw.pdf', N170_Plot_205_bw, width = 22, height = 3.5)

N170_Plot_205_bw 

# arrange facet to one row
N170_Plot_205_cc <- {
  ggplot(data = df.binerp.N170, aes(y = amplitude, x = TimePoint, color = Duration, linetype = Category)) + # set the data, varialbes for x and y axises, and the variable for dividing data
    geom_line(size = .9) +  # plot the lines
    scale_linetype_manual(values=c("solid", "dashed")) +
    scale_color_manual(values=c("gray70", "black")) +
    facet_grid(Type~ Hemisphere) +  # show the left and right hemisphere separately
    theme_classic(base_size = 20) +  # set the theme as classical  
    scale_y_reverse(limits = c(ymax, ymin), breaks = seq(-6, 6, 3)) +  # remove the space between columns and x axis
    coord_cartesian(xlim = c(-200, 300)) +  # the limit for y axis 
    scale_x_continuous(breaks = seq(-200, 300, by = 100)) +  # the marks for x axis
    labs(x = "Time points (ms)", 
         y = expression(paste("Amplitudes (", mu, "V)")), 
         color = "Duration", 
         linetype = "Category") +  # set the names for main, x and y axises   Bin ERP Plots for the N170 (E1)
    # theme(plot.title = element_text(size = 22), axis.title = element_text(size = 20), axis.text = element_text(size = 20)) + # the size of the texts in plot   
    # theme(legend.title=element_blank()) +  # remove legend title
    theme(strip.text = element_text(size=22, lineheight=5.0), 
          strip.background = element_rect(fill="white", colour="white", size=1), 
          strip.placement = "outside", 
          # legend.position = "bottom", 
          # legend.title = element_text(size = 20), 
          legend.margin=margin(0,0,0,0),
          legend.box.margin=margin(-10,-10,-5,-10),
          # legend.text=element_text(size=19),
          legend.key.width = unit(.9, "cm"),
          legend.position = c(.55, .45),
          legend.title=element_text(size=14), 
          legend.text=element_text(size=14),
          legend.spacing.y = unit(-0.005, "cm")
          ) +
    guides(linetype = guide_legend(title.position = "left", nrow = 1),
           color = guide_legend(title.position = "left", nrow = 1)) +
    geom_hline(yintercept = 0, linetype = 5, alpha = 0.5) +  # add line for y=0
    geom_vline(xintercept = 0, linetype = 5, alpha = 0.5) +  # add line for x=0
    geom_rect(aes(xmin = 96 - 0.1, xmax = 136 + 0.1, ymin = ymin + 0.05, ymax = ymax - 0.05),
              fill = "transparent", color = "grey70", size = 1, linetype = "dashed", show.legend= FALSE) + # add the time window for N170
    geom_rect(aes(xmin = 152 - 0.1, xmax = 192 + 0.1, ymin = ymin + 0.05, ymax = ymax - 0.05),
              fill = "transparent", color = "grey70", size = 1, show.legend= FALSE) # add the time window for N170
}

# ggsave('N170_Plot_205_cc.pdf', N170_Plot_205_cc, width = 11, height = 5)

N170_Plot_205_cc 

3 ERP plots for intact stim with confidence

3.1 Load and clean the data

df.binerp.con <- read_csv(file.path("data", "E205_BinERP_Resp_Avg.csv"))

df.con.clean <- {
  df.binerp.con %>% 
    substr_Event() %>% 
    filter(Type == "intact") %>%  # only keep the intact trials
    mutate(Confidence = if_else(Response %in% c("11", "55"), "high", 
                                if_else(Response %in% c("12", "54"), "low",
                                        if_else(substring(Response, 2) == "3", "guess", "NA"))),
           DuraConf = paste(Duration, Confidence, sep = "_")) %>% 
    filter(Confidence != "NA",
           !(DuraConf %in% c("200_guess", "200_low"))) %>% 
    mutate(Confidence = as.factor(Confidence),
           DuraConf = factor(DuraConf, levels = c("17_guess", "17_low", "17_high", "200_high"))) %>% 
    select(-c(Event, Response, Type, Duration, Confidence)) %>% 
    gather(TimePoint, amplitude, -c(Component, Hemisphere, Category, DuraConf)) %>% 
    mutate(Hemisphere = tolower(Hemisphere), 
           TimePoint = as.numeric(TimePoint))
}

str(df.con.clean)
## tibble[,6] [9,600 × 6] (S3: tbl_df/tbl/data.frame)
##  $ Component : chr [1:9600] "N170" "N170" "N170" "N170" ...
##  $ Hemisphere: chr [1:9600] "left" "left" "left" "left" ...
##  $ Category  : Factor w/ 2 levels "face","house": 1 1 1 1 2 2 2 2 1 1 ...
##  $ DuraConf  : Factor w/ 4 levels "17_guess","17_low",..: 4 3 2 1 4 1 2 3 4 3 ...
##  $ TimePoint : num [1:9600] -200 -200 -200 -200 -200 -200 -200 -200 -200 -200 ...
##  $ amplitude : num [1:9600] 0.1733 0.4972 -0.0302 0.2781 0.189 ...

3.2 Plots

con.Plot.205 <- {
  ggplot(data = df.con.clean, aes(y = amplitude, x = TimePoint, color = DuraConf, linetype = Category)) + # set the data, varialbes for x and y axises, and the variable for dividing data
    geom_line(size = .9) +  # plot the lines
    scale_linetype_manual(values=c("solid", "dashed")) +
    # scale_color_manual(values=c("#F8766D", "#7CAE00", "#00BFC4", "#C77CFF")) +
    facet_grid(Category + Hemisphere ~ ., switch = "x") +  # show the left and right hemisphere separately
    theme_classic(base_size = 22) +  # set the theme as classical  
    scale_y_reverse(limits = c(ymax, ymin), breaks = seq(-6, 6, 3)) +  # remove the space between columns and x axis
    coord_cartesian(xlim = c(-200, 500)) +  # the limit for y axis 
    scale_x_continuous(breaks = seq(-200, 500, by = 100)) +  # the marks for x axis
    labs(title = "", 
         x = "Time points (ms)", 
         y = expression(paste("Amplitude (", mu, "V)")), 
         color = "Confidence", 
         linetype = "Category") +  # set the names for main, x and y axises   Bin ERP Plots for the N170 (E1)
    # theme(plot.title = element_text(size = 22), axis.title = element_text(size = 20), axis.text = element_text(size = 20)) + # the size of the texts in plot   
    # theme(legend.title=element_blank()) +  # remove legend title
    theme(strip.text = element_text(face="bold", size=22, lineheight=5.0), 
          strip.background = element_rect(fill="white", colour="white", size=1), 
          strip.placement = "outside", 
          legend.position = "bottom", 
          legend.title = element_text(size = 18),
          # legend.text = element_text(size = 14),
          legend.margin=margin(0,0,0,0),
          legend.box.margin=margin(-10,-10,-5,-10),
          legend.key.width = unit(.9, "cm")
          ) +
    guides(
      color = guide_legend(nrow = 2, byrow = TRUE, order = 1),
      linetype = guide_legend(nrow = 2, byrow = TRUE, order = 2)
    ) +
    geom_hline(yintercept = 0, linetype = 5, alpha = 0.5) +  # add line for y=0
    geom_vline(xintercept = 0, linetype = 5, alpha = 0.5) +  # add line for x=0
    geom_rect(aes(xmin = 96 - 0.1, xmax = 136 + 0.1, ymin = ymin + 0.05, ymax = ymax - 0.05),
              fill = "transparent", color = "grey70", size = 1, linetype = "dashed", show.legend= FALSE) + # add the time window for N170
    geom_rect(aes(xmin = 152 - 0.1, xmax = 192 + 0.1, ymin = ymin + 0.05, ymax = ymax - 0.05),
              fill = "transparent", color = "grey70", size = 1, linetype = "solid", show.legend= FALSE) # add the time window for N170
}

con.Plot.205 

# tmpfn <- "E205_ERP_confidence_plots"
# res_value = 350
# res_rela = 90
# w = 800/res_rela*res_value
# h = 1086/res_rela*res_value
# 
# png(file= paste0(tmpfn, ".png") ,width=w,height=h, res = res_value)
# 
# #plot
# con.Plot.205
# 
# dev.off()
duration_color <- c(brewer.pal(9, 'Blues')[c(6, 7, 9)], brewer.pal(12, "Paired")[6]) 

# arrange facet to one row
con_Plot_205 <- {
  ggplot(data = df.con.clean %>% mutate(DuraConf = if_else(DuraConf=="17_guess", "17ms_guess",
                                                           if_else(DuraConf=="17_low", "17ms_low",
                                                                   if_else(DuraConf=="17_high", "17ms_high", "200ms_high"))),
                                        DuraConf = factor(DuraConf, levels = c("17ms_guess", "17ms_low", "17ms_high", "200ms_high"))), aes(y = amplitude, x = TimePoint, color = DuraConf, linetype = Category)) + # set the data, varialbes for x and y axises, and the variable for dividing data
    geom_line(size = .9) +  # plot the lines
    scale_linetype_manual(values=c("solid", "dashed")) +
    scale_color_manual(values=duration_color) +
    facet_wrap(~ Hemisphere + Category, nrow = 1, labeller = label_wrap_gen(multi_line=FALSE)) +  # show the left and right hemisphere separately
    theme_classic(base_size = 20) +  # set the theme as classical  
    scale_y_reverse(limits = c(ymax, ymin), breaks = seq(-6, 6, 3)) +  # remove the space between columns and x axis
    coord_cartesian(xlim = c(-200, 500)) +  # the limit for y axis 
    scale_x_continuous(breaks = seq(-200, 500, by = 100)) +  # the marks for x axis
    labs(x = "Time points (ms)", 
         y = expression(paste("Amplitudes (", mu, "V)")), 
         color = "Duration_Confidence", 
         linetype = "Category") +  # set the names for main, x and y axises   Bin ERP Plots for the N170 (E1)
    # theme(plot.title = element_text(size = 22), axis.title = element_text(size = 20), axis.text = element_text(size = 20)) + # the size of the texts in plot   
    # theme(legend.title=element_blank()) +  # remove legend title
    theme(strip.text = element_text(size=22, lineheight=5.0), 
          strip.background = element_rect(fill="white", colour="white", size=1), 
          strip.placement = "outside", 
          legend.position = "bottom",
          # legend.title = element_text(size = 20), 
          legend.margin=margin(0,0,0,0),
          legend.box.margin=margin(-15,-10,-5,-10),
          # legend.text=element_text(size=19),
          legend.key.width = unit(.9, "cm"),
          # legend.position = c(.49, .85),
          legend.title=element_text(size=14), 
          legend.text=element_text(size=14)
          ) +
    guides(
      color = guide_legend(order = 1)
    ) +
    geom_hline(yintercept = 0, linetype = 5, alpha = 0.5) +  # add line for y=0
    geom_vline(xintercept = 0, linetype = 5, alpha = 0.5) +  # add line for x=0
    geom_rect(aes(xmin = 96 - 0.1, xmax = 136 + 0.1, ymin = ymin + 0.05, ymax = ymax - 0.05),
              fill = "transparent", color = "grey70", size = 1, linetype = "dashed", show.legend= FALSE) + # add the time window for N170
    geom_rect(aes(xmin = 152 - 0.1, xmax = 192 + 0.1, ymin = ymin + 0.05, ymax = ymax - 0.05),
              fill = "transparent", color = "grey70", size = 1, show.legend= FALSE) # add the time window for N170
}

# ggsave('con_Plot_205.pdf', con_Plot_205, width = 22, height = 4)

con_Plot_205 

duration_color <- c(brewer.pal(9, 'Blues')[c(6, 7, 9)], brewer.pal(12, "Paired")[6]) 

# arrange facet to one row
con_Plot_205 <- {
  ggplot(data = df.con.clean %>% mutate(DuraConf = if_else(DuraConf=="17_guess", "17ms_guess",
                                                           if_else(DuraConf=="17_low", "17ms_low",
                                                                   if_else(DuraConf=="17_high", "17ms_high", "200ms_high"))),
                                        DuraConf = factor(DuraConf, levels = c("17ms_guess", "17ms_low", "17ms_high", "200ms_high"))), aes(y = amplitude, x = TimePoint, color = DuraConf, linetype = Category)) + # set the data, varialbes for x and y axises, and the variable for dividing data
    geom_line(size = .9) +  # plot the lines
    scale_linetype_manual(values=c("solid", "dashed")) +
    scale_color_manual(values=duration_color) +
    facet_wrap(~ Hemisphere + Category, nrow = 1, labeller = label_wrap_gen(multi_line=FALSE)) +  # show the left and right hemisphere separately
    theme_classic(base_size = 20) +  # set the theme as classical  
    scale_y_reverse(limits = c(ymax, ymin), breaks = seq(-6, 6, 3)) +  # remove the space between columns and x axis
    coord_cartesian(xlim = c(-200, 300)) +  # the limit for y axis 
    scale_x_continuous(breaks = seq(-200, 500, by = 100)) +  # the marks for x axis
    labs(x = "Time points (ms)", 
         y = expression(paste("Amplitudes (", mu, "V)")), 
         color = "Duration_Confidence", 
         linetype = "Category") +  # set the names for main, x and y axises   Bin ERP Plots for the N170 (E1)
    # theme(plot.title = element_text(size = 22), axis.title = element_text(size = 20), axis.text = element_text(size = 20)) + # the size of the texts in plot   
    # theme(legend.title=element_blank()) +  # remove legend title
    theme(strip.text = element_text(size=22, lineheight=5.0), 
          strip.background = element_rect(fill="white", colour="white", size=1), 
          strip.placement = "outside", 
          legend.position = "bottom",
          # legend.title = element_text(size = 20), 
          legend.margin=margin(0,0,0,0),
          legend.box.margin=margin(-15,-10,-5,-10),
          # legend.text=element_text(size=19),
          legend.key.width = unit(.9, "cm"),
          # legend.position = c(.49, .85),
          legend.title=element_text(size=14), 
          legend.text=element_text(size=14)
          ) +
    guides(
      color = guide_legend(order = 1)
    ) +
    geom_hline(yintercept = 0, linetype = 5, alpha = 0.5) +  # add line for y=0
    geom_vline(xintercept = 0, linetype = 5, alpha = 0.5) +  # add line for x=0
    geom_rect(aes(xmin = 96 - 0.1, xmax = 136 + 0.1, ymin = ymin + 0.05, ymax = ymax - 0.05),
              fill = "transparent", color = "grey70", size = 1, linetype = "dashed", show.legend= FALSE) + # add the time window for N170
    geom_rect(aes(xmin = 152 - 0.1, xmax = 192 + 0.1, ymin = ymin + 0.05, ymax = ymax - 0.05),
              fill = "transparent", color = "grey70", size = 1, show.legend= FALSE) # add the time window for N170
}

# ggsave('con_Plot_205.pdf', con_Plot_205, width = 22, height = 4)

con_Plot_205 

# arrange facet to one row
con_Plot_205_bw <- {
  ggplot(data = df.con.clean %>% mutate(DuraConf = if_else(DuraConf=="17_guess", "17ms_guess",
                                                           if_else(DuraConf=="17_low", "17ms_low",
                                                                   if_else(DuraConf=="17_high", "17ms_high", "200ms_high"))),
                                        DuraConf = factor(DuraConf, levels = c("17ms_guess", "17ms_low", "17ms_high", "200ms_high"))), aes(y = amplitude, x = TimePoint, color = DuraConf, linetype = Category)) + # set the data, varialbes for x and y axises, and the variable for dividing data
    geom_line(size = .9) +  # plot the lines
    scale_linetype_manual(values=c("solid", "dashed")) +
    scale_color_manual(values=c("gray75", "gray60", "gray35", "black")) +
    facet_wrap(~ Hemisphere + Category, nrow = 1, labeller = label_wrap_gen(multi_line=FALSE)) +  # show the left and right hemisphere separately
    theme_classic(base_size = 20) +  # set the theme as classical  
    scale_y_reverse(limits = c(ymax, ymin), breaks = seq(-6, 6, 3)) +  # remove the space between columns and x axis
    coord_cartesian(xlim = c(-200, 300)) +  # the limit for y axis 
    scale_x_continuous(breaks = seq(-200, 500, by = 100)) +  # the marks for x axis
    labs(x = "Time points (ms)", 
         y = expression(paste("Amplitudes (", mu, "V)")), 
         color = "Duration_Confidence", 
         linetype = "Category") +  # set the names for main, x and y axises   Bin ERP Plots for the N170 (E1)
    # theme(plot.title = element_text(size = 22), axis.title = element_text(size = 20), axis.text = element_text(size = 20)) + # the size of the texts in plot   
    # theme(legend.title=element_blank()) +  # remove legend title
    theme(strip.text = element_text(size=22, lineheight=5.0), 
          strip.background = element_rect(fill="white", colour="white", size=1), 
          strip.placement = "outside", 
          legend.position = "bottom",
          # legend.title = element_text(size = 20), 
          legend.margin=margin(0,0,0,0),
          legend.box.margin=margin(-15,-10,-5,-10),
          # legend.text=element_text(size=19),
          legend.key.width = unit(.9, "cm"),
          # legend.position = c(.49, .85),
          legend.title=element_text(size=14), 
          legend.text=element_text(size=14)
          ) +
    guides(
      color = guide_legend(order = 1)
    ) +
    geom_hline(yintercept = 0, linetype = 5, alpha = 0.5) +  # add line for y=0
    geom_vline(xintercept = 0, linetype = 5, alpha = 0.5) +  # add line for x=0
    geom_rect(aes(xmin = 96 - 0.1, xmax = 136 + 0.1, ymin = ymin + 0.05, ymax = ymax - 0.05),
              fill = "transparent", color = "grey70", size = 1, linetype = "dashed", show.legend= FALSE) + # add the time window for N170
    geom_rect(aes(xmin = 152 - 0.1, xmax = 192 + 0.1, ymin = ymin + 0.05, ymax = ymax - 0.05),
              fill = "transparent", color = "grey70", size = 1, show.legend= FALSE) # add the time window for N170
}

# ggsave('con_Plot_205_bw.pdf', con_Plot_205_bw, width = 22, height = 4)

con_Plot_205_bw 

# arrange facet to one row
con_Plot_205_cc <- {
  ggplot(data = df.con.clean %>% mutate(DuraConf = if_else(DuraConf=="17_guess", "17ms_guess",
                                                           if_else(DuraConf=="17_low", "17ms_low",
                                                                   if_else(DuraConf=="17_high", "17ms_high", "200ms_high"))),
                                        DuraConf = factor(DuraConf, levels = c("17ms_guess", "17ms_low", "17ms_high", "200ms_high"))), aes(y = amplitude, x = TimePoint, color = DuraConf, linetype = Category)) + # set the data, varialbes for x and y axises, and the variable for dividing data
    geom_line(size = .9) +  # plot the lines
    scale_linetype_manual(values=c("solid", "dashed")) +
    scale_color_manual(values=c("gray75", "gray60", "gray35", "black")) +
    facet_grid(Category ~ Hemisphere) +  # show the left and right hemisphere separately
    theme_classic(base_size = 20) +  # set the theme as classical  
    scale_y_reverse(limits = c(ymax, ymin), breaks = seq(-6, 6, 3)) +  # remove the space between columns and x axis
    coord_cartesian(xlim = c(-200, 300)) +  # the limit for y axis 
    scale_x_continuous(breaks = seq(-200, 500, by = 100)) +  # the marks for x axis
    labs(x = "Time points (ms)", 
         y = expression(paste("Amplitudes (", mu, "V)")), 
         color = "Duration_Confidence", 
         linetype = "Category") +  # set the names for main, x and y axises   Bin ERP Plots for the N170 (E1)
    # theme(plot.title = element_text(size = 22), axis.title = element_text(size = 20), axis.text = element_text(size = 20)) + # the size of the texts in plot   
    # theme(legend.title=element_blank()) +  # remove legend title
    theme(strip.text = element_text(size=22, lineheight=5.0), 
          strip.background = element_rect(fill="white", colour="white", size=1), 
          strip.placement = "outside", 
          legend.position = "bottom",
          # legend.title = element_text(size = 20), 
          legend.margin=margin(0,0,0,0),
          legend.box.margin=margin(-15,-10,-5,-10),
          # legend.text=element_text(size=19),
          legend.key.width = unit(.7, "cm"),
          # legend.position = c(.49, .85),
          legend.title=element_text(size=14), 
          legend.text=element_text(size=14)
          ) +
    guides(color = guide_legend(order = 1)) +
    # guides(linetype = guide_legend(title.position = "left", nrow = 1),
    #        color = guide_legend(title.position = "left", nrow = 2)) +
    geom_hline(yintercept = 0, linetype = 5, alpha = 0.5) +  # add line for y=0
    geom_vline(xintercept = 0, linetype = 5, alpha = 0.5) +  # add line for x=0
    geom_rect(aes(xmin = 96 - 0.1, xmax = 136 + 0.1, ymin = ymin + 0.05, ymax = ymax - 0.05),
              fill = "transparent", color = "grey70", size = 1, linetype = "dashed", show.legend= FALSE) + # add the time window for N170
    geom_rect(aes(xmin = 152 - 0.1, xmax = 192 + 0.1, ymin = ymin + 0.05, ymax = ymax - 0.05),
              fill = "transparent", color = "grey70", size = 1, show.legend= FALSE) # add the time window for N170
}

# ggsave('con_Plot_205_cc.pdf', con_Plot_205_cc, width = 11, height = 5.25)

con_Plot_205_cc 

4 ERP plots for intact stim with blocks

4.1 Load and clean the data

df.binerp.block <- read_csv(file.path("data", "E205_BinERP_block.csv"))

df.block.clean <- {
  df.binerp.block %>% 
    substr_Event() %>% 
    filter(Type == "intact") %>%  # only keep the intact trials
    mutate(Confidence = if_else(Response %in% c("11", "55"), "high", 
                                if_else(Response %in% c("12", "54"), "low",
                                        if_else(substring(Response, 2) == "3", "guess", "NA"))),
           DuraConf = paste(Duration, Confidence, sep = "_")) %>% 
    filter(Confidence != "NA",
           DuraConf %in% c("17_high", "200_high")) %>% 
    mutate(DuraBlock = paste(Duration, Block, sep = "-")) %>% 
    # mutate(Confidence = as.factor(Confidence),
    #        DuraConf = factor(DuraConf, levels = c("17_guess", "17_low", "17_high", "200_high"))) %>% 
    select(-c(Event, Response, Block, Type, Duration, Confidence, DuraConf)) %>% 
    gather(TimePoint, amplitude, -c(Component, Hemisphere, Category, DuraBlock)) %>% 
    mutate(Hemisphere = tolower(Hemisphere), 
           TimePoint = as.numeric(TimePoint))
}

str(df.block.clean)
## tibble[,6] [3,600 × 6] (S3: tbl_df/tbl/data.frame)
##  $ Component : chr [1:3600] "N170" "N170" "N170" "N170" ...
##  $ Hemisphere: chr [1:3600] "left" "left" "left" "left" ...
##  $ Category  : Factor w/ 2 levels "face","house": 1 1 1 2 2 2 1 1 1 2 ...
##  $ DuraBlock : chr [1:3600] "200-2" "17-1" "17-2" "200-2" ...
##  $ TimePoint : num [1:3600] -200 -200 -200 -200 -200 -200 -200 -200 -200 -200 ...
##  $ amplitude : num [1:3600] 0.173 0.495 0.274 0.189 0.153 ...

4.2 Plots

block.Plot.205 <- {
  ggplot(data = df.block.clean, aes(y = amplitude, x = TimePoint, color = DuraBlock, linetype = Category)) + # set the data, varialbes for x and y axises, and the variable for dividing data
    geom_line(size = .9) +  # plot the lines
    scale_linetype_manual(values=c("solid", "dashed")) +
    scale_color_manual(values=c("#F8766D", "#7CAE00", "#C77CFF")) + # ,  "#00BFC4"
    facet_grid(Category + Hemisphere ~ ., switch = "x") +  # show the left and right hemisphere separately
    theme_classic(base_size = 22) +  # set the theme as classical  
    scale_y_reverse(limits = c(ymax, ymin), breaks = seq(-6, 6, 3)) +  # remove the space between columns and x axis
    coord_cartesian(xlim = c(-200, 500)) +  # the limit for y axis 
    scale_x_continuous(breaks = seq(-200, 500, by = 100)) +  # the marks for x axis
    labs(title = "", 
         x = "Time points (ms)", 
         y = expression(paste("Amplitude (", mu, "V)")), 
         color = "Confidence", 
         linetype = "Category") +  # set the names for main, x and y axises   Bin ERP Plots for the N170 (E1)
    # theme(plot.title = element_text(size = 22), axis.title = element_text(size = 20), axis.text = element_text(size = 20)) + # the size of the texts in plot   
    # theme(legend.title=element_blank()) +  # remove legend title
    theme(strip.text = element_text(face="bold", size=22, lineheight=5.0), 
          strip.background = element_rect(fill="white", colour="white", size=1), 
          strip.placement = "outside", 
          legend.position = "bottom", 
          legend.title = element_text(size = 18), 
          legend.margin=margin(0,0,0,0),
          legend.box.margin=margin(-10,-10,-5,-10),
          legend.key.width = unit(.9, "cm")
          ) +
    guides(
      color = guide_legend(nrow = 2, byrow = TRUE, order = 1),
      linetype = guide_legend(nrow = 2, byrow = TRUE, order = 2)
    ) +
    geom_hline(yintercept = 0, linetype = 5, alpha = 0.5) +  # add line for y=0
    geom_vline(xintercept = 0, linetype = 5, alpha = 0.5) +  # add line for x=0
    geom_rect(aes(xmin = 96 - 0.1, xmax = 136 + 0.1, ymin = ymin + 0.05, ymax = ymax - 0.05),
              fill = "transparent", color = "grey70", size = 1, linetype = "dashed", show.legend= FALSE) + # add the time window for N170
    geom_rect(aes(xmin = 152 - 0.1, xmax = 192 + 0.1, ymin = ymin + 0.05, ymax = ymax - 0.05),
              fill = "transparent", color = "grey70", size = 1, linetype = "solid", show.legend= FALSE) # add the time window for N170
}

block.Plot.205 

# tmpfn <- "E205_ERP_block_plots"
# res_value = 350
# res_rela = 90
# w = 800/res_rela*res_value
# h = 1086/res_rela*res_value
# 
# png(file= paste0(tmpfn, ".png") ,width=w,height=h, res = res_value)
# 
# #plot
# block.Plot.205
# 
# dev.off()
duration_color <- c(brewer.pal(9, 'Blues')[c(6, 8)], brewer.pal(12, "Paired")[6]) 

block_Plot_205 <- {
  ggplot(data = df.block.clean %>% mutate(DuraBlock = if_else(DuraBlock == "17-1", "17ms_half1", if_else(DuraBlock == "17-2", "17ms_half2", "200ms_half2"))), aes(y = amplitude, x = TimePoint, color = DuraBlock, linetype = Category)) + # set the data, varialbes for x and y axises, and the variable for dividing data
    geom_line(size = .9) +  # plot the lines
    scale_linetype_manual(values=c("solid", "dashed")) +
    scale_color_manual(values=duration_color) +
    facet_wrap(~ Hemisphere + Category, nrow = 1, labeller = label_wrap_gen(multi_line=FALSE)) +  # show the left and right hemisphere separately
    theme_classic(base_size = 20) +  # set the theme as classical  
    scale_y_reverse(limits = c(ymax, ymin), breaks = seq(-6, 6, 3)) +  # remove the space between columns and x axis
    coord_cartesian(xlim = c(-200, 500)) +  # the limit for y axis 
    scale_x_continuous(breaks = seq(-200, 500, by = 100)) +  # the marks for x axis
    labs(x = "Time points (ms)", 
         y = expression(paste("Amplitudes (", mu, "V)")), 
         color = "Duration_Half", 
         linetype = "Category") +  # set the names for main, x and y axises   Bin ERP Plots for the N170 (E1)
    # theme(plot.title = element_text(size = 22), axis.title = element_text(size = 20), axis.text = element_text(size = 20)) + # the size of the texts in plot   
    # theme(legend.title=element_blank()) +  # remove legend title
    theme(strip.text = element_text(size=22, lineheight=5.0), 
          strip.background = element_rect(fill="white", colour="white", size=1), 
          strip.placement = "outside", 
          legend.position = "bottom", 
          # legend.title = element_text(size = 18), 
          legend.margin=margin(0,0,0,0),
          legend.box.margin=margin(-15,-10,-5,-10),
          legend.key.width = unit(.9, "cm"),
          legend.title=element_text(size=14), 
          legend.text=element_text(size=14)
          ) +
    # guides(
    #   color = guide_legend(nrow = 2, byrow = TRUE, order = 1),
    #   linetype = guide_legend(nrow = 2, byrow = TRUE, order = 2)
    # ) +
    geom_hline(yintercept = 0, linetype = 5, alpha = 0.5) +  # add line for y=0
    geom_vline(xintercept = 0, linetype = 5, alpha = 0.5) +  # add line for x=0
    geom_rect(aes(xmin = 96 - 0.1, xmax = 136 + 0.1, ymin = ymin + 0.05, ymax = ymax - 0.05),
              fill = "transparent", color = "grey70", size = 1, linetype = "dashed", show.legend= FALSE) + # add the time window for N170
    geom_rect(aes(xmin = 152 - 0.1, xmax = 192 + 0.1, ymin = ymin + 0.05, ymax = ymax - 0.05),
              fill = "transparent", color = "grey70", size = 1, linetype = "solid", show.legend= FALSE) # add the time window for N170
}

# ggsave('block_Plot_205.pdf', block_Plot_205, width = 22, height = 4)

block_Plot_205 

duration_color <- c(brewer.pal(9, 'Blues')[c(6, 8)], brewer.pal(12, "Paired")[6]) 

block_Plot_205 <- {
  ggplot(data = df.block.clean %>% mutate(DuraBlock = if_else(DuraBlock == "17-1", "17ms_half1", if_else(DuraBlock == "17-2", "17ms_half2", "200ms_half2"))), aes(y = amplitude, x = TimePoint, color = DuraBlock, linetype = Category)) + # set the data, varialbes for x and y axises, and the variable for dividing data
    geom_line(size = .9) +  # plot the lines
    scale_linetype_manual(values=c("solid", "dashed")) +
    scale_color_manual(values=duration_color) +
    facet_wrap(~ Hemisphere + Category, nrow = 1, labeller = label_wrap_gen(multi_line=FALSE)) +  # show the left and right hemisphere separately
    theme_classic(base_size = 20) +  # set the theme as classical  
    scale_y_reverse(limits = c(ymax, ymin), breaks = seq(-6, 6, 3)) +  # remove the space between columns and x axis
    coord_cartesian(xlim = c(-200, 300)) +  # the limit for y axis 
    scale_x_continuous(breaks = seq(-200, 500, by = 100)) +  # the marks for x axis
    labs(x = "Time points (ms)", 
         y = expression(paste("Amplitudes (", mu, "V)")), 
         color = "Duration_Half", 
         linetype = "Category") +  # set the names for main, x and y axises   Bin ERP Plots for the N170 (E1)
    # theme(plot.title = element_text(size = 22), axis.title = element_text(size = 20), axis.text = element_text(size = 20)) + # the size of the texts in plot   
    # theme(legend.title=element_blank()) +  # remove legend title
    theme(strip.text = element_text(size=22, lineheight=5.0), 
          strip.background = element_rect(fill="white", colour="white", size=1), 
          strip.placement = "outside", 
          legend.position = "bottom", 
          # legend.title = element_text(size = 18), 
          legend.margin=margin(0,0,0,0),
          legend.box.margin=margin(-15,-10,-5,-10),
          legend.key.width = unit(.9, "cm"),
          legend.title=element_text(size=14), 
          legend.text=element_text(size=14)
          ) +
    guides(
      color = guide_legend(nrow = 1, byrow = TRUE, order = 1),
      linetype = guide_legend(nrow = 1, byrow = TRUE, order = 2)
    ) +
    geom_hline(yintercept = 0, linetype = 5, alpha = 0.5) +  # add line for y=0
    geom_vline(xintercept = 0, linetype = 5, alpha = 0.5) +  # add line for x=0
    geom_rect(aes(xmin = 96 - 0.1, xmax = 136 + 0.1, ymin = ymin + 0.05, ymax = ymax - 0.05),
              fill = "transparent", color = "grey70", size = 1, linetype = "dashed", show.legend= FALSE) + # add the time window for N170
    geom_rect(aes(xmin = 152 - 0.1, xmax = 192 + 0.1, ymin = ymin + 0.05, ymax = ymax - 0.05),
              fill = "transparent", color = "grey70", size = 1, linetype = "solid", show.legend= FALSE) # add the time window for N170
}

# ggsave('block_Plot_205.pdf', block_Plot_205, width = 22, height = 4)

block_Plot_205 

block_Plot_205_bw <- {
  ggplot(data = df.block.clean %>% mutate(DuraBlock = if_else(DuraBlock == "17-1", "17ms_half1", if_else(DuraBlock == "17-2", "17ms_half2", "200ms_half2"))), aes(y = amplitude, x = TimePoint, color = DuraBlock, linetype = Category)) + # set the data, varialbes for x and y axises, and the variable for dividing data
    geom_line(size = .9) +  # plot the lines
    scale_linetype_manual(values=c("solid", "dashed")) +
    scale_color_manual(values=c("gray60", "gray35", "black")) +
    facet_wrap(~ Hemisphere + Category, nrow = 1, labeller = label_wrap_gen(multi_line=FALSE)) +  # show the left and right hemisphere separately
    theme_classic(base_size = 20) +  # set the theme as classical  
    scale_y_reverse(limits = c(ymax, ymin), breaks = seq(-6, 6, 3)) +  # remove the space between columns and x axis
    coord_cartesian(xlim = c(-200, 300)) +  # the limit for y axis 
    scale_x_continuous(breaks = seq(-200, 500, by = 100)) +  # the marks for x axis
    labs(x = "Time points (ms)", 
         y = expression(paste("Amplitudes (", mu, "V)")), 
         color = "Duration_Half", 
         linetype = "Category") +  # set the names for main, x and y axises   Bin ERP Plots for the N170 (E1)
    # theme(plot.title = element_text(size = 22), axis.title = element_text(size = 20), axis.text = element_text(size = 20)) + # the size of the texts in plot   
    # theme(legend.title=element_blank()) +  # remove legend title
    theme(strip.text = element_text(size=22, lineheight=5.0), 
          strip.background = element_rect(fill="white", colour="white", size=1), 
          strip.placement = "outside", 
          legend.position = "bottom", 
          # legend.title = element_text(size = 18), 
          legend.margin=margin(0,0,0,0),
          legend.box.margin=margin(-15,-10,-5,-10),
          legend.key.width = unit(.9, "cm"),
          legend.title=element_text(size=14), 
          legend.text=element_text(size=14)
          ) +
    guides(
      color = guide_legend(nrow = 1, byrow = TRUE, order = 1),
      linetype = guide_legend(nrow = 1, byrow = TRUE, order = 2)
    ) +
    geom_hline(yintercept = 0, linetype = 5, alpha = 0.5) +  # add line for y=0
    geom_vline(xintercept = 0, linetype = 5, alpha = 0.5) +  # add line for x=0
    geom_rect(aes(xmin = 96 - 0.1, xmax = 136 + 0.1, ymin = ymin + 0.05, ymax = ymax - 0.05),
              fill = "transparent", color = "grey70", size = 1, linetype = "dashed", show.legend= FALSE) + # add the time window for N170
    geom_rect(aes(xmin = 152 - 0.1, xmax = 192 + 0.1, ymin = ymin + 0.05, ymax = ymax - 0.05),
              fill = "transparent", color = "grey70", size = 1, linetype = "solid", show.legend= FALSE) # add the time window for N170
}

# ggsave('block_Plot_205_bw.pdf', block_Plot_205_bw, width = 22, height = 4)

block_Plot_205_bw 

block_Plot_205_cc <- {
  ggplot(data = df.block.clean %>% mutate(DuraBlock = if_else(DuraBlock == "17-1", "17ms_half1", if_else(DuraBlock == "17-2", "17ms_half2", "200ms_half2"))), aes(y = amplitude, x = TimePoint, color = DuraBlock, linetype = Category)) + # set the data, varialbes for x and y axises, and the variable for dividing data
    geom_line(size = .9) +  # plot the lines
    scale_linetype_manual(values=c("solid", "dashed")) +
    scale_color_manual(values=c("gray60", "gray35", "black")) +
    facet_grid(Category ~ Hemisphere) +  # show the left and right hemisphere separately
    theme_classic(base_size = 20) +  # set the theme as classical  
    scale_y_reverse(limits = c(ymax, ymin), breaks = seq(-6, 6, 3)) +  # remove the space between columns and x axis
    coord_cartesian(xlim = c(-200, 300)) +  # the limit for y axis 
    scale_x_continuous(breaks = seq(-200, 500, by = 100)) +  # the marks for x axis
    labs(x = "Time points (ms)", 
         y = expression(paste("Amplitudes (", mu, "V)")), 
         color = "Duration_Half", 
         linetype = "Category") +  # set the names for main, x and y axises   Bin ERP Plots for the N170 (E1)
    # theme(plot.title = element_text(size = 22), axis.title = element_text(size = 20), axis.text = element_text(size = 20)) + # the size of the texts in plot   
    # theme(legend.title=element_blank()) +  # remove legend title
    theme(strip.text = element_text(size=22, lineheight=5.0), 
          strip.background = element_rect(fill="white", colour="white", size=1), 
          strip.placement = "outside", 
          legend.position = "bottom", 
          # legend.title = element_text(size = 18), 
          legend.margin=margin(0,0,0,0),
          legend.box.margin=margin(-15,-10,-5,-10),
          legend.key.width = unit(.9, "cm"),
          legend.title=element_text(size=14), 
          legend.text=element_text(size=14)
          ) +
    guides(
      color = guide_legend(nrow = 1, byrow = TRUE, order = 1),
      linetype = guide_legend(nrow = 1, byrow = TRUE, order = 2)
    ) +
    geom_hline(yintercept = 0, linetype = 5, alpha = 0.5) +  # add line for y=0
    geom_vline(xintercept = 0, linetype = 5, alpha = 0.5) +  # add line for x=0
    geom_rect(aes(xmin = 96 - 0.1, xmax = 136 + 0.1, ymin = ymin + 0.05, ymax = ymax - 0.05),
              fill = "transparent", color = "grey70", size = 1, linetype = "dashed", show.legend= FALSE) + # add the time window for N170
    geom_rect(aes(xmin = 152 - 0.1, xmax = 192 + 0.1, ymin = ymin + 0.05, ymax = ymax - 0.05),
              fill = "transparent", color = "grey70", size = 1, linetype = "solid", show.legend= FALSE) # add the time window for N170
}

# ggsave('block_Plot_205_cc.pdf', block_Plot_205_cc, width = 11, height = 5)

block_Plot_205_cc