我有几个文件,我正在循环通过我的申请。在中,我想为每个文件创建一个MAPLOT和一个Volcano图。这是我的代码:
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下运行这个程序,我期望它为字符向量files中的每个输入文件生成1个MAPLOT和1个Volcano图(有8个输入文本文件)。但相反,它只绘制了8座火山的地块。
我在这里错过了什么?
我也想输出所有的火山图在一个pdf(volcano.pdf),每页一个和所有的MAPLOT在另一个pdf(maplot.pdf),每页1。我怎样才能做到这一点?
发布于 2022-02-11 18:11:21
下面是一个关于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"))https://stackoverflow.com/questions/71075922
复制相似问题