[R] Placing legends
Alexander Ploner
Alexander.Ploner at meb.ki.se
Mon Oct 6 09:24:28 CEST 2003
> > I'm trying to automate making a bunch of figures and I need a way to
> > automate legend position. As I understand it the legend is
> placed based
> > on coordinates. I don't know before hand what the
> coordinates are going
> > to be. On one graph my y axis might go from -50 to -10.
> On another it
> > might go from 0 to 180.
I've been using the following utility - just use named arguments to
pass on to legend, i.e. "legend=", "pch=" etc.
"Legend" <- function (x="right", y="top", ..., offset=0.25)
{
# Name: Legend
# Desc: fixes a legend to one of the corners of a plot
# Auth: Alexander.Ploner at meb.ki.se
x <- match.arg(x,c("left","center","right"))
y <- match.arg(y,c("bottom","center","top"))
cc <- par("usr")
if (x=="left") {
x <- cc[1] + xinch(offset)
xjust <- 0
} else if (x=="center") {
x <- (cc[1] + cc[2])/2
xjust <- 0.5
} else if (x=="right") {
x <- cc[2] - xinch(offset)
xjust <- 1
}
if (y=="bottom") {
y <- cc[3] + yinch(offset)
yjust <- 0
} else if (y=="center") {
y <- (cc[3] + cc[4])/2
yjust <- 0.5
} else if (y=="top") {
y <- cc[4] - yinch(offset)
yjust <- 1
}
legend(x, y, xjust=xjust, yjust=yjust, ...)
}
HTH,
alexander
Alexander.Ploner at meb.ki.se
Phone: 46-8-524-82329
Fax : 46-8-31 11 01
Medical Epidemiology & Biostatistics
Karolinska Institutet,
P.O. Box 281, SE-171 77 Stockholm
More information about the R-help
mailing list