为 lapply 内的每个文件创建多个绘图
creating multiple plots for each file within lapply
我有几个文件正在通过 lapply 循环播放。在 lapply 内,我想为每个文件制作 1 个 MAPLOT 和 1 个火山图。这是我的代码:
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)
运行 在 Rstudio 下,我希望它为字符向量中存在的每个输入文件生成 1 个 MAPLOT 和 1 个火山图 files
(有 8 个输入文本文件)。但是相反,它只绘制了 8 个火山图。
我在这里错过了什么?
我还想在 1 个 pdf(volcano.pdf) 中输出所有火山图,每页 1 个,在另一个 pdf(maplot.pdf) 中输出所有 MAPLOT,每页 1 个。我怎样才能做到这一点?
这是一个主要是 dev.set
的教学示例,我在没有任何打开的图形设备的新 R 进程中有 运行:
## Report active device
dev.cur()
## null device
## 1
## Open device 2 and make it the active device
pdf("bar.pdf")
## Open device 3 and make it the active device
pdf("foo.pdf")
## List all open devices
dev.list()
## pdf pdf
## 2 3
f <- function() {
## Plot in device 3
plot(1:10, 1:10, main = "foo")
## Cycle to device 2
dev.set()
## Plot in device 2
plot(rnorm(10L), rnorm(10L), main = "bar")
## Cycle to device 3
dev.set()
invisible(NULL)
}
## Call 'f' four times
replicate(4L, f())
## Close device 3 and report active device
dev.off()
## pdf
## 2
## Close device 2 and report active device
dev.off()
## null device
## 1
## Clean up
unlink(c("foo.pdf", "bar.pdf"))
@Limey 的建议是一次在一台设备上工作,以避免 dev.set
所需的簿记:
pdf("foo.pdf")
f1 <- function() {
plot(1:10, 1:10, main = "foo")
invisible(NULL)
}
replicate(4L, f1())
dev.off()
pdf("bar.pdf")
f2 <- function() {
plot(rnorm(10L), rnorm(10L), main = "bar")
invisible(NULL)
}
replicate(4L, f2())
dev.off()
unlink(c("foo.pdf", "bar.pdf"))
我有几个文件正在通过 lapply 循环播放。在 lapply 内,我想为每个文件制作 1 个 MAPLOT 和 1 个火山图。这是我的代码:
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)
运行 在 Rstudio 下,我希望它为字符向量中存在的每个输入文件生成 1 个 MAPLOT 和 1 个火山图 files
(有 8 个输入文本文件)。但是相反,它只绘制了 8 个火山图。
我在这里错过了什么?
我还想在 1 个 pdf(volcano.pdf) 中输出所有火山图,每页 1 个,在另一个 pdf(maplot.pdf) 中输出所有 MAPLOT,每页 1 个。我怎样才能做到这一点?
这是一个主要是 dev.set
的教学示例,我在没有任何打开的图形设备的新 R 进程中有 运行:
## Report active device
dev.cur()
## null device
## 1
## Open device 2 and make it the active device
pdf("bar.pdf")
## Open device 3 and make it the active device
pdf("foo.pdf")
## List all open devices
dev.list()
## pdf pdf
## 2 3
f <- function() {
## Plot in device 3
plot(1:10, 1:10, main = "foo")
## Cycle to device 2
dev.set()
## Plot in device 2
plot(rnorm(10L), rnorm(10L), main = "bar")
## Cycle to device 3
dev.set()
invisible(NULL)
}
## Call 'f' four times
replicate(4L, f())
## Close device 3 and report active device
dev.off()
## pdf
## 2
## Close device 2 and report active device
dev.off()
## null device
## 1
## Clean up
unlink(c("foo.pdf", "bar.pdf"))
@Limey 的建议是一次在一台设备上工作,以避免 dev.set
所需的簿记:
pdf("foo.pdf")
f1 <- function() {
plot(1:10, 1:10, main = "foo")
invisible(NULL)
}
replicate(4L, f1())
dev.off()
pdf("bar.pdf")
f2 <- function() {
plot(rnorm(10L), rnorm(10L), main = "bar")
invisible(NULL)
}
replicate(4L, f2())
dev.off()
unlink(c("foo.pdf", "bar.pdf"))