Tom Wenseleers
2015-Jul-04 17:17 UTC
[Rd] Support for transparency in metafile export & support for export to Powerpoint
Dear all, Further to my previous message I now made a one-line convencience function to export your currently active graphics window/plot to either Word or Powerpoint in Office-native vector-based DrawingML format using either export2ppt(file="plot.pptx") or export2doc(file="plot.docx") : see http://stackoverflow.com/questions/31212659/r-function-to-capture-r-plot-in-current-graphics-device-and-export-it-to-powerp/31221813#31221813 (analogous in syntax to function dev.copy2pdf in grDevices) The code of the function is: export2office = function(file = "plot", type="PPT", scaling = 90, aspectr=NULL, vector.graphic = TRUE, fontname = "Arial", pointsize=20) { file=sub("^(.*)[.].*", "\\1", file) if (type=="PPT"|type=="PPTX") {ext=".pptx";type="PPT"} else {ext=".docx";type="DOC"} require(ReporteRs) captureplot = function() {p = invisible(recordPlot()) dev.copy() return(p)} p = captureplot() plotsize = dev.size() plotaspectr = plotsize[[1]]/plotsize[[2]] if (!is.null(aspectr)) plotaspectr=aspectr myplot=function(pl=p) print(pl) if (type=="PPT") {doc = pptx();doc = addSlide(doc, slide.layout = "Blank");pagesize = dim(doc)$slide.dim} else {doc = docx();pagesize = dim(doc)$page-dim(doc)$margins[c(4,3)]} pageaspectr = pagesize["width"]/pagesize["height"] if (pageaspectr>plotaspectr) {xf=plotaspectr/pageaspectr;yf=1} else {xf=1;yf=pageaspectr/plotaspectr} w = (scaling/100)*pagesize["width"]*xf; h = (scaling/100)*pagesize["height"]*yf if (type=="PPT") {doc = addPlot( doc, myplot, vector.graphic = vector.graphic, fontname = fontname, pointsize = pointsize, offx = (pagesize["width"]-w)/2, offy = (pagesize["height"]-h)/2, width = w, height = h) } else {doc = addPlot( doc, myplot, vector.graphic = vector.graphic, fontname = fontname, pointsize = pointsize, width = w, height = h)} writeDoc( doc, paste0(file,ext) ) } export2ppt = function(type="PPT", ...) export2office(type=type,...) export2doc = function(type="DOC", ...) export2office(type=type,...) # Examples: require(ggplot2) qplot(Sepal.Length, Petal.Length, data = iris, color = Species, size = Petal.Width, alpha = I(0.7)) export2ppt(file="plot.pptx") export2ppt(file="plot.pptx",aspectr=1.7,fontname="Times New Roman") heatmap(as.matrix(eurodist)) export2ppt(file="heatmap.pptx") In Powerpoint you can then right click on the graph and Ungroup it, thereby allowing you to make minor changes to the layout if need be, before saving it as PDF from PPT. The quality is much better than what you get if you try to do the editing in the PDF version using Inkscape. It works with ggplot2 and lattice plots as well as base R plots and also fully supports transparency (unlike e.g. EPS or EMF export in R - EMF in the meantime I found out does not support transparency at all, and can only deal with it by rasterizing all semi-tranparent graphics elements). Given the widespread use of Office/LibreOffice/OpenOffice I think it would be very handy if this kind of functionality were provided as part of base R at one stage or another (as would Excel import and export, for that matter). So if anyone on this list thinks it would be a good idea to incorporate this function in grDevices or something, please do! (would be handy e.g. if powerpoint export also showed in the File...Save as... interactive graphics devices, like windows() ) Otherwise I'll be in touch with the ReporteRs author to try to convince him to add it there. cheers, Tom [[alternative HTML version deleted]]