[R] legend in R plot
Jim Lemon
jim at bitwrit.com.au
Thu Jul 22 13:39:43 CEST 2010
On 07/22/2010 02:06 PM, li li wrote:
> Hi all,
> I am have some difficulty with the legend function.
> I need to add a legend to describe the different line types in a plot. The
> legend box is small.
> It did not include sufficient length of each line type to help distinguish
> the differnt line types.
> Is there a way to fix this.
Hi Hannah,
I thought that there was some argument to specify the horizontal
proportion of the legend box devoted to text, but I can't find it.
Perhaps you could roll your own legend using the rect, text and line
functions.
wide.legend<-function(x,y=NA,legend,lty=1,lwd=1,bg=par("bg"),
border=par("fg"),col=par("fg"),cex=1,
xjust=0,yjust=1,text.prop=0.5,...) {
if(is.na(y) && is.list(x)) {
y<-unlist(x[[2]])
x<-unlist(x[[1]])
}
boxwidth<-(max(strwidth(legend))+strwidth("o"))/text.prop
boxheight<-strheight(paste(legend,collapse="\n"))+strheight("o")
boxleft<-x-boxwidth*xjust
boxbottom<-y-boxheight*yjust
rect(boxleft,boxbottom,boxleft+boxwidth,
boxbottom+boxheight,col=bg,border=border)
text(boxleft+boxwidth*(1-text.prop),
boxbottom+1:length(legend)*strheight("O"),
legend,cex=cex,adj=c(0,0.5),...)
hgap<-strwidth("o")
segments(boxleft+hgap,
boxbottom+1:length(legend)*strheight("O"),
boxleft+boxwidth*(1-text.prop)-hgap,
boxbottom+1:length(legend)*strheight("O"),
lty=lty,lwd=lwd,col=col)
}
Pretty rough, but it's a start
Jim
More information about the R-help
mailing list