[R] legend with mixed boxes and lines (not both)

Michael Toews mwtoews at sfu.ca
Tue May 15 03:13:21 CEST 2007


Hi,
I seem to be unable to get a mixed legend that has lines *or* polygons 
(not both). For example:

ppi <- seq(0,2*pi,length.out=21)[-21]
frame()
plot.window(ylim=c(-5,5),xlim=c(-5,5),asp=1)
polygon(cos(ppi)*4+rnorm(20,sd=.2),sin(ppi)*4+rnorm(20,sd=.2),
    col="green",border=FALSE)
polygon(cos(ppi)*2+rnorm(20,sd=.1),sin(ppi)*2+rnorm(20,sd=.1),
    col="blue",border=FALSE)
abline(0,2,col="red")
legend("topleft",legend=c("out","in","line"),bty="n",
    fill=c("green","blue",NA),col=c(NA,NA,"red"),
    lwd=c(NA,NA,1))

I'm really guessing the behaviour in the legend() call, by setting fill 
to NA for the item, etc. I also tried fill=c("green","blue",FALSE), but 
that didn't go over too well either. I also tried adding "merge=TRUE", 
but that just puts the line into the box. I also tried using 
box.lwd=c(1,1,0), but that also did not work
Is there either a way to do this or a clean workaround? Thanks in advance.
+mt



More information about the R-help mailing list