2017-03-09 11 views
1

何らかの理由で、このループで生成されるPDFが壊れてしまいます。しかし、個々にプロットすると保存され、開くことができます。怒ってアドバイスしてください!pdfプロットを開くことができませんr

for (l in 1:length(which_genes)) { 
     gene_name <- which_genes[[l]] 
     cases_values <- cases[cases$HGNC == genes[gene_name],] 
     controls_values <- controls[controls$HGNC == genes[gene_name],] 
     t <- t.test(cases_values[c(2:ncol(cases_values))], controls_values[c(2:ncol(controls_values))]) 
     case <- cbind(t(cases_values[c(2:ncol(cases_values))]), "cases") 
     cont <- cbind(t(controls_values[c(2:ncol(controls_values))]), "controls") 
     dat <- as.data.frame(rbind(case, cont)) 
     names(dat) <- c("expression", "type") 
     dat$expression <- as.numeric(dat$expression) 
     #plot significant genes 
     pdf(file = paste(genes[gene_name], "_different.pdf", sep="")) 
     ggplot(dat, aes(type, expression, fill=type)) + 
     geom_boxplot() + 
     ggtitle(paste(genes[gene_name], "pvalue", t$p.value)) + 
     xlab("cases vs controls") 
     dev.off() 
} 
+0

データのサンプルを追加できますか? http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Travis

+0

Sys.sleepを使用して、ggplot2/gridにプロットを完了するまでの時間を長くしてください。 – Roland

+0

@Roland私はSys.sleep(5)をpdfの前後に試しましたが、今度は、プロットのサイズが163バイトから4KBに保存されているのがわかります。まだ壊れています。ループなしでプロットすると、開いているPDFの保存が5KBになります。他に提案はありますか? – user3324491

答えて

1

故障対printエラーのさらに別の例(R-FAQに記載されているように)。代わりに、ループ内でこれを使用します。目標は、あなたがprintは、ループ内-ed、そして、ループの外でPDF-デバイスが開かれている必要があり、その後、マルチページ出力を持っていた場合

pdf(file = paste(genes[gene_name], "_different.pdf", sep="")) 
    print(ggplot(dat, aes(type, expression, fill=type)) + 
    geom_boxplot() + 
    ggtitle(paste(genes[gene_name], "pvalue", t$p.value)) + 
    xlab("cases vs controls") 
     ) 
dev.off() 

、デバイスを閉じました外側。

関連する問題