6

I have used the recipe given here with a lot of success. However, for past few days this does not seem to work. My sessionInfo() looks as follows:

R version 2.15.2 (2012-10-26)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] VennDiagram_1.5.1

loaded via a namespace (and not attached):
[1] tools_2.15.2 

I tried the following, and did not produce any result:

 require(VennDiagram)
 
 venn.diagram(list(B = 1:1800, A = 1571:2020),fill = c("red", "green"), alpha = c(0.5, 0.5), cex = 2,cat.fontface = 4,lty =2, fontfamily =3, filename = "trial2.emf")

But did not produce any result.

Am I doing anything wrong?

user438383
  • 5,716
  • 8
  • 28
  • 43
Sam
  • 7,922
  • 16
  • 47
  • 62
  • This works for me. Can you provide a bit more information? How didn't it produce a result? Was there an error? Is your session empty? what does `list.files(getwd(), '*.emf')` result in? – Justin Jan 09 '13 at 18:10
  • @Justin There was no error, the session is not empty, and there are no output files produced. None whatsoever. But to be sure, can you tell me how to look if the session is empty? – Sam Jan 09 '13 at 18:12
  • By empty I meant "clean". Quit R and start it again. Then run only the two commands you included in your post. Also, make sure you're in the directory you think you are. You can sort that out with the two functions in my previous comment. – Justin Jan 09 '13 at 18:19
  • @Justin yes, i thought so, its still the same result. No files generated that end with `.emf`. I guess i will write to the maintainer. – Sam Jan 09 '13 at 18:23
  • and the output of `list.files(getwd(), '*.emf')` is nothing? You may want to check with the package maintainer then. – Justin Jan 09 '13 at 18:23
  • 1
    I can confirm that no file is created on x86_64-apple-darwin9.8.0/x86_64 (64-bit) – GSee Jan 09 '13 at 18:32
  • 1
    Can you draw it onscreen: `temp = venn.diagram(...filename=NULL); grid.draw(temp)` – MattBagg Jan 09 '13 at 18:33
  • @MattBagg, > grid.draw(temp) Error in grid.Call.graphics(L_text, as.graphicsAnnot(x$label), x$x, x$y, : Invalid font type – GSee Jan 09 '13 at 18:37
  • @GSee, I assume you've gone on to try omitting the two font arguments. – MattBagg Jan 09 '13 at 18:42
  • @MattBagg It does generate the `grob`, and the font arguments did not give any error. It works with `temp = venn.diagram( ...filename=NULL);grid.draw(temp)`. – Sam Jan 09 '13 at 19:13
  • @Sam If you can draw onscreen, your issue may be writing the file. emf (a windows format) is an unusual choice on apple. I am guessting you are putting it into an office doc? You might try pdf or a high resolution png instead. – MattBagg Jan 09 '13 at 19:22
  • @MattBagg, i have tried png, pdf does not work. – Sam Jan 09 '13 at 19:23
  • Have you checked that R has permissions to write to the working dir (identifiable with `getwd()` ). A general way to save plots is putting code to draw the plot between a `pdf()` or `png()` and a `dev.off()` as in: `library(grDevices);png(file="venn.png"); grid.draw(temp);dev.off()` – MattBagg Jan 09 '13 at 19:43
  • @MattBagg permissions are not a problem. I actually saved the file exactly as you have shown here. Its just confusing. – Sam Jan 09 '13 at 19:57
  • Maybe I should submit that as an answer so you can close the issue if no one else has a better solution. – MattBagg Jan 09 '13 at 20:55
  • @MattBagg Please do submit it as a answer, so that i can close it. Thank you again. – Sam Jan 10 '13 at 13:22

2 Answers2

13

One work-around is to use png() or pdf() to save the plot. We first confirm that we can draw the plot onscreen using grid.draw():

library(VennDiagram)
temp <- venn.diagram(list(B = 1:1800, A = 1571:2020),
    fill = c("red", "green"), alpha = c(0.5, 0.5), cex = 2,cat.fontface = 4,
    lty =2, fontfamily =3, filename = NULL)
grid.draw(temp)

Having confirmed that, all we need to do to save it is repeat the grid.draw() between pdf() and dev.off()

library(grDevices)

pdf(file="venn.pdf")
    grid.draw(temp)
dev.off()

As described in their help files, pdf() and png() have arguments for controlling things like the size of the image, improving control over image quality.

MattBagg
  • 10,268
  • 3
  • 40
  • 47
7

MattBagg's answer is excellent but for completeness, let me add how to save multiple venn diagrams in the same page - useful when comparing multiple conditions. Something like this: enter image description here This solution is a mash up-up of MattBagg's and nmel's answers wrapped in a pdf() function.

# libraries
library(VennDiagram)
library(grid)
library(gridBase)
library(lattice)

# create the diagrams
temp1 <- venn.diagram(list(B = 1:1800, A = 1571:2020),
    fill = c("red", "green"), alpha = c(0.5, 0.5), cex = 1,cat.fontface = 2,
    lty =2, filename = NULL)
temp2 <- venn.diagram(list(A = 1:1800, B = 1571:2020),
    fill = c("red", "green"), alpha = c(0.5, 0.5), cex = 1,cat.fontface = 2,
    lty =2, filename = NULL)    


# start new page
plot.new() 

pdf("testpdf", width = 14, height = 7)
# setup layout
gl <- grid.layout(nrow=1, ncol=2)
# grid.show.layout(gl)

# setup viewports
vp.1 <- viewport(layout.pos.col=1, layout.pos.row=1) 
vp.2 <- viewport(layout.pos.col=2, layout.pos.row=1) 

# init layout
pushViewport(viewport(layout=gl))
# access the first position
pushViewport(vp.1)

# start new base graphics in first viewport
par(new=TRUE, fig=gridFIG())

grid.draw(temp2)

# done with the first viewport
popViewport()

# move to the next viewport
pushViewport(vp.2)

  grid.draw(temp2)

# done with this viewport
popViewport(1)

dev.off()
Community
  • 1
  • 1
fridaymeetssunday
  • 1,118
  • 1
  • 21
  • 31