plots - save plot in r

Is there a command to export the current plot? (2)

I'm creating a whole mess of charts, and would like to export the produced charts as PDF. It's possible to click 'export' in the Plot tab in rstudio and manually select "save plot as PDF", of course, and I'd prefer not to wrap every single one of my sections in pdf() /

Ideally I'd like a function that would take the currently-displayed plot in RStudio and save it with the parameters I'd like (format /filename / width / height).



As per @naught101's suggestion - for a 5x7 (inch!) pdf file which is a convenient size to be pasted into an A4 Word document, the following works well:

dev.copy2pdf(file="example.pdf", width = 7, height = 5)

Better yet, as an easily-called function with default dimensions:

dopdf <- function(filename = "dopdf.pdf", pdf.width = 7, pdf.height = 5) {
 dev.copy2pdf(file=filename, width = pdf.width, height = pdf.height)

While using ggplot2 would have let me save using the ggsave function, dev.copy2pdf is a good generic solution (answering my original question).

I think you're looking for dev.copy2pdf. This allows you to copy the current device to a pdf, using the same arguments as pdf(). Works just as well for the base plotting functions as for ggplot2, (and any other plotting libraries that use the standard grDevice plotting devices, by the looks of it).

If you use ggplot2, you can use the ggsave function to save the current plot. This function allows you to choose the width, heigth etc of the resulting plot (PDF, png, or other). When not using ggplot2, the savePlot function can be used to get similar functionality, although the man page suggests it only works on cairo X11 devices.