1

I've read this and this but cannot make it work.

vDF <- data.frame(v = rnorm(50,1,40))
g <- ggplot(vDF, aes(x = vDF)) + geom_histogram()
ggsave(g, file="name.eps") 

I keep getting the error

Error in grDevices::postscript(..., onefile = FALSE, horizontal = FALSE, : cannot open file 'name.eps'

Why can't I make this work? I've see advice saying 'hey just do...

setEPS()
postscript("whatever.eps")
plot(rnorm(100), main="Hey Some Data")
dev.off()

But I can't even save the original .eps file in the first place.

markus
  • 23,189
  • 5
  • 29
  • 47
llewmills
  • 2,137
  • 1
  • 23
  • 40

3 Answers3

4

First, there's an error in your code. It should be:

vDF <- data.frame(v = rnorm(50,1,40))
g <- ggplot(vDF, aes(x = v)) + geom_histogram()

(note the aes).

Now, to save the plot as eps, you have to use the option device=eps in ggsave:

ggsave(g, file="name.eps", device="eps")
Stéphane Laurent
  • 48,421
  • 14
  • 86
  • 170
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/20694434) – Vasim Shaikh Aug 27 '18 at 12:51
  • @VasimVanzara Why do you say that does not provide an answer? That doesn't work? For me the question was clear and this is the answer. – Stéphane Laurent Aug 27 '18 at 12:53
  • You should add a some explanation. If you want to try something you can ask same in comment. – Vasim Shaikh Aug 27 '18 at 12:54
  • @VasimVanzara Ah I see there's an error. I edit. Thanks. – Stéphane Laurent Aug 27 '18 at 12:55
  • Thank you @Stephane Laurent. Embarrassing coding error. However even after I fixed that it still did not work. Then shifted to my laptop and it worked fine. It seems my difficulties may be related to permissions for creating image files on my work server rather than an error with ggplot or myself. Anyway this is handy to know, thank you again. – llewmills Aug 28 '18 at 04:48
1

Discovered how to do this properly

ggplot2::ggsave(filename = "foo.eps",
                plot = foo,
                device = cairo_ps)

specifying cairo_ps for the device is the key

llewmills
  • 2,137
  • 1
  • 23
  • 40
0

None of the above works for me. I had to specify the font in the ggsave()

    ggsave(filename = "fig1.eps", plot=fig1,family="Times")
Gilbert M.
  • 21
  • 3