绘制多页 [英] Plotting over multiple pages

查看:71
本文介绍了绘制多页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个在多个页面上绘制ggplot facet_wrap图的函数.这只是一个hack,因为此功能似乎在ggplot2功能的待办事项列表中.我进行了一些小的计算,以找到所需的页面数,每页需要的data.frame行数等.我对所有这些工作很有信心.

I'm trying to write a function that plots a ggplot facet_wrap plot over multiple pages. It's just a hack, as this feature seems to be on the ggplot2 feature to-do list. I do some small calculations to find the number of pages I'm going to need, the number of rows of my data.frame that I need per page etc. I'm pretty confident this all works.

pdf(filename)
for (i in seq(num_pages)){
    slice = seq(((i-1)*num_rows)+1,(i*num_rows))
    slice = slice[!(slice > nrow(df.merged))]
    df.segment=df.merged[slice,]
    p <- ggplot(df.segment, aes(y=mean,x=phenotype))
    p <- p + geom_bar(stat="identity",fill="white",colour="black") 
    p + facet_wrap("ID",scales="free_y",ncol=n_facets,nrow=n_facets)
}
dev.off()

我的问题是,通过将所有内容包装在这样的for循环中,在pdf()dev.off()函数之间,是for循环似乎没有等待ggplot完成其任务,并快速浏览其循环并输出无效的PDF.

My problem is that, by wrapping it all up in a for loop like this, in between the pdf() and dev.off() functions, is that the for loop doesn't seem to wait for ggplot to do its thing, and blazes through its loop very quickly and outputs an invalid PDF.

如果我设置了i = 1,启动了pdf(),在for循环内运行了上面的代码,然后设置了i=2,然后运行了代码,依此类推,直到感到无聊(i=3),然后转向在设备上生成的PDF非常出色.

If I set i = 1, start the pdf(), run the above code inside the for loop, then set i=2, then run the code, and so on until I get bored (i=3) then turn off the device the resulting PDF is brilliant.

有没有一种方法可以让for循环在进入下一个迭代之前等待最后一行完成绘制?

Is there a way I can get the for loop to wait for the final line to finish plotting before moving onto the next iteration?

推荐答案

我认为问题是,您需要在最后一行(p + ...)周围使用print()才能将其实际打印到for中的设备中环形 . . .

I think the problem is that you need print() around your last line (p+ ...) to get it to actually print to the device inside the for loop . . .

这篇关于绘制多页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆