Mahmoud K. Okasha
2004-Feb-20 15:54 UTC
[R] R: Including R plots in a Microsoft Word document
Greetings List, I am conducting some large simulations using R. As a result, I get many plots but I'm having some trouble with including some of them in a Microsoft Word document. Can any one tell me the easiest method of having copies of the R-graphs in the Word documents? Best regards Mahmoud [[alternative HTML version deleted]]
Gabor Grothendieck
2004-Feb-20 16:19 UTC
[R] R: Including R plots in a Microsoft Word document
Right click on the plot and choose copy as metafile. Copying it as a metafile copies it as a vector graphic rather than as a bitmapped graphic so the elements of the graphic (e.g. axis labels, points on the plot) stay intact allowing later editing in Word. In your Word document move to the point where you want to the plot and press ctrl-V (or Edit | Paste). In Word you can right click the plot and choose Edit in which case you will be thrown into an editor and can edit the elements the graphic such as the axis labels, etc. To do that, right click on the plot in Word, choose Edit and now you can edit it. For example, click on an axis label and then change its text, its font, etc. in the usual way. --- Date: Fri, 20 Feb 2004 17:54:33 +0200 From: Mahmoud K. Okasha <m.okasha at palnet.com> To: <r-help at stat.math.ethz.ch> Subject: [R] R: Including R plots in a Microsoft Word document Greetings List, I am conducting some large simulations using R. As a result, I get many plots but I'm having some trouble with including some of them in a Microsoft Word document. Can any one tell me the easiest method of having copies of the R-graphs in the Word documents? Best regards Mahmoud --- On Fri 02/20, Mahmoud K. Okasha < m.okasha at palnet.com > wrote: From: Mahmoud K. Okasha [mailto: m.okasha at palnet.com] To: r-help at stat.math.ethz.ch Date: Fri, 20 Feb 2004 17:54:33 +0200 Subject: [R] R: Including R plots in a Microsoft Word document Greetings List,<br><br>I am conducting some large simulations using R. As a result, I get many plots but I'm having some trouble with including some of them in a Microsoft Word document. Can any one tell me the easiest method of having copies of the R-graphs in the Word documents?<br><br>Best regards<br>Mahmoud<br> [[alternative HTML version deleted]]<br><br>______________________________________________<br>R-help at stat.math.ethz.ch mailing list<br>https://www.stat.math.ethz.ch/mailman/listinfo/r-help<br>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html<br>
Dear Mahmoud, There are several ways to do this. I find the following the simplest: Right click on the graphics-device window in R and select "Copy as metafile" from the pop-up menu. Then right-click in the Word document where you want the graph to appear and select "Paste." Perhaps if you could indicate what trouble you're experiencing, there would be something more to say. That's it. John At 05:54 PM 2/20/2004 +0200, Mahmoud K. Okasha wrote:>Greetings List, > >I am conducting some large simulations using R. As a result, I get many >plots but I'm having some trouble with including some of them in a >Microsoft Word document. Can any one tell me the easiest method of having >copies of the R-graphs in the Word documents?----------------------------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario, Canada L8S 4M4 email: jfox at mcmaster.ca phone: 905-525-9140x23604 web: www.socsci.mcmaster.ca/jfox
uaca@alumni.uv.es
2004-Feb-20 16:36 UTC
[R] R: Including R plots in a Microsoft Word document
On Fri, Feb 20, 2004 at 05:54:33PM +0200, Mahmoud K. Okasha wrote:> Greetings List, > > I am conducting some large simulations using R. As a result, I get many plots but I'm having some trouble with including some of them in a Microsoft Word document. Can any one tell me the easiest method of having copies of the R-graphs in the Word documents?R can produce at least PostScript, PDF, png, jpeg/jpg see: help(postscript) help(pdf) help(png) help(jpeg) I don't use word, for me the PostScript format (more precisely Encapsulated PostScript/.eps) is the best/more easy/powerful format if you don't have thousands of points or lines :-) por instance, to print a simple plot: postscript(file="somefile.eps"); plot(whatever); dev.off(); <<---- Important other formats are similar regards Ulisses Debian GNU/Linux: a dream come true ----------------------------------------------------------------------------- "Computers are useless. They can only give answers." Pablo Picasso Humans are slow, innaccurate, and brilliant. Computers are fast, acurrate, and dumb. Together they are unbeatable ---> Visita http://www.valux.org/ para saber acerca de la <--- ---> Asociaci?n Valenciana de Usuarios de Linux <---
On Fri, 2004-02-20 at 09:54, Mahmoud K. Okasha wrote:> Greetings List, > > I am conducting some large simulations using R. As a result, I get > many plots but I'm having some trouble with including some of them in > a Microsoft Word document. Can any one tell me the easiest method of > having copies of the R-graphs in the Word documents? > > Best regards > MahmoudA couple of different ways: 1. If you actually need to see the graphics within the document and/or send the .doc file to someone who needs to be able to see the plots as they appear, then you should use Windows Metafile format images. Since these are vector format files, you can resize them as required on your pages. Bitmapped images (ie .BMP/.PNG) will distort as you resize them. You can generate these by plotting directly into an R plot window and then copy (ie. right mouse click) and paste into the Word document using the Windows clipboard, or generate the plot files directly using the win.metafile() function. 2. If you will be generating hard copies of the documents using a PS printer, you can generate the graphics as EPS files using the postscript() function. Word can import EPS files, but you will see them only as place holders in your document (ie. a frame box) since Word cannot actually interpret the images for display. Keep in mind that the function has very specific argument requirements to enable the generation of EPS files. These include: horizontal = FALSE, onefile = FALSE, paper = "special" With these in place, you can then generate your plots to the EPS files and import them into your Word documents. See ?postscript for more information. If this is something that you will be doing with a level of repetition, you might want to look into using Sweave, which combines LaTeX and R to automate formatted report generation. More information is here: http://www.ci.tuwien.ac.at/~leisch/Sweave/ There were also a couple of articles in RNews: Friedrich Leisch. Sweave, part I: Mixing R and LaTeX. R News, 2(3):28-31, December 2002. Friedrich Leisch. Sweave, part II: Package vignettes. R News, 2(2):21-24, October 2003. Frank Harrell also has a document at: http://cran.r-project.org/doc/contrib/Harrell-statcomp-notes.pdf HTH, Marc Schwartz
Gabor Grothendieck
2004-Feb-20 19:04 UTC
[R] R: Including R plots in a Microsoft Word document
However, 1. the editing of .ps files once they have been inserted into Word seems to be limited compared to using Windows metafiles. 2. R on Windows does not provide the capability to do right click copy with a .ps file so you have to right click save the file and then insert it into Word which is a bit less convenient than using the clipboard and it leaves a file around that you still have to delete. --- Date: Fri, 20 Feb 2004 12:29:24 -0500 From: Frank E Harrell Jr <feh3k at spamcop.net> To: <MSchwartz at medanalytics.com> Cc: <r-help at stat.math.ethz.ch> Subject: Re: [R] R: Including R plots in a Microsoft Word document On Fri, 20 Feb 2004 11:08:00 -0600 Marc Schwartz <MSchwartz at medanalytics.com> wrote:> On Fri, 2004-02-20 at 09:54, Mahmoud K. Okasha wrote: > > Greetings List, > > > > I am conducting some large simulations using R. As a result, I get > > many plots but I'm having some trouble with including some of them in > > a Microsoft Word document. Can any one tell me the easiest method of > > having copies of the R-graphs in the Word documents? > > > > Best regards > > Mahmoud > > > A couple of different ways: > > 1. If you actually need to see the graphics within the document and/or > send the .doc file to someone who needs to be able to see the plots as > they appear, then you should use Windows Metafile format images. Since > these are vector format files, you can resize them as required on your > pages. Bitmapped images (ie .BMP/.PNG) will distort as you resize them. > You can generate these by plotting directly into an R plot window and > then copy (ie. right mouse click) and paste into the Word document using > the Windows clipboard, or generate the plot files directly using the > win.metafile() function. > > 2. If you will be generating hard copies of the documents using a PS > printer, you can generate the graphics as EPS files using the > postscript() function. Word can import EPS files, but you will see them > only as place holders in your document (ie. a frame box) since Word > cannot actually interpret the images for display. Keep in mind that the > function has very specific argument requirements to enable the > generation of EPS files. These include:Newer versions of Word will display postscript and pdf images on-screen, so I think these are the way to go. -Frank
Gabor Grothendieck
2004-Feb-21 00:04 UTC
[R] R: Including R plots in a Microsoft Word document
Perhaps some additional explanation is in order. There are two basic classes of format: - vector graphics such as windows metafile (wmf) and svg where the actual structure of the drawing is stored. Editing these can be done with no loss of resolution and you can access the individual components of the plot, the titles, the points, etc. directly. - bitmapped (also called raster) graphics such as jpg and png where the drawing is stored as a sequence of pixels. You can't access the individual objects in a plot with raster graphics since the image is just a set of pixels. Resizing involves a loss of resolution. Windows metafiles are the preferred format for Word. They are vector graphics, not raster, and they can be edited from within Word directly -- you don't need another editing program. This should be much easier than using bmp or jpg together with Photoshop. You can either generate wmf files by right clicking the plot and copying to the clipboard or using R code like this: win.metafile("/myfile.wmf") plot(1:10) dev.off() followed by Insert | Picture | File in Word. In Paul's case he is generating his images in Linux, where I gather Windows metafiles are not available, but in your case everything is on Windows so you should not have that problem. --- Date: Sat, 21 Feb 2004 00:38:37 +0200 From: Mahmoud K. Okasha <m.okasha at palnet.com> [ Add to Address Book | Block Address | Report as Spam ] To: Paul Johnson <pauljohn at ku.edu>, <r-help at stat.math.ethz.ch> Subject: Re: [R] R: Including R plots in a Microsoft Word document Hello, I first would like to thank all of you for your great ideas. However, I agree with Paul particularly in that the answer is more complicated than other people make it seem when you have many graphs. I am trying all the ideas. It seems that all of them work but with some difficulties. I have Windows 2000 and MS Office 200. It seems to me that the easiest way of solving the problem is through saving the file in Bmp or Jpeg format and edit it in a graphic program such as Photoshop then insert it in the file. I will continue trying all methods to find the easiest. Best regards Mahmoud ----- Original Message ----- From: "Paul Johnson" <pauljohn at ku.edu> To: <r-help at stat.math.ethz.ch> Sent: Friday, February 20, 2004 9:47 PM Subject: Re: [R] R: Including R plots in a Microsoft Word document> I have wrestled with this problem a lot. I use Linux, coauthors use > Windows, and the eps files I make from R don't work with MS Word. Well, > the don't ever have "previews" and they sometimes won't print at all > when I use CrossOver Office with MS Office 2000 in Linux. My coauthor > says he can often wrestle my eps files into word on his system with > Office 2003. People keep telling me to use gsview to insert the preview > panes into eps files, and that does work, but more than one half of the > time my system creates eps files that look significantly worse than the > originals. Sometimes it inserts a blank page at the top of the eps or > it reshapes a figure. I don't care enough about MS to try to track that > down. It just pisses me off. > > > As a result, I think the answer is more complicated than other people > make it seem. > > I don't think it does any good to output a pdf file because, as I > learned yesterday, MS Word users can't import a pdf file into a doc. > > Clearly, if you are an MS windows user of R, you can save graphics in > the windows meta format (wmf) (or is it enhanced meta format, emf?). > That will go more or less seamlessly into Word. If you have a chance to > boot into Windows, and you really must make an image that works well > with Word, then you should boot into Windows, run your R in there and > make the wmf file. > > If you are a Linux/Unix user, and you are too proud to use Windows, the > problem is much more difficult to deal with. > > If you are ABSOLUTELY SURE that your image does not need to be resized > in any way, you could output from R into a picture type format, such as > png. As long as the image does not need to resized in any way, that > will be fine. If it is resized, then all bets are off. > > I find that the R output to the xfig format is quite good and I can edit > files in xfig. You can edit those files, add text, so its very very > handy. So right now I'm looking for a good bridge from xfig format to > Word. But I just started investigating that. > > uaca at alumni.uv.es wrote: > > >On Fri, Feb 20, 2004 at 05:54:33PM +0200, Mahmoud K. Okasha wrote: > > > > > >>Greetings List, > >> > >>I am conducting some large simulations using R. As a result, I get manyplots but I'm having some trouble with including some of them in a Microsoft Word document. Can any one tell me the easiest method of having copies of the R-graphs in the Word documents?> >> > >> > > > >R can produce at least PostScript, PDF, png, jpeg/jpg > > > >see: > > > > help(postscript) > > help(pdf) > > help(png) > > help(jpeg) > > > >I don't use word, for me the PostScript format (more preciselyEncapsulated> >PostScript/.eps) is the best/more easy/powerful format if you don't havethousands of> >points or lines :-) > > > >por instance, to print a simple plot: > > > >postscript(file="somefile.eps"); > > > >plot(whatever); > > > >dev.off(); <<---- Important > > > >other formats are similar > > > >regards > > > > Ulisses > > > > > > Debian GNU/Linux: a dream come true > >-----------------------------------------------------------------------------> >"Computers are useless. They can only give answers." PabloPicasso> > > >Humans are slow, innaccurate, and brilliant. > >Computers are fast, acurrate, and dumb. > >Together they are unbeatable > > > >---> Visita http://www.valux.org/ para saber acerca de la <--- > >---> AsociaciĆ³n Valenciana de Usuarios de Linux <--- > > > >______________________________________________ > >R-help at stat.math.ethz.ch mailing list > >https://www.stat.math.ethz.ch/mailman/listinfo/r-help > >PLEASE do read the posting guide!http://www.R-project.org/posting-guide.html> > > > > > > -- > Paul E. Johnson email: pauljohn at ku.edu > Dept. of Political Science http://lark.cc.ku.edu/~pauljohn > 1541 Lilac Lane, Rm 504 > University of Kansas Office: (785) 864-9086 > Lawrence, Kansas 66044-3177 FAX: (785) 864-5700 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide!http://www.R-project.org/posting-guide.html>
It was pointed out that while win.metafile("/myfile.wmf") is useful, it only works on windows. Here's a path which would work on Unix: 1) Write an xfig file using R. I use something like : xfig(file="created.fig", onefile=TRUE, bg="LightSkyBlue", width=5, height=3) plot() This is a very smart path to take if you want to manually touch up the picture in xfig (leave aside the Windows problem). 2) The program fig2dev converts .fig files into many file formats, including one "cgm" which is reputed to feed well into Microsoft software. I have no Windows here, however, and can't try this out. Now for a (perhaps trivial) question: Several people said you have to do win.metafile("/myfile.wmf") plot(1:10) dev.off() <-------- this is essential Why is the 3rd line essential? I have been feeding R programs into R using the command $ R --vanilla < file.R and I find things work just fine without having a dev.off() command. E.g. I have this program which seems to work fine: A <- read.table( file="datafile.2", na.strings=".", col.names=c("date","dlinrchf","dlusdchf","dljpychf","dldemchf") ) xfig(file="created.fig", onefile=TRUE, bg="LightSkyBlue", width=5, height=3) plot(A$dlusdchf, A$dlinrchf, xlab="USD/CHF returns", ylab="INR/CHF returns", col = "dark red") -ans. -- Ajay Shah Consultant ajayshah at mayin.org Department of Economic Affairs http://www.mayin.org/ajayshah Ministry of Finance, New Delhi
> From: Ajay Shah[snip]> Now for a (perhaps trivial) question: Several people said you have to > do > > win.metafile("/myfile.wmf") > plot(1:10) > dev.off() <-------- this is essential > > Why is the 3rd line essential? I have been feeding R programs into R > using the command > > $ R --vanilla < file.R > > and I find things work just fine without having a dev.off() > command. E.g. I have this program which seems to work fine:Without closing the device explicitly with dev.off(), the file is not closed. If this is done in an interactive session, then win.metafile("try.wmf") plot(1:10) [import the file into some application] will give you empty graph. This works for you inside a script because any opened graphic devices are closed at the end of the execution. HTH, Andy> A <- read.table( > file="datafile.2", > na.strings=".", > > col.names=c("date","dlinrchf","dlusdchf","dljpychf","dldemchf") > ) > xfig(file="created.fig", onefile=TRUE, bg="LightSkyBlue", > width=5, height=3) > plot(A$dlusdchf, A$dlinrchf, > xlab="USD/CHF returns", > ylab="INR/CHF returns", > col = "dark red") > > -ans. > > -- > Ajay Shah Consultant > ajayshah at mayin.org Department of Economic Affairs > http://www.mayin.org/ajayshah Ministry of Finance, New Delhi > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >------------------------------------------------------------------------------ Notice: This e-mail message, together with any attachments,...{{dropped}}
Gabor Grothendieck
2004-Feb-23 03:05 UTC
[R] R: Including R plots in a Microsoft Word document
Another way to transfer plots is to transfer the display list. This is a list of the low level plot commands that R produces. For example, on Linux do this: dev.control(displaylist="enable") # enable display list plot(1:10) myplot <- recordPlot() # load displaylist into variable save(myplot, file="myplot", ascii=TRUE) Send the ascii file, myplot, to the Windows machine and on Windows do this: dev.control(displaylist="enable") # enable display list load("myplot") myplot # displays the plot savePlot("myplot", type="wmf") # saves current plot as wmf I don't have Linux to actually try this out but perhaps someone with both can try it out. --- Date: Sun, 22 Feb 2004 21:13:37 +0530 From: Ajay Shah <ajayshah at mayin.org> To: <r-help at stat.math.ethz.ch> Subject: Re: [R] R: Including R plots in a Microsoft Word document It was pointed out that while win.metafile("/myfile.wmf") is useful, it only works on windows. Here's a path which would work on Unix: 1) Write an xfig file using R. I use something like : xfig(file="created.fig", onefile=TRUE, bg="LightSkyBlue", width=5, height=3) plot() This is a very smart path to take if you want to manually touch up the picture in xfig (leave aside the Windows problem). 2) The program fig2dev converts .fig files into many file formats, including one "cgm" which is reputed to feed well into Microsoft software. I have no Windows here, however, and can't try this out. Now for a (perhaps trivial) question: Several people said you have to do win.metafile("/myfile.wmf") plot(1:10) dev.off() <-------- this is essential Why is the 3rd line essential? I have been feeding R programs into R using the command $ R --vanilla < file.R and I find things work just fine without having a dev.off() command. E.g. I have this program which seems to work fine: A <- read.table( file="datafile.2", na.strings=".", col.names=c("date","dlinrchf","dlusdchf","dljpychf","dldemchf") ) xfig(file="created.fig", onefile=TRUE, bg="LightSkyBlue", width=5, height=3) plot(A$dlusdchf, A$dlinrchf, xlab="USD/CHF returns", ylab="INR/CHF returns", col = "dark red") -ans. -- Ajay Shah Consultant ajayshah at mayin.org Department of Economic Affairs http://www.mayin.org/ajayshah Ministry of Finance, New Delhi ______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html