R tips and tricks – Paste a plot from R to a word file

In this post you will learn how to properly paste an R plot\chart\image to a word file. There are few typical problems that occur when people try to do that. Below you can find a simple, clean and repeatable solution.
When you google how to paste a plot from R to a word file you find that there are some solutions. But they are not satisfactory. For example, stackoverflow highest-ranking reply offers to use the Rstudio button to export your plot as an Enhanced Metafile (EMF) format. Couple of things wrong with it: the first is that you need to start messing around with the device scaling, because the export remembers the port dimensions. The second is that the word file is often not the final version. For better readability\representation we often convert the word to a pdf format before sending\publishing. But then you get something funny which you may have seen before, and drove some people insane consumed much of some people’s time, myself included:
Ugly chart after converting to PDF

Without further ado, here is what you should do. Install the EMF R package(Philip Johnson). The package elegantly has the one function: emf, which works the way you think it should:

We are almost there.

The above code creates an EMF file (called EMF_plot.emf in this case) of your plot in the working directory. You can specify the width and height of the graphics region as you do for any other graphic devices; very handy when you need to produce similar charts in bulk. Now instead of playing around with the scaling inside Rstudio, I suggest to quickly move to word file and do the work there. This 24 seconds video shows how to do that:

The eventual PDF then looks as it should, not blurry as it does when pasting a PNG for example, and without those ugly diagonal grey lines you saw before. So suitable for corporate environment where often word format, rather than latex or markdown, is the starting point for collaboration.

One comment on “R tips and tricks – Paste a plot from R to a word file”

  1. Also succeeds as expected for an emf file pulled into LibreOffice on an Ubuntu box running 18.04 LTS. Thanks for the heads up on this package.

Leave a Reply to David Winsemius

Your email address will not be published. Required fields are marked *