并排输出 Venn gList 对象和网络图

Output Venn gList object and Network plot side-by-side

我们如何绘制网络图(igraph package plot)和维恩图(VennDiagram gList 对象side -并排在一个 PDF 页面中?

尝试了以下解决方案,但没有成功:
Plot gList side by side

Side-by-side Venn diagram using Vennerable

这是一个示例,将它们绘制在两页中。我使用 grid.newpage() 将其绘制在单独的页面中,否则它会被绘制在彼此之上。

library(grid)
library(igraph)
library(VennDiagram)

#network graph object
relations <- data.frame(from=c("Bob", "Cecil", "Cecil", "David",
                               "David", "Esmeralda"),
                        to=c("Alice", "Bob", "Alice", "Alice", "Bob", "Alice"))
g <- graph_from_data_frame(relations, directed=TRUE)

# venn plot object
plotVenn <- venn.diagram(
  list(A = 1:150, B = 121:170),
  filename = NULL)
class(plotVenn)
# [1] "gList"

# output to PDF, outputs into 2 pages, I need 1 page 2 plots side-by-side
pdf("temp.pdf")

#network
igraph::plot.igraph(g)

#venn
grid.newpage()
grid.draw(plotVenn)

dev.off()

从上面的第一个 link 中汲取灵感,您可以 "trick" 通过让 "regular" plotgrid 共存来解决问题:

pdf("temp.pdf", )
layout(matrix(1:2, nrow=1))
igraph::plot.igraph(g)
plot.new()
pushViewport(viewport(layout = grid.layout(1, 2, widths=unit(c(0.5, 0.5), "npc"))))
pushViewport(viewport(layout.pos.col = 2))
grid.draw(plotVenn)
popViewport(0)
dev.off()

根据评论中的链接提出以下解决方案:

pdf("temp.pdf")

layout(matrix(1:2, 1, byrow = TRUE))
#network
igraph::plot.igraph(g)

#network
frame()
vps <- baseViewports()
pushViewport(vps$inner, vps$figure, vps$plot)
grid.draw(plotVenn)
popViewport(2)

dev.off()