113

How do I export a graph to an .eps format file? I typically export my graphs to a .pdf file (using the 'pdf' function), and it works quite well. However, now I have to export to .eps files.

d-cubed
  • 902
  • 5
  • 29
  • 52
mStudent
  • 1,418
  • 3
  • 12
  • 21
  • You could try searching in R by typing `??eps`. You would either get `postscript` page or at least `ps.options` (which would lead you to `postscript`). – Roman Luštrik Feb 28 '11 at 14:30
  • See also [pdf2ps](https://www.ghostscript.com/doc/current/Use.htm#PDF) to convert pdf files to ps. Shell usage: `$ pdf2ps plot.pdf`, will create `plot.ps`. – Paul Rougieux Aug 28 '17 at 13:25

5 Answers5

134

The easiest way I've found to create postscripts is the following, using the setEPS() command:

setEPS()
postscript("whatever.eps")
plot(rnorm(100), main="Hey Some Data")
dev.off()
CompEcon
  • 1,834
  • 1
  • 14
  • 10
55

If you are using ggplot2 to generate a figure, then a ggsave(file="name.eps") will also work.

Maiasaura
  • 29,590
  • 23
  • 96
  • 103
  • 3
    Ah, this is just what I needed! And you can modify size like this: `ggsave("name.eps", width = 20, height = 20, units = "cm")` – DirtStats Apr 29 '16 at 05:05
31

The postscript() device allows creation of EPS, but only if you change some of the default values. Read ?postscript for the details.

Here is an example:

postscript("foo.eps", horizontal = FALSE, onefile = FALSE, paper = "special")
plot(1:10)
dev.off()
Gavin Simpson
  • 157,540
  • 25
  • 364
  • 424
  • Thanks. I am tryin' to use it, but there is an error : graph margins too large... – mStudent Mar 01 '11 at 09:42
  • 6
    make the plot dimensions bigger: `postscript("foo.eps", horizontal = FALSE, onefile = FALSE, paper = "special", height = 10, width = 10)` for example. The units are in inches. The problem is that the device you are plotting to is not large enough to contain the margins of the plot region you are using. – Gavin Simpson Mar 01 '11 at 10:43
  • 1
    I needed the exported *.eps to work in Microsoft Powerpoint, which was not the case initially. After some trial and error I noticed I have to set additionally the parameter `colormodel="rgb"` of postscript(). – agoldev Apr 12 '16 at 14:49
14

Another way is to use Cairographics-based SVG, PDF and PostScript Graphics Devices. This way you don't need to setEPS()

cairo_ps("image.eps")
plot(1, 10)
dev.off()
USER_1
  • 2,096
  • 1
  • 23
  • 27
  • 1
    This is a useful suggestion. `cairo_ps` differs from `postscript` in that it supports more unicode glyphs, but has the drawback that semi-transparency is poorly handled, and will often trigger bitmap rather than vector output. – ms609 Oct 26 '18 at 09:34
4

Yes, open a postscript() device with a filename ending in .eps, do your plot(s) and call dev.off().

Dirk Eddelbuettel
  • 331,520
  • 51
  • 596
  • 675