7

I am wanting to export an R produced figure to Word. The figure contains transparency (alpha channel). Below is some example code - when exported to Windows metafile it throws an error:

Warning message: In plot.xy(xy, type, ...) : semi-transparency is not supported on this device: reported only once per page

Exporting to SVG produces the desired result, but this image format is not supported by MS Office. Is there a way around this? What image type could I use while retaining the alpha channel? PNG is possible, but this doesn't produce very crisp graphics - it loses the clear vectorized image.

# Get some colours with transparency (alpha = 0.6)
col.dot <- rainbow(5, alpha = .6)

# Save to svg file - OK
svg("test_fig.svg")
plot(1:5,col = col.dot, pch=15)
dev.off()

# Save to wmf - warning "semi-transparency is not supported on this device..."
win.metafile("test_fig.wmf")
plot(1:5,col = col.dot, pch=15)
dev.off()

I should add, this is on a Windows system (Windows 8 64 bit, with Word 2013)

Tom Evans
  • 553
  • 2
  • 6
  • 13
  • 1
    Check out some of the options here ... http://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/png.html – Jean V. Adams Nov 08 '13 at 22:07
  • Below also a potentially better and more modern solution involving direct export to Powerpoint, resulting in a fully editable graph that also retains transparency - you might like to check that one as the correct answer... – Tom Wenseleers Jun 26 '15 at 17:02

2 Answers2

6

I just made a new package export to easily export R graphs to Office (Word, Powerpoint), see https://cran.r-project.org/web/packages/export/index.html and for demo https://github.com/tomwenseleers/export.

Typical syntax is very easy, e.g.:

install.packages("export")
library(export)
library(ggplot2)
qplot(Sepal.Length, Petal.Length, data = iris, color = Species, 
      size = Petal.Width, alpha = I(0.7))     
graph2ppt(file="ggplot2_plot.pptx", width=6, height=5) 

Output is vector format and so fully editable after you ungroup your graph in Powerpoint. You can also use it to export to Word, Excel, Latex or HTML and you can also use it to export statistical output of various R stats objects.

enter image description here

This results in a fully editable, high quality Powerpoint graph in native Office vector-based DrawingML format, which you can also readily copy & paste as enhanced metafile if you like, and which unlike the EMFs exported from R also fully supports transparency.

Tom Wenseleers
  • 7,535
  • 7
  • 63
  • 103
2

From the help of win.metafile:

There is support for semi-transparent colours of lines, fills and text on the screen devices. These work for saving (from the ‘File’ menu) to PDF, PNG, BMP, JPEG and TIFF, but will be ignored if saving to Metafile and PostScript.

So you cannot use transparency in a metafile. You can try saving as png and increasing the resolution of the output.

Ricardo Oliveros-Ramos
  • 4,322
  • 2
  • 25
  • 42
  • Thanks for the suggestion. The problem I had with increasing the resolution of the png was that it leads to a larger figure with the same thickness lines etc, thus if displayed at the same size symbols and lines will appear too small, unless also enlarged. I think the best solution may be to export to SVG then make later file conversion with another application, such as 'UniConvertor' as suggested by @IShouldBuyABoat above. – Tom Evans Jan 07 '14 at 11:57
  • 1
    You can fix that by increasing the size of the figure, and also changing the "res" parameter. By default, res=72, try increasing to res=300 or something. I do it all the time. See the help of png for more information, you don't need to change lwd or stuff. – Ricardo Oliveros-Ramos Jan 07 '14 at 11:59
  • Thanks for suggestion. I didn't realise that you could change the resolution for PNG files output. I have accepted your answer as it is the closest to doing what I'd like, still wish it was possible for meta-files though ;) – Tom Evans Jan 08 '14 at 13:27