【发布时间】:2015-11-09 15:26:49
【问题描述】:
我正在尝试在一页中绘制多个图。我知道像gridExtra::grid.arrange 这样的函数可以绘制由ggplot2 包生成的图形。我面临的问题是我有两个由ggplot2 包生成的图(bar.plot 和下面的density.plot)和一个使用limma::vennDiagram 函数生成的图。我已经尝试了以下方法,但它不起作用:
output <- paste('summary.pdf')
pdf(output,width = 25,height = 20)
par(mfrow = c(3, 3))
plot(bar.plot)
plot(density.plot)
print(vennDiagram(dat.venn, circle.col = col,cex = c(3,3,3)))
invisible(dev.off())
dat.venn 是 VennCounts 类型的数据:
I-H-10003-T1-D1 I-H-10003-T2-D1 I-H-10003-T3-D1 Counts
0 0 0 0
0 0 1 41
0 1 0 81
0 1 1 66
1 0 0 10
1 0 1 2
1 1 0 4
1 1 1 56
attr(,"class")
[1] "VennCounts"
我找不到与grid.arrange 函数兼容的维恩图包。我不认为VennCounts不能用grid.arrange函数打印出来,ggplot2 plots可以用par函数打印出来。
更新: 我尝试使用 pushViewport,但它仍在下一页上打印维恩图:
pdf(output,width = 25,height = 20)
# Create layout : nrow = 2, ncol = 2
pushViewport(viewport(layout = grid.layout(2, 2)))
# A helper function to define a region on the layout
define_region <- function(row, col){
viewport(layout.pos.row = row, layout.pos.col = col)
}
# Arrange the plots
print(bar.plot, vp = define_region(1, 1:2))
print(density.plot, vp = define_region(2, 1))
print(vennDiagram(dat.venn, circle.col = col,cex = c(3,3,3)), vp = define_region(2, 2))
dev.off()
任何帮助将不胜感激!
【问题讨论】:
-
@Roland 查看我的更新
-
我没有看到你在哪里使用 gridBase 包中的函数。
-
你给我的链接是gridBase的手册。我看到有一些使用
pushViewport的建议,我使用了它。 -
由于需要组合多种绘图类型(基于网格的或 Excel、plotrix 等),我已将它们全部保存为。 .png 文件,使用 grid 包的 read.PNG 函数加载它们,然后使用 rasterGrob 函数对其进行转换。一旦所有图的格式都具有可比性,grid.arrange() 就可以了。
标签: r plot ggplot2 venn-diagram