1

I do not know if this belongs here. However, I hope that the question fits in here and that someone can give a hint as to its solution.

I like R graphics. When preparing a presentation or thelike, I love the option to just create some "fancy" graphs and import these as "Metafile" into Powerpoint. However, there is one major draback with this method: Boxplots created with R and saved as "Metafile" are corrupted whenever I try to store any Microsoft Office Document as PDF. I had this issue on several Computers and couldn't find no solution but to use other formats like .bmp. However, I'd really appreciate to use the Metafile option.

I create a Boxplot in RStudio:

boxplot(mtcars$disp)

then I use the export function --> copy to clipboard. Here, I can choose between "bitmap" and "metafile". I'd prefer to use "metafile" here. Then I paste the image in Powerpoint. The last step is to save the pp-presentation as a PDF.

The following images are copied with the snipping tool from the final pdf:

enter image description here

this is the boxplot copied to clipboard in RStudio as bitmap.

enter image description here

This is the Boxplot copied to clipboard in RStudio as metafile. Is there any suggestion of how to avoid the nasty lines?

yenats
  • 531
  • 1
  • 3
  • 16
  • 1
    this would benefit from a little more reproducibility. For instance, generation of this plot (or one based on `mtcars`, for instance) would be nice, but then your exact steps for "printing to PDF" and then how to import a PDF as a metafile-image in powerpoint ... that last point is new to me, perhaps I've been missing a big piece all these years. – r2evans Sep 06 '18 at 16:50
  • I updated the question. Hopefully, its clearer now. And no, I don't think you've missed anything - my question was just very unprecise. – yenats Sep 06 '18 at 17:10
  • That's a good update ... and what I feared. I've never had really good success with windows R metafile to office functionality. I had better luck at one point with [`devEMF`](https://cran.r-project.org/web/packages/devEMF/index.html), though I haven't been using it recently (preferring RMarkdown to LaTeX/PDF for reports). When I need to go to powerpoint, I either accept the granular pixelation or use `png(..., res=300)` or similar to improve on the default resolution of 72. Not an answer, sorry. – r2evans Sep 06 '18 at 17:38
  • @yenats Have you tried ungrouping the metafile graphic after inserting it into PowerPoint. That might allow you to do a little editing in PPT to clean it up. – Steve Rindsberg Sep 07 '18 at 14:55
  • @SteveRindsberg, thank you - for a really quick and dirty work-around, this helps. However, the legends are partly moved around when ungrouping the graphics. Another solution would be to export pp-graphs like described by Tom Wenseleers here https://stackoverflow.com/questions/9555889/producing-a-vector-graphics-image-i-e-metafile-in-r-suitable-for-printing-in/31073189#comment91374518_31073189. Unfortunately, the package he put out doesn't work anymore due to java compability issues. Perhaps, there is something similar out there to export pp-graphs? – yenats Sep 09 '18 at 17:20
  • @yenats "Perhaps, there is something similar out there to export pp-graphs?" I'm not an R user so I'm not familiar with what's available. Sorry ... – Steve Rindsberg Sep 09 '18 at 20:53
  • Added a solution below using my new export package to export R graphs in native Powerpoint format without any issues. It's built on top of the officer package... – Tom Wenseleers Nov 04 '18 at 00:50
  • Does this answer your question? [Weird lines appearing in the R graph](https://stackoverflow.com/questions/28927205/weird-lines-appearing-in-the-r-graph) – oberlies Mar 16 '22 at 14:38

1 Answers1

1

There's a new package export that just came out on CRAN that allows you to export graphs to Powerpoint or Word in native Office format. From there you can then save as PDF in Office without problems, without any weird lines appearing, see https://cran.r-project.org/web/packages/export/index.html and https://github.com/tomwenseleers/export

E.g.

install.packages("export")
library(export)
boxplot(count ~ spray, data = InsectSprays, las = 2)
graph2doc(file="plot.docx", width=7, height=5)
graph2ppt(file="plot.pptx", width=7, height=5)

Even after saving to PDF within Powerpoint this will give you a perfect quality vector format PDF without any weird lines :

enter image description here

Other advantage is that the Powerpoint version you get is fully editable vector format, enabling you to make any small required changes in the layout (it also fully supports transparency / alpha channels).

Tom Wenseleers
  • 7,535
  • 7
  • 63
  • 103
  • whoop whoop. This seems to be a VERY useful new package. Thank you for the Answer! – yenats Nov 05 '18 at 11:33
  • I detected a problem with the graph2doc - function, described here: https://stackoverflow.com/questions/58591852/exportgraph2office-moves-axis-labels-around perhaps you are interested in having a look at it? – yenats Oct 28 '19 at 15:33