10
$\begingroup$

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?

$\endgroup$
  • 1
    $\begingroup$ 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. $\endgroup$ – mark999 Jan 20 '12 at 11:06
  • 1
    $\begingroup$ @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. $\endgroup$ – static_rtti Jan 20 '12 at 13:26
  • 4
    $\begingroup$ @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. $\endgroup$ – whuber Jan 20 '12 at 14:26
  • 3
    $\begingroup$ @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. $\endgroup$ – Fomite Jan 20 '12 at 15:33
  • 1
    $\begingroup$ @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. $\endgroup$ – mark999 Jan 20 '12 at 22:06
4
$\begingroup$

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.

| cite | improve this answer | |
$\endgroup$
12
$\begingroup$

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.

| cite | improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ 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. $\endgroup$ – Peter Ellis Jan 25 '12 at 23:29
7
$\begingroup$

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

| cite | improve this answer | |
$\endgroup$
5
$\begingroup$

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.

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ Yes. A Cairo png device is what I've found produces the nicest looking plots for on-screen viewing in a Word doc. On machines for which capabilities("cairo") is TRUE, you can alternatively just use png(..., type="cairo") or perhaps something like png(..., type="cairo", res=200) to get similar output. $\endgroup$ – Josh O'Brien Mar 16 '16 at 3:23
  • 1
    $\begingroup$ @JoshO'Brien - Good point. I've since my original answer written two blog posts on this issue: gforge.se/2013/02/exporting-nice-plots-in-r and gforge.se/2013/03/exporting-plain-lattice-or-ggplot $\endgroup$ – Max Gordon Mar 17 '16 at 18:23
  • $\begingroup$ Super, thanks for the links. Somehow I had missed the units="in" option until I read them. Wish I'd known about that a year or two or 10 ago! Since yours is the best answer here, it might be worth editing it, or adding an update up top about the type="cairo" option with the built-in device -- especially given its advantages even relative to the Cairo package functions... $\endgroup$ – Josh O'Brien Mar 17 '16 at 18:48
3
$\begingroup$

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.

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ Note that R's svg device sometimes gives you raster when you don't expect it. $\endgroup$ – Thomas Levine Jan 24 '12 at 19:37
2
$\begingroup$

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.

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – James Jan 20 '12 at 16:20
  • $\begingroup$ or create the png file at the desired size in the first place. The defaults are width = 480, height = 480, units = "px", pointsize = 12, but you can change these in your R script $\endgroup$ – Henry Sep 26 '15 at 14:08
2
$\begingroup$

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.

| cite | improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.