[R] trouble positioning legends on barplot written to a file

Warnes, Gregory R gregory_r_warnes at groton.pfizer.com
Wed Nov 13 00:19:19 CET 2002


You might consider putting the legend in a separate plot area.  For
instance, using the layout function:

library(grid)
data(VADeaths, package = "base")
layout( cbind(1,2), widths=c(8,3) )  # make two plotting areas, 
                                     #   8 and 3 units wide
barplot(VADeaths, plot = TRUE)       # generate the barplot
plot.new()                           # move to the new region
legend(x=0.5,y=0.5,letters[1:5],     # center a legend in the new
       fill=heat.colors(5)[1:5],     #   plot region
       xjust=0.5,yjust=0.5)

-Greg

> -----Original Message-----
> From: Tom Arnold [mailto:tarnold at smpllc.com]
> Sent: Tuesday, November 12, 2002 12:40 PM
> To: r-help at stat.math.ethz.ch
> Subject: [R] trouble positioning legends on barplot written to a file
> 
> 
> 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:
> >Error in legend(locator(1),legend = l : missing y
> 
> 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
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
> _._._._._._._._._
> 


LEGAL NOTICE
Unless expressly stated otherwise, this message is confidential and may be privileged. It is intended for the addressee(s) only. Access to this E-mail by anyone else is unauthorized. If you are not an addressee, any disclosure or copying of the contents of this E-mail or any action taken (or not taken) in reliance on it is unauthorized and may be unlawful. If you are not an addressee, please inform the sender immediately.
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list