6

I'm having trouble with exporting eps files from R and importing into Word 2010.

I'm using ggplot2 plots, eg

library(ggplot2)
p <- qplot(disp,hp,data=mtcars) + stat_smooth()
p

Even after calling setEPS() neither of the following produce files which can be successfully imported.

ggsave("plot.eps")

postscript("plot.eps")
print(p)
dev.off()

The strange thing is that if I produce the plot using File -> Save As -> Postscript from the menu in the GUI, it can be imported correctly. However, when the Word document is subsequently exported as a pdf, the fonts in the graphic are a little jagged.

So my questions are:

  • What combination of (ggsave/postscript) settings allows me to produce eps files that can be imported into Word 2010?
  • How can I ensure the fonts remain clear when the Word document is exported as a pdf?

Update

After more investigation I have had more luck with cairo_ps to produce the plots. However, no text shows up when imported into Word.

Furthermore, after checking the various eps outputs (cairo_ps, save from the GUI, ggsave) in a latex document, it seems like the eps import filter in Word quite poor as the printed/pdf output doesn't match the quality of the latex'd document. The ggsave version (which uses postscript) did have some issues with colours that the other two methods didn't have though.

The conclusion is that this is a Word issue and therefore fortune(109) does not apply. I'd be happy to be proven otherwise, but I'll award the answer and the bounty to whoever can provide the commands that can replicate the output from the GUI in command form.

James
  • 65,548
  • 14
  • 155
  • 193

5 Answers5

4

This worked for me... following advice in the postscript help page:

 postscript("RPlot.eps", height = 4, width = 4, horizontal = FALSE, onefile = FALSE,
             paper = "special")
 library(ggplot2)
 p <- qplot(disp,hp,data=mtcars) + stat_smooth()
 p
#geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to #change the smoothing method.
#Warning message:
#In grid.Call.graphics(L_polygon, x$x, x$y, index) :
#  semi-transparency is not supported on this device: reported only once per page
 dev.off()
#quartz 
#     2 

The funny stuff at the end puts you on notice that this is only a Mac-tested solution, so far anyway.

Edit: I just tested it with R version 2.15.1 (2012-06-22) -- "Roasted Marshmallows": Platform: i386-pc-mingw32/i386 (32-bit) and MS Word 2007 in Win XP and it worked. Commands were Insert/Picture.../select eps format/select file.

Edit2: There is another method for saving besides directly using the postscript device. The savePlot method with an "eps" mode is available in Windows (but not in the Mac). I agree that the fonts are not as smooth as they appear on a Mac but I can discern no difference in quality between saving with savePlot and using save as from an interactive window.

savePlot(filename = "Rplot2", type = "eps", device = dev.cur(), restoreConsole = TRUE)

savePlot calls (.External(CsavePlot, device, filename, type, restoreConsole))

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • I think `setEPS()` sets these options globally. In any case, it still doesn't work for me on Windows. – James Oct 07 '12 at 16:38
  • It only shows in some of the viewing modes. But that's a Word issue, not an R problem. – IRTFM Oct 08 '12 at 06:26
  • `savePlot` looks like the answer, but it has less flexibility than the other methods. Yes I agree, and always knew really, that Word was the issue - it has more quirks and bugs than the entomology breakout session at the national OCD conference! Anyway, Bravo! The tick and bounty are yours. – James Oct 08 '12 at 08:06
  • It looks like MS Word import filter for EPS ignores previews, and it is critical to [embed fonts](https://github.com/wch/extrafont). Otherwise preview will look ugly, while it will print just fine on PostScript printer with appropriate drivers. – mlt Dec 20 '12 at 17:44
4

I solved the problem with exporting .eps files from R and importing into Word 2010 on Windows 7 using the colormodel="rgb" option (defaults to "srgb") of the postscript command.

postscript("RPlot.eps", height = 4, width = 4, horizontal = FALSE, 
         paper = "special", colormodel = "rgb")
library(ggplot2)
p <- qplot(disp,hp,data=mtcars) + stat_smooth(se=FALSE, method="loess")
p
dev.off()
1

You are probably better of using wmf as a format which you can create on Windows.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • not necessarily, wmf format can be a little glitchy too. I thought there was a nice reference about exporting graphics to MS Office on the R-wiki, but I can't find it. – Ben Bolker Oct 05 '12 at 14:22
  • 1
    Well my _real_ advice is of course not to touch Word at all, but not everybody can escape to latex/sweave/knitr/rmd/... That said, when I had to use it in the past, wmf was the least painful option. – Dirk Eddelbuettel Oct 05 '12 at 14:28
  • I usually had the best luck with ridiculously high-resolution PNGs (storage size doesn't scale that badly) that could then be resized appropriately within the Office document. The only trick is keeping the 'physical size' (scaling of fonts, cex, etc.) constant while bumping up the resolution. – Ben Bolker Oct 05 '12 at 14:36
  • Thanks Dirk, I may have to do this. I've heard about issues with wmf though. What puzzles me is that I can get working eps files out of the GUI, but not programatically. – James Oct 05 '12 at 14:51
  • @BenBolker I was using PNGs before, 4.5 megapixel, which looked ok until zoomed in. The perfectionist in me however isn't too happy with them. Mind you, he's not happy using Word either! – James Oct 05 '12 at 14:54
  • wmf files really struggle with density estimate charts – James Oct 05 '12 at 15:37
  • wfm doesn't support transparency, which is a huge drawback - see above for a nice solution to export plots to native Office format, with support for transparency & with vector based output – Tom Wenseleers Jul 01 '15 at 02:30
1

Word indeed doesn't support EPS very well. A better solution is to export your graphs to Word or Powerpoint directly in native Office format. I just made a new package, export, that does exactly that, see https://cran.r-project.org/web/packages/export/index.html and for demo https://github.com/tomwenseleers/export

Typical syntax is very easy, e.g.:

install.packages("export")
library(export)
library(ggplot2)
qplot(Sepal.Length, Petal.Length, data = iris, color = Species, 
      size = Petal.Width, alpha = I(0.7))     
graph2doc(file="ggplot2_plot.docx", width=6, height=5)
graph2ppt(file="ggplot2_plot.pptx", width=6, height=5) 

Output is vector format and so fully editable after you ungroup your graph in Word or Powerpoint. You can also use it to export statistical output of various R stats objects.

Tom Wenseleers
  • 7,535
  • 7
  • 63
  • 103
0

You can use R studio to knit html files with all of your plots and then open HTML files with Word.

knitr tutorial

jem77bfp
  • 1,270
  • 11
  • 13