5

How can we plot Network plot (igraph package plot) and Venn diagram (VennDiagram gList object) side-by-side in one PDF page?

Tried to follow below solutions, didn't work:
Plot gList side by side
Plot 2 tmap objects side-by-side
Side-by-side Venn diagram using Vennerable

Here is an example, which plots them in two pages. I used grid.newpage() to make it plot in separate pages, otherwise it gets plotted on top of each other.

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()

enter image description here

Community
  • 1
  • 1
zx8754
  • 52,746
  • 12
  • 114
  • 209
  • 2
    http://stackoverflow.com/questions/33269538/venn-diagrams-in-multiple-figure-r perhaps useful – user20650 Nov 24 '16 at 08:45
  • 1
    perhaps also useful : http://stackoverflow.com/questions/27929452/r-return-corrplot-as-object#27948707, to convert igraph plot to grob – user20650 Nov 24 '16 at 08:56
  • 1
    @user20650 OK, I think this is a dupe to your earlier answers. Thank you for the links. – zx8754 Nov 24 '16 at 09:23

2 Answers2

4

Taking inspiration from the first link above, you can "trick" your way out by making coexist a "regular" plot and a grid:

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()

enter image description here

Cath
  • 23,906
  • 5
  • 52
  • 86
3

Came up with below solution following links in the comments: Venn diagrams in multiple figure

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()
Community
  • 1
  • 1
zx8754
  • 52,746
  • 12
  • 114
  • 209