R:循环中的cairo_pdf输出不产生任何文件

时间:2013-12-01 08:39:37

标签: r ggplot2 cairo

我正在尝试使用cairo_pdf生成多个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
}

但这根本不会产生任何文件。同时,如果我手动运行for {}内的代码(即运行上面的代码后,我只是逐行运行大括号中的代码),它会按预期生成一个PDF。

如何正确实现这个想法?

1 个答案:

答案 0 :(得分:1)

抱歉,这是重复的。解决方案在这里:

ggplot's qplot does not execute on sourcing

简而言之,我需要为print(ggplot(..))执行cairo_pdf来捕获输出。