Export image from R to word with alpha channel (transparency)

I want to export an R shape to Word. The figure contains transparency (alpha channel). The following is an example code - when exporting to a metafile on Windows, it throws an error:

Warning message: In plot.xy (xy, type, ...): translucency is not supported on this device: it is displayed only once on the page

Export to SVG gives the desired result, but this image format is not supported by MS Office. Is there any way around this? What type of image could I use while preserving the alpha channel? PNG is possible, but it does not give very clear graphics - it loses a clear vector 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 have to add that this is on a Windows system (Windows 8 64 bit, from Word 2013)

+7
r graphics
source share
2 answers

the best solution I've seen so far is that R charts work together with Office (Word, Powerpoint) to export directly to Powerpoint using ReporteRs , as in

 library( ReporteRs ) require( ggplot2 ) mydoc = pptx( ) mydoc = addSlide( mydoc, slide.layout = "Title and Content" ) mydoc = addTitle( mydoc, "Plot examples" ) myplot = qplot(Sepal.Length, Petal.Length , data = iris, color = Species , size = Petal.Width, alpha = I(0.7) ) mydoc = addPlot( mydoc, function( ) print( myplot ), vector.graphic=TRUE) writeDoc( mydoc, file = "test plot.pptx" ) 

enter image description here

This results in a fully editable, high-power Powerpoint graphic in Office-based native DrawingML format, which you can also easily copy and paste as an extended metafile if you want, and which, unlike EMFs exported from R, also fully supports transparency.

For a simple base line R, the syntax will be

 library( ReporteRs ) mydoc = pptx( ) mydoc = addSlide( mydoc, slide.layout = "Title and Content" ) mydoc = addTitle( mydoc, "" ) myplot = function( ) {return(plot(c(1:100), c(1:100), pch=20))} mydoc = addPlot( mydoc, fun=myplot, vector.graphic=TRUE, offx=0,offy=0,width=12, height=8, fontname="Calibri", pointsize=20) writeDoc( mydoc, file = "test plot2.pptx" ) 

enter image description here

basic R-graphics, lattice graphics and ggplots are supported.

EDIT: now I made a small export package with graph2ppt and graph2doc helper functions to export the current active graph for Powerpoint or Word, as well as table2doc , table2ppt and table2html to export the previously specified R-statistics object to a Word, Powerpoint or HTML table, for example

 install.packages("rJava") install.packages("ReporteRs") install.packages("ReporteRsjars") install.packages("ggplot2") install.packages("rtable") install.packages("xtable") install.packages("taRifx") install.packages("devtools") library(devtools) devtools::install_github('tomwenseleers/export2office',local=F) library(export) ?graph2ppt ?table2doc ## export of ggplot2 plot library(ggplot2) qplot(Sepal.Length, Petal.Length, data = iris, color = Species, size = Petal.Width, alpha = I(0.7)) graph2ppt(file="ggplot2 plot.pptx", aspectr=1.7) # add 2nd slide with same graph in different aspect ratio graph2ppt(file="ggplot2 plot.pptx", aspectr=1.3, append=T) # add 3d slide with same graph with fixed width & height graph2ppt(file="ggplot2 plot.pptx", width=6, height=5, append=T) # export of aov Anova output fit=aov(yield ~ block + N * P + K, npk) summary(fit) table2doc(file="table_aov.docx") summary(fit) table2doc(file="table_aov.docx",append=T,digits=4) summary(fit) table2doc(file="table_aov.docx",append=T,digits=4,digitspvals=1) summary(fit) table2html(file="table_aov.html") 

If anyone finds any errors, let me know!

+5
source share

Using win.metafile :

There is support for translucent colors of lines, fills and text on the device screen. They work to save (from the File menu) PDF, PNG, BMP, JPEG and TIFF, but will be ignored when saving Metafile and PostScript.

Therefore, you cannot use transparency in the metafile. You can try to save as png and increase the resolution of the output.

+2
source share

All Articles