Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R eps export and import into Word 2010

Tags:

r

ms-word

ggplot2

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.

like image 285
James Avatar asked Oct 05 '12 13:10

James


People also ask

Can you import EPS files into Word?

No. For Microsoft 365, Office 2019, and future releases of Office, there is no way to use EPS files in Office documents.


2 Answers

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

like image 170
IRTFM Avatar answered Sep 28 '22 10:09

IRTFM


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()
like image 20
Marco Sandri Avatar answered Sep 28 '22 10:09

Marco Sandri