Hi,
The reason this is happening is that a loop will only print output if you explicitly tell it to.
# No output printed to console
for(i in 1:10){
i
}
# Output printed
for(i in 1:10){
print(i)
}
So to make sure the PDF captures the ggplot, you need to wrap it in the plot() function
library(ggplot2)
pdf(file="test.pdf")
for(i in 1:10){
plot(ggplot(data.frame(x = 1:10, y = runif(10)), aes(x = x, y = y)) +
geom_line())
}
dev.off()
That should fix it... unless there's something wrong with the plots itself of course, but you can check that by running the code inside the loop manually.
Hope this helps,
PJ