2

I am generating a plot which I am able to see in the RMarkdown output but whenever I am trying to save it, I am getting just a blank(white) image. I am just adding following two lines before and after plot

png("Output.png")
#Plot code
dev.off()

It was working. And suddenly it stopped working. Can someone help me on this??

edit

When I do it with pdf

pdf("output.pdf")
#plot code
dev.off()

I am getting error as:

There was error opening this document. This document cannot be opened because it has no pages. 

Thanks.

Community
  • 1
  • 1
Rachit Agrawal
  • 3,203
  • 10
  • 32
  • 56
  • 1
    This should work, the problem is probably in the `plot code` section. Please create a reproducible example, http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. My guess is that you are using either `lattice` or `ggplot2` and that you need to call `print` on the code that generates the figure. – Paul Hiemstra Apr 18 '13 at 05:28
  • I suspect you have a device opened. Run `dev.off()` until you get a message `Error in dev.off() : cannot shut down device 1 (the null device)`. – Roman Luštrik Apr 18 '13 at 05:30
  • 1
    @PaulHiemstra Yes, you were right I was using `ggplot`. And I needed to call `print` to get the image. Thanks. – Rachit Agrawal Apr 18 '13 at 05:52
  • I posted my comment as an answer, you can accept it to show everyone else that this has been solved. – Paul Hiemstra Apr 18 '13 at 05:55
  • possible duplicate of [Call to plot doesn't actually produce plot](http://stackoverflow.com/questions/12730030/call-to-plot-doesnt-actually-produce-plot) and http://stackoverflow.com/questions/4811106/cant-print-to-pdf-ggplot-charts/4811119#4811119 and 14 more. – IRTFM Apr 18 '13 at 07:11

1 Answers1

5

From the comments I gather that I was right, you need to print the resulting ggplot2 object in order to show the plot, see also R FAQ 7.22.

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
  • Hi, this happens to me even without using ggplot2 (I'm using base graphics "boxplot" function). Any idea why this might be happening? – AHegde Mar 13 '17 at 20:00
  • @AHegde this problem is not related to this one as the boxplot function does not use `grid` graphics. Please create a new question which includes a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Paul Hiemstra Mar 14 '17 at 15:04