[R] is there a way to create legends automatically?

ben@zoo.ufl.edu ben at zoo.ufl.edu
Tue Oct 24 15:32:12 CEST 2000


  Can you be a little more specific about what you want to do?
  To "automatically" create a legend you can write a small function that
plots your data and then generates a legend.  An example is below: the
only slightly tricky thing is automatically generating the correct
placement of the legend.  I've chosen here to put it in the upper
left-hand corner.  It would be wonderful to be able to somehow know where
to put it that it wouldn't overlap any of the data, but short of expanding
the plot range beyond the range of the data I don't see that this is
possible to do automatically.

x <- as.data.frame(matrix(runif(200),ncol=5))
names(x) <- LETTERS[1:5]

plot.with.legend <- function(x,col=1:ncol(x),lty=1:ncol(x),...) {
## x should be a data frame with all numeric variables
   matplot(1:nrow(x),x,col=col,lty=lty,type="l",...)
   u <- par("usr")  
   ## put an automatic legend in the upper left-hand corner of the plot
   legend(u[1],u[4],names(x),col=col,lty=lty)
}

On Tue, 24 Oct 2000 Pan_Yuming at aam.de wrote:

> Hi all,
> 
> when plotting mutiple data series, i need to specify how one data series
> should be plotted. that s fine, but can i let the program automatically
> create legends afterwards? thanks very much.
> 
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> 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
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> 
> 

-- 
318 Carr Hall                                bolker at zoo.ufl.edu
Zoology Department, University of Florida    http://www.zoo.ufl.edu/bolker
Box 118525                                   (ph)  352-392-5697
Gainesville, FL 32611-8525                   (fax) 352-392-3704

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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