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.
{grid}
and {VennDiagram
}? stackoverflow.com/questions/71326403/…