1

I'm trying to print a series of Venn diagrams (using the VennDiagram package) to a single png file by opening the file, calling the function multiple times, then dev.off(). I can get the plots to print but they all come out printed on top of each other. I want them one per page or in a layout, don't mind, as long as you can see each Venn! :) I can't find any help already on Stack overflow with reference to this problem of printing on top of each other or just crashing.

Here is my current (minimal) code with dummy data. My real function does a lot of other things and has to return something else, which is why I've put a placeholder return() here, just to highlight that I can't (don't want to) return the grid object and print them all once I have the full list. Which would be the sensible way to do it, but that spot's taken...

library(VennDiagram)

plot_Venn <- function(data) {
  venn.plot <- venn.diagram(data, filename = NULL, imagetype = "png")
  grid.draw(venn.plot)
  #grid.newpage()
  return("yay")
}

data.list <- list(list(A = 1:150, B = 121:170),
                  list(A = 3:50,B = 45:55),
                  list(A = 1:10, B = 8:400),
                  list(A = 15:109, B = 8:140),
                  list(A = 40:80, B = 8:55),
                  list(A = 1:100, B = 80:200))
png("testmin.png")
par(mfrow=c(2,3))
# or: grid.layout(ncol = 2,nrow = 3)

lapply(data.list, plot_Venn)

dev.off()

Using grid.newpage() in plot_Venn() made just a blank page in the png file. The 'par()' line of code doesn't appear to do anything. Neither did the grid.layout attempt. i.e. with or without those lines the plots printed on top of each other. But I'm still stumbling about in the dark a bit when it comes to layouts! I guess I could also print each Venn in individual files and then combine them, but the point is not to clutter up the folder with a thousand Venns, just make one neat file. Any help would be greatly appreciated, thankyou!

Update: I just tried returning the plot from the function and I can't get that to work either - even from a list they still print either on top of each other or not at all.

1

1 Answer 1

0

Hmm, worked out a solution myself eventually. So in case anyone else needs it... The trick was to put it in a pdf (if you're cool with that - which I am). I was halfway there because using filename=NULL in venn.diagram() makes it return a grid object. Then even though the grid object was made with imagetype="png", you can stick it in a pdf. And in a pdf, grid.newpage() worked fine. If you want it in a png I'm still clueless :) Here's the solution code:

library(VennDiagram)
plot_Venn <- function(data) {
    venn.plot <- venn.diagram(data, filename = NULL, imagetype = "png")
    grid.newpage()
    grid.draw(venn.plot)
    return("yay")
}

data.list <- list(list(A = 1:150, B = 121:170),
              list(C = 3:50,D = 45:55),
              list(E = 1:10, EF = 8:400),
              list(G = 15:109, H = 8:140),
              list(I = 40:80, J = 8:55),
              list(K = 1:100, L = 80:200))

pdf("testmin.pdf")
lapply(data.list, plot_Venn)
dev.off()

Not the answer you're looking for? Browse other questions tagged or ask your own question.