[R] problem with legend on other graphics devices

Gavin Simpson gavin.simpson at ucl.ac.uk
Fri Jun 16 20:42:14 CEST 2006


I have a bit of a problem with a legend in png, eps and pdf plots
produced from a custom plotting function. I was writing a little
function to produce some stock plots for a routine analysis conducted in
our lab. I have a wrapper function figures() (see below) that produces
eps, png and pdf versions of the lab output. When I draw the plot on the
X11() device the correct amount of space is made available for the
legend, but when I draw the plot directly on the png, postscript or pdf
devices, the legend is placed too close to the edge of the plot and is

This example uses the actual test data I'm using to write the functions.
figures() calls the "dating" plot method I wrote to do the plotting, and
dating() is a wrapper for read.csv() that creates a "dating" object with
the lab analysis results and some extra meta data. All the R code is in
dating.R at the URL below.

felb5 <- dating("http://ecrc3.geog.ucl.ac.uk/download/dating/felb5.csv",
                cs = 1963, csError = 5,
                csDepth = 22.5, csDepthError = 2)
plot(felb5, as.Age = TRUE)
figures(felb5, "felbrig5", as.Age = TRUE)

As you can see, in the generated png, eps and pdf files the legend is
clipped, but it displays fine on the screen. From the list archives, I
seem to be doing things in the recommended way (not using dev.copy()

What can I do to force sufficient space for the legend in the pdf,
postscript and png plots?

Many thanks,


figures <- function(object, filename, eps = TRUE, png = TRUE, pdf = TRUE,
                    inch.width = 6, inch.height = 6, horizontal = TRUE,
                    pixel.width = 600, pixel.height = 600,
                    paper = "special", onefile = FALSE, pointsize = 12, ...)
    if(eps) {
      postscript(paste(filename, ".eps"), width = inch.width,
                 height = inch.height, onefile = onefile, paper = paper,
                 pointsize = pointsize, horizontal = horizontal)
      plot(object, ...)
    if(png) {
      png(paste(filename, ".png"), width = pixel.width, height = pixel.height,
          pointsize = pointsize)
      plot(object, ...)
    if(pdf) {
      pdf(paste(filename, ".pdf"), width = inch.width,
          height = inch.height, onefile = onefile, paper = paper,
          pointsize = pointsize)
      plot(object, ...)

*  Note new Address, Telephone & Fax numbers from 6th April 2006  *
Gavin Simpson
ECRC & ENSIS                  [t] +44 (0)20 7679 0522
UCL Department of Geography   [f] +44 (0)20 7679 0565
Pearson Building              [e] gavin.simpsonATNOSPAMucl.ac.uk
Gower Street                  [w] http://www.ucl.ac.uk/~ucfagls/cv/
London, UK.                   [w] http://www.ucl.ac.uk/~ucfagls/

More information about the R-help mailing list