Skip to content

Instantly share code, notes, and snippets.

@giorgiosironi
Created June 5, 2017 20:23
Show Gist options
  • Save giorgiosironi/c9727e8679420c2c8f11406959aded65 to your computer and use it in GitHub Desktop.
Save giorgiosironi/c9727e8679420c2c8f11406959aded65 to your computer and use it in GitHub Desktop.
R plots

Export current plot from ggplot

https://stackoverflow.com/a/11304431/91590

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).

PNG or other formats

http://statistics.berkeley.edu/computing/saving-plots-r

For example, to create a png file called myplot.png from a graph that is displayed by R, type

dev.copy(png,'myplot.png') dev.off()

Cairo package for antialiased plots

Primarily for print: http://gforge.se/2013/02/exporting-nice-plots-in-r/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment