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.
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:
The problem is that you don't close the
pdf()
device withdev.off()
That works, as does:
But don't mix the two.
The following plot
works in the console but not in a function or when you source this from a file.
Will produce a corrupt pdf file and the way to fix it us use
within a function.
In a console. "p" is automatically printed but not in a function or when you source the file.
It is in the R FAQ, you need a
print()
around your call toggplot()
-- and you need to close the plotting device withdev.off()
as well, ie tryEdit: I was half-right on the
dev.off()
, apparently theprint()
isn;t needed. Gavin's answer has more.