I'm trying to generate multiple PDF plots by using cairo_pdf
:
c <- 1
for (q in unique(resp$ques)) {
cairo_pdf(sprintf("./imgs/q%1$d.pdf", c), width=16, height=9)
ggplot(resp[resp$ques==q,], aes(x=dep, y=answ)) +
stat_summary(fun.y = "mean", geom = "bar") +
coord_flip()
dev.off()
c <- c + 1
}
But this produces no files at all. Meanwhile, if I run the code inside for {}
manually (i.e. after running the above code, I just run the code in the braces line by line), it does make one PDF as expected.
How to implement this idea correctly?