[R] fill prob. in legend

Jim Lemon jim.lemon at uts.edu.au
Tue Feb 25 00:26:04 CET 2003


Jeremy Z. Butler wrote:
>
> Hi,
> I'm trying to construct a legend which has four lines of text and
>  associated symbols. The first two symbols need to be normal lines which
>  vary only in colour. The second two symbols should have filled boxes. How
>  do I suppress the fill boxes in the first two lines?
> J.

As far as I can see, it would be really hard to suppress the fill boxes. 
However, it isn't too hard to overwrite the point/line indicators. First, do 
the legend like this:

legendinfo<-legend(...,lty=1,col=c(...))

This will produce a legend with all different colored lines. Then call the 
function below like this:

add.legend.bars(legendinfo,which=c(3,4),border="black",col=c(2,3))

The function below seems to work for a linear or log axis plot. Hope it helps.

Jim

add.legend.bars<-function(legend.info,which,border,col) {
 if(!missing(legend.info) &&
    !missing(which) &&
    !missing(border) &&
    !missing(col)) {
  nelem<-length(legend.info$text$x)
  if(par("ylog")) 
   barheight<-(10^legend.info$text$y[1]-10^legend.info$text$y[2])/2
  else
   barheight<-(legend.info$text$y[1]-legend.info$text$y[2])/2
  if(par("xlog"))
   barwidth<-(10^legend.info$text$x[1]-10^legend.info$rect$left)/2
  else
   barwidth<-(legend.info$text$x[1]-legend.info$rect$left)/2
  if(par("xlog"))
   xleft<-10^legend.info$rect$left+barwidth/2
  else
   xleft<-legend.info$rect$left+barwidth/2
  xright<-xleft+barwidth
  if(par("ylog"))
   ytop<-(10^legend.info$text$y+barheight/2)[which]
  else
   ytop<-(legend.info$text$y+barheight/2)[which]
  ybottom<-ytop-barheight
  rect(xleft,ybottom,xright,ytop,border=border,col=col)
 }
 else cat("Usage: add.legend.bars(legend.info,which,border,col)\n")
}



UTS CRICOS Provider Code:  00099F

DISCLAIMER\ ==================================================== ... [[dropped]]




More information about the R-help mailing list