# In R, what is the best graphics driver for using the graphs in Microsoft Word?

I use R to make neat graphs that I use in Microsoft Office documents. According to this page, the best quality is obtained with the PDF driver. Unfortunately, Word doesn't support importing PDF figures. What should I use?

-
You can put PDF figures into Word. Use "Insert object". Side note: If you've never used LaTeX before, I highly recommend trying it as an alternative to MS Word. – mark999 Jan 20 '12 at 11:06
@mark999: Thanks for your suggestion. I tried it, but unfortunately I could not get anything better than a pdf icon in the document. I have to use Word in this context because I work with other people who use Word. LaTeX is nice, but it's not for every project or everyone. – static_rtti Jan 20 '12 at 13:26
@mark999 LaTeX simply is not an option for collaborating with non-mathematical people. As Gavin Simpson explains, high-quality graphics can be incorporated within Word documents by choosing appropriate formats. – whuber Jan 20 '12 at 14:26
@mark999 Sadly I have to agree with whuber. LaTeX isn't really an option outside of a few fields, and even ones that are fairly math intensive still use Word as the default. I considered it for awhile, then dropped because it was more trouble than it was worth for collaborating. – EpiGrad Jan 20 '12 at 15:33
@whuber, I agree with you, and EpiGrad I partly agree with you. I probably do 90% of my writing in LaTeX and the rest in MS Word (I only use Word when I don't have a choice). static_rtti: I don't know what's going wrong for you, because I have put pdf files into Word documents without any problems. – mark999 Jan 20 '12 at 22:06
show 1 more comment

If you are working on a PC, it is quite convenient to insert figures into Office documents in emf format via the clipboard.

emf is a windows native vector graphics format and it allows you to edit the figures with MS Office tools. It's not perfect but it's a quick way of generating a simple report.

If your documents are at all complicated, MS Word handling of figures is horrible.

-

Depends on how you will be using your Word documents and what types of figures you wish to include in the document.

If the figures are standard R plots with a moderate number of points/data, then vector-based formats will provide the best reproduction and allow for easy rescaling of image size whilst having small filesize. EPS via postscript(...., onefile = FALSE, paper = "special") in such cases would be the best format if you're outputting to a PDF or printing on a laser printer that knows postscript. Unfortunately, Word uses a third-party plugin to load EPS figures which produces a crappy low-res bitmap preview which is displayed on screen - when printed you get the high quality EPS though.

If the plots could be considered rasters (surface plots or image plots [heatmaps] with a high number of "pixels"), or if the plots contain a very large number of points, retaining the vector information for such plots will result in a large file size and high processing costs to load into Word and store in memory as you are working on the document. In such cases, I would use a high-res TIFF or a PNG via tiff() or png() respectively. See the help files for assistance in setting the size of the image in pixels and the resolution. These devices will render the figure as a bitmap image, which if given sufficient size/resolution will tolerate some amount of rescaling but will tend to be far more efficiently stored when the number of data points or cells is very large.

Do consider also that not all devices support transparency, largely because the underlying file formats do not allow it; postscript() doesn't support transparency, pdf() and png() do. cairo_ps() does support transparency but in doing so will actually produce a bitmap image in EPS format, which might not be what you want.

Do note that if saving your document out to PDF, bitmap figures will be downsampled to some degree. I forget how Word controls this, but it had two settings in the Save dialog when PDF type was selected and one of those will downsample the images to produce a smaller file size. This will be associated with some loss of quality so be careful which option you choose. This is not specific to Word - OpenOffice.org and LibreOffice have the same features, but they offer far finer grained control of how the images are compressed and whether they are compressed or not. This arises because PDF was designed for both on screen and on printer reproduction. You don't need the high resolution in images if displaying on the screen for the web. Higher res images result in larger file sizes, so the intended destination of the PDF could be set, which affected how the PDF was created.

-
Thanks, good tip. postscript seems to speak nicely to Word for printing. Unfortunately the poor preview on the screen is a non-trivial problem. In my environment a lot of Word documents are sent around and read on screen. – Peter Ellis Jan 25 '12 at 23:29

If you are on Windows try win.metafile. It is vector format, if I remember correctly, and it plays nicely with Word.

-

R graphics in windows can be pretty tricky. The biggest problem that I have is that anti-aliasing is not working properly in Windows. For some reason I need to use the Cairo device plugin to get it nicely formatted.

I've tried both Cairo and cairoDevice package and currently I find the cairoDevice to be the easiest and most reliable, there is a StackOverflow post about this.

To use the cairoDevice package all you need is to:

# Create the device
Cairo_png(filename="my_file_name.png",
width=16,
height=16,
pointsize=18)
# Do a plot
plot(x, y)
# Saves the file
dev.off()


You can also save in PDF/svg-format and then use Inkscape to edit your graph, change fonts etc and then export it to png format.

-

PDF, ps,SVG, or eps are vector based graphics device. JPG, TIFF,PNG,...etc are Raster graphics. When it comes raster graphics, the trade off is between the size and quality. By adjusting width,height and other formatting you could keep high quality in raster graphics. You could take PNG or JPG format to insert the image.

SVG images may insert in MSWORD document. You can check this.

-
 Note that R's svg device sometimes gives you raster when you don't expect it. – Thomas Levine Jan 24 '12 at 19:37

The easiest way migth be chosing .png files so did I. Yet word does show them quite blurred - at least in my case. Back when i was using Eviews i tried .epswhich resulted in an own set of problems but the quality was better. I think in the end you have to chose between the lesser of two evils.

-
 I think the blur is due to the Word scaling algorithm. You may have better results resizing in an image editing program before inserting so it is displayed at 100% by Word. – James Jan 20 '12 at 16:20

This does not directly answer your question, but you can get better integration between R and MS Word using either the R package R2wd or the commercial software Inference for R. These provide Sweave-like direct integration of R and MS Word, eliminating cut-and-paste or save-and-load, and making it easier to update plots in the document from the command line. I have not used these but I do use Sweave and recommend literate programming, which is supported by all of these tools.

Sweave is the most widely used literate programmiing tool among R users. It is LaTeX + R, and is how R vignettes and many books and journal articles are written.

The Word + R tools promise a less daunting interface for new users. LaTeX has a steeper learning curve than Word.

However, at \$199/yr, inference for R makes it challenging for others to reproduce your analyses, unlike Sweave, which is part of base R.

-