13

I am encountering an odd problem. I am able to create and save pdf file using R/ggplot2 and view them while the R Console is running. As soon as I exit the R console, Preview on Mac OS X will no longer display the PDF. I have been able to save .png files w/o problem, but for reasons beyond my control, I need to save in pdf files. The code I am using to save is as follows:

  pdfFile <-c("/Users/adam/mock/dir/structure.pdf")
  pdf(pdfFile)
  ggplot(y=count,data=allCombined, aes(x=sequenceName, fill=factor(subClass))) + geom_bar()
  ggsave(pdfFile)  

Has anyone encountered a similar problem? If so, what do I need to do to fix it? Thank you very much for your time.

wespiserA
  • 3,131
  • 5
  • 28
  • 36

4 Answers4

31

The problem is that you don't close the pdf() device with dev.off()

dat <- data.frame(A = 1:10, B = runif(10))
require(ggplot2)

pdf("ggplot1.pdf")
ggplot(dat, aes(x = A, y = B)) + geom_point()
dev.off()

That works, as does:

ggplot(dat, aes(x = A, y = B)) + geom_point()
ggsave("ggplot1.pdf")

But don't mix the two.

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
  • @Thierry There was nothing wrong with the original code I supplied - you *don't* *need* to save the previously plotted object in an object as `ggsave()` has a default for argument `plot`, the result of `last_plot()`. Hence I rolled back your edit. – Gavin Simpson Apr 11 '11 at 21:32
6

It is in the R FAQ, you need a print() around your call to ggplot() -- and you need to close the plotting device with dev.off() as well, ie try

pdfFile <-c("/Users/adam/mock/dir/structure.pdf")
pdf(pdfFile)
ggplot(y=count,data=allCombined,aes(x=sequenceName,fill=factor(subClass)))
      + geom_bar()
dev.off()

Edit: I was half-right on the dev.off(), apparently the print() isn;t needed. Gavin's answer has more.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • the print() is needed when you put the call in a function or when you source the script. – Thierry Apr 11 '11 at 20:33
  • Thanks for answering Dirk! Stackoverflow is a real life saver at work. Whats the URL for the R FAQ you referred to? – wespiserA Apr 12 '11 at 00:44
  • 2
    @wespiserA I wanted to include a LMGTFY link but SO won't allow me, so the actual link will have to suffice ;-) http://cran.r-project.org/doc/FAQ/R-FAQ.html – Gavin Simpson Apr 13 '11 at 12:30
  • haha, thanks Gavin, SO does all the work for me, well, besides the actual coding and reading of documents. – wespiserA Apr 13 '11 at 12:58
  • @Gavin Simpson, you should read this b4 egregiously recommending google, http://meta.stackexchange.com/questions/8724/how-to-deal-with-google-questions – wespiserA Apr 14 '11 at 02:38
  • @wespiserA I wasn't recommending Google, egregiously or otherwise. And anyway, the post on Meta that you link to is about linking to Google to answer Qs... And my comment was meant as a joke! :-) – Gavin Simpson May 03 '11 at 13:15
5

The following plot

pdf("test.pdf")  
p <- qplot(hp, mpg, data=mtcars, color=am,   
         xlab="Horsepower", ylab="Miles per Gallon", geom="point")   
p  
dev.off()

works in the console but not in a function or when you source this from a file.

myfunc <- function() {  
  p <- qplot(hp, mpg, data=mtcars, color=am,   
           xlab="Horsepower", ylab="Miles per Gallon", geom="point")  
  p 
}  
pdf("test.pdf")  
myfunc()  
dev.off()  

Will produce a corrupt pdf file and the way to fix it us use

print(p) 

within a function.

In a console. "p" is automatically printed but not in a function or when you source the file.

Icarus
  • 1,627
  • 7
  • 18
  • 32
user7732049
  • 51
  • 1
  • 1
-1

You can also change the filename of your pdf plot within ggsave if you want to call it something other than "ggplot1" or whatever concise object name you chose; just give the filename first and then tell it which plot you're referring to, for example:

a <- ggplot(dat, aes(x = A, y = B)) + geom_point()
ggsave("Structure.pdf",plot=a)
user3482899
  • 320
  • 3
  • 10
  • The original question already was saving the plot with a customized name (namely the value in `pdfFile`). And this doesn't answer the given question. – Brian Diggs Oct 03 '14 at 21:41
  • Yes but it's an extra line of code in the original post and unnecessary. The question is how to view a plot and save it as a PDF. The original code doesn't work because dev.off() isn't being used. As Gavin mentioned, ggsave circumvents the need to use dev.off(). What's not clear from Gavin's answer is how to keep the filename as something other than the name of the plot. ggsave *also* circumvents the need to specify the filename on a separate line, as the original post did. So, I think this answer does answer the question. Calling "a" shows plot in R, and ggsave makes PDF w/ desired filename – user3482899 Oct 04 '14 at 00:12