On Mon, 2007-05-14 at 18:13 -0700, Michael Toews wrote:> 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
Is this what you want?
ppi <- seq(0, 2 * pi, length.out = 21)[-21]
plot(c(-5, 5), c(-5, 5), xaxs = "i", yaxs = "i",
type = "n", axes = FALSE, ann = FALSE, 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",
col = c("green", "blue", "red"),
lty = c(0, 0, 1), lwd = c(0, 0, 1),
pch = c(22, 22, NA),
pt.bg = c("green", "blue", NA),
pt.cex = 2)
Instead of using 'fill', set the points explicitly and then define the
point backgrounds, line types, etc. to get the desired result.
See ?par for line type information.
BTW, some strategically placed spaces would help with code readability.
HTH,
Marc Schwartz