0

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?

Anton Tarasenko
  • 8,099
  • 11
  • 66
  • 91

1 Answers1

1

Sorry, that's a duplicate. Solution is here:

ggplot's qplot does not execute on sourcing

In short, I needed to do print(ggplot(..)) for cairo_pdf to capture the output.

Community
  • 1
  • 1
Anton Tarasenko
  • 8,099
  • 11
  • 66
  • 91