3

I can produce and save in pdf a ggplot figure in .pdf, including semi-transparency (alpha):

library(ggplot2)
library(cowplot)
p <- qplot(Sepal.Length, Petal.Length, data = iris, color = Species, size = Petal.Width, alpha = 0.5)
save_plot("plot.pdf",p)

enter image description here

The figure is perfectly displayed in my laptop (Mac), both as an individual figure in pdf and embedded in a word document. However, I've painfully experienced over the years that semi-transparency in my figures is usually not displayed in others' computers, commonly when they see them embedded in a Microsoft office document. This is particularly painful when I have to make a presentation in front of an audience, and suddenly the figures of my Powerpoint presentation in the conference room's computer do not show anything, just a blank plot. This is also problematic if I submit a paper to a journal and I get an email from the editor saying my figures are not displaying anything in the Word document. As the figures look ok in my Mac, I don't have a way to find out if the figures are correct, except by explicitly asking someone else to review the documents included the figures.

I have tried a couple of workarounds I've found online, e.g.:

save_plot("plot.pdf",p, useDingbats=F)

Or

save_plot("plot.pdf",p, device = cairo_pdf, fallback_resolution = 1200

But I still get emails from collaborators saying they can't see the figures, and still experience some embarrassed moments in presentations when I can't make my point because figures are not showing anything.

How on Earth can I save my figures in pdf without loosing information? At this point I don't know if this issue is related to the way the figures are constructed in R, or an issue of Microsoft Office dealing with pdf figures with semi-transparency. The truth is that I don't see anybody else in my research network experiencing these issues.

FYI

> capabilities()
       jpeg         png        tiff       tcltk         X11        aqua    http/ftp 
       TRUE        TRUE        TRUE        TRUE        TRUE        TRUE        TRUE 
    sockets      libxml        fifo      cledit       iconv         NLS     profmem 
       TRUE        TRUE        TRUE        TRUE        TRUE        TRUE        TRUE 
      cairo         ICU long.double     libcurl 
       TRUE        TRUE        TRUE        TRUE  
fede_luppi
  • 1,063
  • 4
  • 17
  • 29
  • Maybe try `ggplot2::ggsave()`? But, `save_plot()` calls it under the hood, so might not make much of a difference. Or try with EPS with rasterized transparency via `cairo_ps()`. `ggsave("plot.eps", p, device=cairo_ps, fallback_resolution = 1200)` Admittedly, not an ideal solution. – Jav Mar 05 '19 at 14:50
  • Quick test of Word document on Mac to Android shows that only EPS version works. – Jav Mar 05 '19 at 14:56
  • Can't you use `svg` and export to svg rather than pdf ? edit: this answer suggests `postscript` for compatibility with Word: https://stackoverflow.com/questions/9555889/producing-a-vector-graphics-image-i-e-metafile-in-r-suitable-for-printing-in – moodymudskipper Mar 05 '19 at 15:59
  • see also answer using `graph2doc` – moodymudskipper Mar 05 '19 at 16:06

2 Answers2

8

The problem is not related to R or RStudio.

Problem Explanation:

The issue is related to the process of creation and conversion of embedded images in Microsoft Word documents on Mac OS X, and how the document is then rendered by Microsoft Word on Windows.

Why? Essentially, PDF is a native component of Apple's drawing system Quartz. The Mac OS X Quartz's internal imaging model correlates well with the PDF object graph, making it easy to display and manipulate pdf embedded images.

Alas, this is not the case with the Windows operating system. [This is not a critique merely an observation.] The pdf engine is not included within Windows, it is an add on. Thus a Mac OS X Word Document will not be correctly rendered in Microsoft Word for Windows. Further to this, there appears to be no usable option to have an OLE Link to the PDF.

Example:

In Mac OS X it is possible to simply drag and drop a pdf image into a word document. The image displayed will be shown as:

enter image description here

now if we go to a Windows 10 PC and open the document we will see:

enter image description here

Work Around:

The recommended work around is to save the images as .png or .jpg and insert these into the word document.

library(ggplot2)
library(cowplot)
p <- qplot(Sepal.Length, Petal.Length, data = iris, color = Species, size = Petal.Width, alpha = 0.5)
save_plot("plot.pdf",p)
save_plot("plot.png",p)
save_plot("plot.jpg",p)

Result:

Microsoft Word on Windows 10

enter image description here

Addendum Requested - EPS

The other option considered is to use

ggsave("plot.eps", p, device=cairo_ps, fallback_resolution = 1200)

this does work, however, striations were noted in the rendered images on Windows 10.

EPS Implicit Conversion to EMF due to Security Attack Vector:

The striations appear to be related to an implicit conversation occurring, in that Word converts EPS images to EMF format due to a security restriction.

https://support.office.com/en-us/article/support-for-eps-images-has-been-turned-off-in-office-a069d664-4bcf-415e-a1b5-cbb0c334a840

Thus, you can insert an EPS image, but it appears to be converted to EMF to block EPS Embedded script attack vectors.

Example:

enter image description here

Technophobe01
  • 8,212
  • 3
  • 32
  • 59
  • This is what I was suspecting after some tests. Thanks! Would I encounter the same issue if I save my figures in .eps and embed them in a Word document? ggsave("plot.eps", p, device=cairo_ps, fallback_resolution = 1200) – fede_luppi Mar 07 '19 at 06:59
  • @fede_luppi Microsoft Word appears to perform an implicit conversion of EPS to EMF format to address an EPS embedded script attack vector. See updated answer. Take care. – Technophobe01 Mar 07 '19 at 16:28
0

Does the output file have to be a PDF? Can you not just save the graph directly as a different filetype which does support transparency, such as .png, and insert that inside your presentations or Word files?

I have had formatting problems with inserting PDF images in Word before, with or without transparency.

Phil D
  • 183
  • 10
  • 1
    1) I like the look of vector figures, and 2) Journals explicitly request vector format figures. I could submit manuscripts at the initial stage with .png embedded in the document, with pdf versions included separately in the final stage. But .png figures with an adequate quality would make the document too heavy and so more difficult to handle. I am open to an alternative workflow though. – fede_luppi Mar 05 '19 at 10:59