[R] Exporting R graphs

Marc Schwartz MSchwartz at medanalytics.com
Tue Aug 26 17:35:49 CEST 2003


On Tue, 2003-08-26 at 06:46, ucgamdo at ucl.ac.uk wrote:
> Hi,
> 
> 	I have been a happy user of R for windows for more than a year, however,
> recently, I started using linux as my operating system and now I have
> practically switched completely. Of course, I still use R with linux,
> however, certain nice features of R in windows seem to be missing or
> hidden. I need help in basically two points:
> 
> 1. In windows, I could copy the contents of a window graphic's device as a
> windows metafile, and then I could paste the metafile into OpenOffice draw.
> The advantage of doing this is that I could then manipulate my graph as a
> vector object and get absolute control of every line, point, etc. I could
> also paste several graphs in the same page and resize them arbitrarily,
> which is a lot nicer than using the screen.split() or frame() functions in
> R. Is there any equivalent method I could use to get R graphics into
> OpenOffice draw in linux?, keeping the same functionality of course.


There are, as you are finding out, some notable differences between the
Windows and Linux versions of R which are described in the R Windows
FAQ, section 4. This is also a reflection to an extent, of the
differences between Windows and Linux. As one who has also made the
switch in the past year, this is an internal struggle that requires a
shift in thinking to overcome years of ingrained behavior.

Depending upon what you are doing with the output (ie. displaying on the
screen or generating printed output), you have options of generating a
png() or jpg() bitmap format image for the screen or sending the output
to an encapsulated postscript file for printing. OpenOffice's (OOo)
applications can import either in to an Impress slide or Writer
document, for example.

As an aside, there is a Linux library for generating metafile graphics
called libEMF, but my personal experience is less than enthusiastic
relative to the image quality generated.

See ?bitmap, ?png and ?jpeg for additional information on generating
bitmap images. See ?postscript for information on generating an EPS
file, noting very carefully, the instructions in the Detail section
required to do so.

Using bitmaps of course, the behavior of the graphic objects are
different as compared to vector objects, not least of which is the more
limited ability to re-size the images, before a loss of image quality. 

That being said, from a philosophic perspective, I would recommend that
you use R alone to get the graphic or matrix of graphics (ie. using
layout() ) the way you want and then use OOo only as a display or
document layout mechanism. If there are things that you must edit
outside of R, you could use The GIMP to do this on the jpg or png files.

Yet another option for document layouts is called Sweave
(http://www.ci.tuwien.ac.at/~leisch/). You may wish to explore Sweave,
which enables you to create LaTeX documents that can be automatically
updated with the output of R's analyses.

R's graphics provide you with a very substantial ability to minutely
control the plot output and to do so in a fashion that enables
reproducible results once you have coded the plots. This is a shift from
the "point and click" or "drag and drop" approach that is common under
Windows.

If I am going to display a plot on the screen (ie. in Impress) and know
what size image I require, what I have generally done is to use bitmap()
or png() to generate a PNG file, properly sized to what I require in the
target slide. I then Insert the graphic file into the slide.

If I need hard copy output to the printer, I generate EPS versions of
the plots and insert those into the documents and/or slides and then
print them. The challenge with using EPS files is that in OOo, you
cannot see the image. They show up as an object, generally displaying
the embedded postscript title. You can create "EPS previews", however
doing so adds tremendously to the file size for a decent preview image.
Thus, using and setting the 'title' argument in postscript() to
something that tells you what the plot is can be helpful if you need to
re-order the slides or change the document layout.

Another option with postscript formats, one you have the document set in
OOo, is to use the "Print to File" option, generating a postscript file,
which can then be displayed (or printed) using gv or one of the other PS
viewers available under Linux.

In conjunction with the above, I have also used ps2pdf under Linux to
convert the PS file to a PDF file, if I need to send the output to
someone who may not have the ability to view a PS file (ie. most Windows
users will have Adobe's free Acrobat Reader, but not GhostView.) This
gives you a range of options if you need to share the output with others
electronically.

Lastly, if you just need to create some plots that you can send to folks
independent of OOo, you can also use pdf() in R (see ?pdf), which will
enable you to create a PDF file directly containing one or more plots,
that you can send to folks who cannot view PS files.

Bottom line, I would use R to create the plots and use the other
applications for document/slide formatting. But...that's my opinion
based upon what I am typically doing.


> 2. In windows, I could 'record' a graph, and then undo any changes made.
> For example, if I misplaced a text label somewhere, I could go back and
> place it again properly without having to re-plot everything again. I have
> been browsing the R documentation and found some recording functions for
> graphic devices but they do not seem to be exactly what I am looking for.
> Is there any way I can access the display list of a device and change it?
> Some of my plots are the product of tedious and long simulations which I
> wouldn't like to repeat if I make a mistake labelling my plots, placing
> legends, etc.


What you could do is to use recordPlot() to save the version of the plot
to a file, prior to the point at which you start annotating, so that you
can return to that same point if you make a mistake.

As a brief example, with output to the display:

# Do a quick scatterplot
plot(1:10)

# Now save the plot's current state
myplot <- recordPlot()

# Now save display list 'myplot' to a file
save(myplot, file = "myplot")

# Now place some text on the plot
# Click on the plot with the mouse after 
# this line executes, to indicate where you
# want the text located.
text(locator(1), "Some Text", adj=0)

# OK, didn't like where I located the text
# so load and redisplay the base plot
load("myplot")
myplot

# Now re-locate the text
text(locator(1), "Some Text", adj=0)


BTW, thanks to Paul Murrell
(http://www.stat.auckland.ac.nz/~paul/grid/saveload.pdf) for hints on
the above approach.

> 
> Thanks a lot for any help you can offer me,
> M.


HTH,

Marc Schwartz




More information about the R-help mailing list