0

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 !

Callie
  • 333
  • 1
  • 9
  • 1
    ggplot grobs need to be explicitly `print`ed; this happens naturally on the console, as the object returned looks for a `print` method. Inside of a `for` loop and in an `if` block, this can also be a problem. See https://stackoverflow.com/q/2547306/3358272, https://stackoverflow.com/q/4811106/3358272, https://stackoverflow.com/q/15678261/3358272 – r2evans May 03 '23 at 12:07
  • 1
    FYI, you should not use a single `&` in an `if` conditional unless it is wrapped in an aggregation such as `sum`, `any`, or `all`. Use `&&` here. In the cases here it will not _error_, but: (1) it's a bad habit; and (2) `&` is less-efficient here in that it does not short-circuit. – r2evans May 03 '23 at 12:09
  • 1
    Lastly, you overwrite `file` in each pass of your `for` loop, so you only have one file's worth loaded. It seems likely you want something like `alldata <- dplyr::bind_rows(sapply(file.path("Desktop/Sample101", file_list), read.table, header=TRUE, simplify=FALSE), .id = "filename")`. – r2evans May 03 '23 at 12:12

0 Answers0