2

I am using the following code to save multiple plots in a single pdf file. Individual plots work well, but with a loop I get a corrupt file that doesn't have pages

pdf("HistogramsAll.pdf", onefile=TRUE)
for (i in 1:nFilt) {
d.i<-dAll[,c(1,i+1)];
nameP.i<-names(dAll)[i+1];
names(d.i)<-c("cond", "p");
    ggplot(data=d.i, aes(x=p, fill=cond))+
        geom_histogram(binwidth=.3, position="dodge")   +
    ggtitle(eval(nameP.i));
}
dev.off()
tanyalog
  • 21
  • 2

1 Answers1

1

As per the documentation, an explicit print() is required inside a loop.

Generally, you do not need to print or plot a ggplot2 plot explicitly: the default top-level print method will do it for you. You will, however, need to call print() explicitly if you want to draw a plot inside a function or for loop.

Jonathan Carroll
  • 3,897
  • 14
  • 34