Producing a vector graphics image (i.e. metafile) in R suitable for printing in Word 2007
Producing a vector graphics image (i.e. metafile) in R suitable for printing in Word 2007
First a caveat: I posted this question here on SuperUser, but it is clearly the wrong place to ask R questions. I recognize that it is not directly a programming question, but I believe it can be solved by changing how plots are produced (i.e. by coding appropriately). So I hope readers find this appropriate for the forum.
R plots usually consist entirely of vector graphics elements (i.e. points, lines, polygons, text). R permits you to save your figure (or copy-paste) in various formats including various raster formats, as a PDF, or as a Windows meta-file.
I usually save my images as PDFs and print them. This renders the images exactly as I intended them on paper, in the highest quality. I avoid raster formats (e.g. JPG, TIFF) for printing as inevitably the quality is poorer and publishers prefer vector formats.
However, I need to make a large multi-page desktop published document using Microsoft Word 2007, and therefore using PDFs is not an option. When I import my figures from meta-files, or copy and paste directly from R into Word both the screen and print rendering of the image changes slightly (e.g. polygons and their fills become slightly misaligned).
Given that I want to retain high vector quality (and not use raster formats), what can I do to make R vector graphics work with Word? (Of course Sweave and LaTeX would be nice, but again, not a realistic option).
Consider this example:
plot(c(1:100), c(1:100), pch=20) ## Copy and paste to Word 2007 as Windows metafile ## Print ## Quality is poorer (e.g. dot fills misaligned with borders) pdf("printsPerfectly.pdf") plot(c(1:100), c(1:100), pch=20) dev.off() ## Now print PDF ## Quality is as expected EDIT: Further to suggestions by @John I produced it as an EPS postscript file (see below), inserted it as a picture into Word. Because ultimately it will be printed from a PDF created from Word, I converted it to a PDF using default Word 2007 settings, printed it on my HP Laserjet P1606dn laser printer, and then took aphotograph to illustrate the issue of polygons borders and fills misaligning (image on left, below). I also produced it directly as PDF from R using pdf() and printed the PDF and took a photograph (image on right, below).

It may seem like small potatoes! But when you have gone to a lot of trouble to achieve high quality, it is disappointing to be thwarted at the end. In addition, it is not really obvious here, but the numerals are not as high-quality (left) as in the PDF (right), disregarding differences in focus on the photograph.
Answer by John for Producing a vector graphics image (i.e. metafile) in R suitable for printing in Word 2007
Your only option is to use high resolution raster graphics. Once you're over 300 dpi it will be completely indistinguishable from vector printed; it will just make larger files.. Your copy and paste method is coming in at 72 dpi and will look terrible. If you import from a file you can get the resolution in the file and things will be much better. Fortunately Office 2007 is supposed to handle png images, which have the best compression for typical graphs. Let's say you wanted the image 4" wide and 6" high...
png('printsGreat.png', width = 4, height = 6, units = 'in', res = 300) plot(c(1:100), c(1:100), pch=20) dev.off() Also, Office 2007 is supposed to be able to handle EPS files and R postscript files are by default EPS compatible when you print one page.
postscript("printsPerfectly.eps", width = 4, height = 6, horizontal = FALSE, onefile = FALSE) plot(c(1:100), c(1:100), pch=20) dev.off() But if you don't have luck with them go back to the high resolution image.
Answer by Tungurahua for Producing a vector graphics image (i.e. metafile) in R suitable for printing in Word 2007
My preferred solution is to use the windows metafile device for plotting, e.g.:
win.metafile("mygraph.wmf") print(gg1) dev.off() This produces a *.wmf file that can be copy-pasted into the word file.
Answer by Kevin Wright for Producing a vector graphics image (i.e. metafile) in R suitable for printing in Word 2007
The devEMF package seems to produce graphics that look nicer than the default wmf when pasted into PowerPoint.
Answer by Konstantin Popov for Producing a vector graphics image (i.e. metafile) in R suitable for printing in Word 2007
Since I tried to produce png at high res in R and it didn't seem to work on my PC (if I set the resolution higher than, say, 300 dpi, R would produce an error like "cannot start png device"), the way I found was to save the figure using postscript() and then use GSView to convert the ps file into png with 600 dpi resolution. MS Word consumes the png's happily and the quality of print seems to be perfect.
Answer by Tom Wenseleers for Producing a vector graphics image (i.e. metafile) in R suitable for printing in Word 2007
The current best answer above to me is not acceptable, since if one goes to the trouble of making a nice vector based figure, the last thing one would like to do is just rasterize it to a bitmap... Unless it's an increadibly complex graph that takes ages to render in vector format, or something like that, but for most graphs that's not the case.
In fact, the best solution I've seen so far to make R graphs (base R plots, lattice plots or ggplots) work together with Office (Word, Powerpoint) is to export them directly to a Powerpoint using package 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" ) 
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 (using Copy...Paste special...Enhanced metafile), and which unlike the EMFs exported from R also fully supports transparency. For final production you can also readily print it to PDF from Powerpoint if need be, and it will stay nicely in vector format then.
For your simple example this gives us :
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" ) 
Or zoomed in, to show that it's good quality (and editable vector format) :

Importing the PDF of an R graph into Inkscape, and then exporting it from there to an enhanced metafile also results in better looking figures than when one uses the direct export to EMF from R (either natively or using devEMF), but it's not as good as the solution above, and still buggy.
The second best solution would be to export to EMF using R package devEMF. In this case this gives us
library(devEMF) emf(file="example.emf", bg="white", width=12, height=8, family="Calibri", pointsize=20) plot(c(1:100), c(1:100), pch=20) dev.off() but on-screen rendering in powerpoint e.g. is not as good, as it renders non-anti-aliased (here zoomed in to show this) and transparency also isn't supported : 
Right clicking on the image and selecting Ungroup converts it to native Office DrawingML format, which will then render anti-aliased, but the placement of the text labels is slightly messed up then (solved in this case by selecting and centering all text) : 
So the solution I gave above is clearly better.
EDIT: I now made a [small package export] with helper functions graph2ppt and graph2doc to export the currently active graph to Powerpoint or Word as well as table2doc, table2ppt and table2html to export the previously shown R stats object to a Word, Powerpoint or HTML table, e.g. :
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/export',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 bugs in there let me know!
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:
Post a Comment