I have several files which I am looping via lapply. Within lapply, I want to make for each file, 1 MAPLOT and 1 Volcano plot. Here is my code :
library(ggplot2)
library(dplyr)
files <- list.files(path = baseDir,pattern = "*.txt",full.names = T,recursive = F)
fun <- function(x){
a <- basename(x)
a <- gsub(".txt","",a)
df <- read.table(x,header = TRUE,sep = "\t")
df <- df[,c(1,(ncol(df)-5):(ncol(df)))]
df <- mutate(df,threshold = ifelse(padj < 0.05,"sig","non-sig"))
df$sigtype <- paste(df$threshold,df$type,sep="-")
## Make MAPLOT
ggplot(df, aes(x = baseMean, y = log2FoldChange)) +
scale_x_continuous(trans = "log10")+
geom_point(aes(col = threshold), size = 1, shape = 20)+
scale_color_manual(values = c("non-sig" = "gray70","sig" = "red")) +
ylim(-5, 10)+geom_hline(yintercept = 0, linetype = "dashed",color = "black") +
xlab("mean of normalized counts")+ theme_classic()
## Make Volcano plot
ggplot(df, aes(x = log2FoldChange, y = -log10(padj))) +
scale_x_continuous()+ geom_point()+ xlab("fold change")+ theme_classic()
}
lapply(files,fun)
Running this under Rstudio, I expected it to generate 1 MAPLOT and 1 Volcano plot for every input file present in the character vector files
(there are 8 input text files). But instead, it only plotted the 8 volcano plot.
What am I missing here?
Also I want to output all the volcano plots in 1 pdf(volcano.pdf), 1 per page and all the MAPLOT's in another pdf(maplot.pdf), 1 per page. How can I achieve this?