r – Plot 6 plots in one pdf using ggplot2

I have 6 files which I want to compare to two other files and if a match is found in file1 plot red dots if it is found in file2 plot green dots and if found in none plot grey dots.

I have the following code however no results are being generated.

    file1 <- read.table("truthlist.tab", header = TRUE)
    file2 <- read.table("background.tab", header = TRUE)

    file_list <- list(
      file3 = "all/Sample101.summary.tab",
      file4 = "all-Sort/Sample101.summary.tab",
      file5 = "right/Sample101.summary.tab",
      file6 = "right-Sort/Sample101.summary.tab",
      file7  = "left/Sample101.summary.tab",
      file8  = "left-Sort/Sample101.summary.tab"
    )
pdf("Sampl101_plots.pdf")

    for (i in 1:length(file_list)) {
      file_name <- names(file_list)[i]
      file_path <- file_list[[i]]
      file <- read.table(paste0("Desktop\\Sample101\\", file_path), header = TRUE)
    }

matched_file1 <- file[file$Position %in% file1$Position, ]
matched_file2 <- file[file$Position %in% file2$Position, ]

if(nrow(matched_file1) > 0 & nrow(matched_file2) > 0){
  ggplot(file, aes(x = Position, y = AF)) +
    geom_point(color = "grey", size = 3) +
    geom_point(data = matched_file1, aes(color = "matched_file1"), size = 3) +
    geom_point(data = matched_file2, aes(color = "matched_file2"), size = 3) +
    scale_color_manual(values = c("truthlist" = "red", "background" = "green")) +
    labs(title = file_name)
} else if (nrow(matched_file1) > 0 & nrow(matched_file2) == 0){
  ggplot(file, aes(x = Position, y = AF)) +
    geom_point(color = "grey", size = 3) +
    geom_point(data = matched_file1, aes(color = "matched_file1"), size = 3) +
    scale_color_manual(values = c("truthlist" = "red", "background" = "green")) +
    labs(title = file_name)
} else if (nrow(matched_file1) == 0 & nrow(matched_file2) == 0){
  ggplot(file, aes(x = Position, y = AF)) +
    geom_point(color = "grey", size = 3) +
    scale_color_manual(values = c("truthlist" = "red", "background" = "green")) +
    labs(title = file_name)
} else if (nrow(matched_file1) == 0 & nrow(matched_file2) > 0){
  ggplot(file, aes(x = Position, y = AF)) + geom_point(color = "grey", size = 3) +
    scale_color_manual(values = c("truthlist" = "red", "background" = "green")) +
    labs(title = file_name)
}

The script is working for individual files generating a simgle plot, however it is not generating all 6 plots in one pdf.

How can I resolve this issue. Thanks in advance !

Read more here: Source link