plot_names <- c("ERP plot", "Butterfly plot", "Topoplot", "Topoplot\ntimeseries", "ERP grid", "ERP image", "Channel image", "Parallel plot")
t <- list(n1, n2, n3, n4, n5, n6, n7, n8) 
create_table <- function(t){
  tmp <- t[[1]] %>% mutate(total = sum(Scores)) %>% slice(1:3) %>% mutate(type = plot_names[1]) %>% rename_with(.cols = 1, ~"names") %>% relocate(type, names )
  
  for (i in 2:8){
    tmp <- t[[i]]  %>% mutate(total = sum(Scores)) %>% slice(1:3) %>% 
      mutate(type = plot_names[i]) %>% rename_with(.cols = 1, ~"names") %>% relocate(type, names) %>% 
      rbind(tmp, .)
  }
  tmp <- tmp %>% 
    group_by( type) %>%
    dplyr::mutate(groupRow = 1:n()) %>%
    ungroup() %>% mutate(p = round(as.numeric(Scores)/as.numeric(total), 2) * 100) %>% 
    mutate(score = paste(p, total, sep = "% out of ")) %>% 
    dplyr::mutate(type = ifelse(groupRow == 1, as.character(type), "")) %>%
    dplyr::mutate(Scores = ifelse(groupRow == 1, as.character(score), paste(p, "%", sep = ""))) %>%
    select(-c(groupRow, score))
}
plots2 <- c("../miniatures/1.png", "../miniatures/filler.png", "../miniatures/filler.png", "../miniatures/2.png", "../miniatures/filler.png", "../miniatures/filler.png", "../miniatures/3.png","../miniatures/filler.png", "../miniatures/filler.png", "../miniatures/4.png", "../miniatures/filler.png", "../miniatures/filler.png", "../miniatures/5.png", "../miniatures/filler.png", "../miniatures/filler.png", "../miniatures/6.png", "../miniatures/filler.png", "../miniatures/filler.png", "../miniatures/7.png", "../miniatures/filler.png", "../miniatures/filler.png", "../miniatures/8.png", "../miniatures/filler.png")
plotnames <- create_table(t) %>% data.frame() %>% 
  mutate(names = str_to_sentence(names)) %>% 
  mutate(names = str_replace(names, "Erp", "ERP")) %>% 
  mutate(names = str_replace(names, "erp", "ERP")) %>% tibble::add_column(Plots = plots2, .before = "type") %>% 
  dplyr::rename(`Plot types` = type,
         `Aggregated responses` = names) %>% select(-total, -p) 
t2 <- plotnames %>% flextable() %>% 
  merge_at(j= 1, i = 1:3) %>% merge_at(j= 1, i = 4:6) %>% 
  merge_at(j= 1, i = 7:9) %>% merge_at(j= 1, i = 10:12) %>%
  merge_at(j= 1, i = 13:15) %>% merge_at(j= 1, i = 16:18) %>%
  merge_at(j= 1, i = 19:21) %>% merge_at(j= 1, i = 22:23) %>%
  
  merge_at(j= 2, i = 1:3) %>% merge_at(j= 2, i = 4:6) %>% 
  merge_at(j= 2, i = 7:9) %>% merge_at(j= 2, i = 10:12) %>%
  merge_at(j= 2, i = 13:15) %>% merge_at(j= 2, i = 16:18) %>%
  merge_at(j= 2, i = 19:21) %>% merge_at(j= 2, i = 22:23)  %>% 
  valign(j = 2, valign = "center", part = "all") %>%
  compose(j = 'Plots', value = as_paragraph(as_image(Plots, width = 1, height = 0.6))) %>%
   padding(padding = 1, part = "all") %>% 
  autofit() %>% valign(valign = "top") %>% 
  height_all(0.01, part = "all", unit = "cm") %>% 
  surround(
    i = c(3, 6, 9, 12, 15, 18, 21),
    j = 1:4,
    border.bottom = fp_border(color = "gray", style = "dashed"),
    part = "body"
  ) %>%   font(fontname = "Lato", part="all") %>% 
  hline_bottom(part = "body", border = fp_border(color = "white", width = 3)) 
t2