Skip to content

trouble positioning legends on barplot written to a file

6 messages · Tom Arnold, Paul Murrell, Brian Ripley +1 more

#
I'm trying to generate some plots for print publication and I'm having trouble
getting them the way I want. I get a plot with a legend that overlaps the bars
and looks bad. Also, I'm saving this as PDF file (whatever happened to the
"png" function to save as PNG format, in r 1.4.1?)

I've tried a number of things, which produce a number of questions.

1) it seems that the "legend" for a plot must be within the plot area. Is
there a way to put the legend above, below, or to the side of the plot? The
particular data I have fill up the plotting area (stacked bar charts, going to
100% in each bar)

2) I've tried using "locator()" to position the legend. This only works, so
far, if I am not using trying to divert output to another device -- like
creating a PDF via
pdf();
barplot(resultmat);
dev.off();

so if I take out the pdf() and the dev.off() and just do this:
barplot(resultmat);
legend(locator(1),legend=l,fill=colors);

it works, on the graphics display device window on screen.

However, if I do this:
pdf(onefile=FALSE);
barplot(resultmat);
legend(locator(1),legend=l,fill=colors);
dev.off();

I get:
Also it doesn't let me specify a point. I suppose that's not so surprising,
because it is drawing to a non-interactive graphics device (the PDF "device"
is non-interactive, right?), but the error message is confusing.

So, is there any way to specify the location with locator() AND to save that
plot to a file?

And if I don't use "locator' then I have to specify the legend location to
"legend(x,y, legend) but the documentation doesn't say what coordinate system
that's supposed to be in.

TIA.

Tom Arnold

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Hi

It is possible to draw the legend outside the plot, but it is
inconvenient because the location is specified in terms of the scales on
the x- and y-axes.  Positioning therefore becomes a matter of trial and
error.  In the example below, I create space for the legend using
par(oma) [again the size of the margin is a matter of guess work] and
make sure that the legend is not clipped out of existence by specifying
par(xpd=NA) ...

   # generate some dummy data that sums to 100 in each col
   data(VADeaths)
   temp <- function(x) {
     sample(seq(10, 30, 5))
   }
   dummy <- apply(VADeaths, 2, temp)
   # Plotting ...
   par(oma=c(0, 0, 5, 0), xpd=NA)
   colours <- 1:5
   barplot(dummy, col=colours)
   legend(par("usr")[1], 110,
          row.names(VADeaths), fill=colours,
          yjust=0, horiz=TRUE)

You might have other colours you would prefer to use :)

Hope that helps.

Paul
Tom Arnold wrote:

  
    
#
On Tue, 12 Nov 2002, Tom Arnold wrote:

            
Nothing: it is still there on the systems that have ever had it, Windows
and Unix-alikes with an X11 display.  What's your problem in finding it?
What does ?png give on your system?
Look up the xpd par argument to plot outside the plot region (but still in
the figure region).
It's you who confused it.  Print the result of locator(1) and look up
?legend and you will see why you get the message.  If you call a function
with incorrect arguments, why don't you expect an error message telling
you so?
Yes, via dev.copy and friends.
There is only one coordinate system, that set by the plot() command.
*None* of the R graphics commands tell you the coordinate system to be
used precisely because there is only one at a time.

There *is* are example on the help page for legend.
#
Tom Arnold wrote:
??? pdf() produces PDFs, png() portable network graphics, etc. .... I
don't get the point.
BTW: R-1.4.1 is rather old.
Use 
 par(xpd = TRUE)
before legend(), see ?par for details.
I wouldn't expect locator() to work that way at all ...
Yes. plot() to the windows() or x11() device, use locator() and "copy"
the plot to another device.
Look particularly at 
 ?dev.copy, ?dev.print, and ?dev.copy2eps(...)
I don't recommend this way.
Use par("usr") (after barplot()) to get some interesting information.
Thgis can be combined with par(xpd = TRUE), which was mentioned above:

 pdf(onefile = FALSE)
 barplot(resultmat)
 # get "extremes of the user coordinates of the plotting region." 
 par("usr")     
 # "plotting clipped to figure region"  
 par(xpd = TRUE)
 # now a proper legend()
 dev.off()

Uwe Ligges
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Thanks to all who replied with suggestions about using par(xpd=TRUE), using
dev.copy, and using a separate plot area for the legend. These are all good
and with the first response I got enough information to get a satisfactory
solution -- I haven't tried them all yet.

Another question in my post confused the issue. I mentioned that I'm using
pdf() and asked what happened to the png() function. Let me try to clarify:
- I want to produce a separate file for inclusion in web or print documents.
- In the past I've used the png() function, on Windows to create a file for
use on a web page
- I work alternately on Mac, Windows, Linux, and Mac OS X (A FreeBSD
derivative)
- I'm working on Mac OS 9.1 at the moment.

I grabbed some old files I'd created with subroutines in R that I used on R
1.4.1 on Windows. I opened them on my Mac and tried to run them in R 1.6.1 and
found that png() does not exist. I didn't think to go back to the Windows
platform and try it, and I guessed that for some reason png had disappeared --
much the same as GIF disappeared from some open source packages like GD a few
years ago because of the Unisys patent claim on LZW compression. Of course I
had no data for this, it was a guess. It didn't occur to me that a call to
produce "portable network graphics" would appear only on certain computing
platforms, [mild irony] and I saw no mention of it in the documentation.

So I looked around and found the pdf() call and started using that. For print
documents, it will probably work better anyway. Of course the formats were
created for different needs; that was not my question. I just was surprised
that png() seemed to have "disappeared" from my Mac version of R, when in fact
I guess it never existed in that version anyway.

Got that?
-Tom

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Would you like to contribute a png() device for MacOS?

The thing is, kind users of Windows and of Unix/X11 did contribute one for
each. Perhaps you now appreciate their efforts?
On Tue, 12 Nov 2002, Tom Arnold wrote: