Hi
Morten Sickel wrote:>
> I am testing out the grid-plots, but get into a problem making a legend. I
> have a line plot and some points, following the model in
> grid.plot.and.legend() i manage to get the points correctly in the legend,
> but what do I do with the line? (Of cource, I can just draw a line at the
> right location on the plot, but I would prefer to use legend)
Apologies for the very late reply (it's a busy teaching time:().
I'm afraid the grid.legend() function is only a little bit less of a
how-to demonstration than the grid.plot.and.legend() function. However,
it is fairly straightforward to add line types to it. The code below
defines a grid.legend2() function that allows you to specify lty as well
as pch. Here are a couple of simple demonstrations of how it could be
used ...
grid.legend2(1:3, 1:3, 1:3)
grid.newpage()
grid.legend2(lty=1:3, labels=1:3)
... there is also a grid.plot.and.legend2() function given below which
does the same thing as grid.plot.and.legend(), but draws a legend using
lty instead of pch. I hope this is of some help. Please get back to me
if any of this is not clear.
Paul
############
grid.legend2 <- function (pch=NULL, lty=NULL,
labels, frame = TRUE,
hgap = unit(0.5, "lines"),
vgap = unit(0.5, "lines"),
default.units = "lines",
gp = gpar(),
draw = TRUE, vp = NULL)
{
labels <- as.character(labels)
nkeys <- length(labels)
if (!is.unit(hgap))
hgap <- unit(hgap, default.units)
if (length(hgap) != 1)
stop("hgap must be single unit")
if (!is.unit(vgap))
vgap <- unit(vgap, default.units)
if (length(vgap) != 1)
stop("vgap must be single unit")
legend.layout <-
grid.layout(nkeys, 3,
widths = unit.c(unit(2, "lines"),
max(unit(rep(1, nkeys), "strwidth",
as.list(labels))),
hgap),
heights = unit.pmax(unit(2, "lines"),
vgap + unit(rep(1, nkeys), "strheight",
as.list(labels))))
gf <- grid.frame(layout = legend.layout, vp = vp, gp = gp,
draw = FALSE)
for (i in 1:nkeys) {
if (!is.null(pch))
grid.place(gf, grid.points(0.5, 0.5, pch = pch[i],
draw = FALSE),
col = 1, row = i, draw = FALSE)
if (!is.null(lty))
grid.place(gf, grid.lines(x=c(0.2, 0.8), y=0.5,
gp=gpar(lty=lty[i]),
draw=FALSE),
col = 1, row = i, draw=FALSE)
grid.place(gf, grid.text(labels[i], x = 0, y = 0.5,
just = c("left", "centre"),
draw = FALSE),
col = 2, row = i, draw = FALSE)
}
if (draw)
grid.draw(gf)
gf
}
grid.plot.and.legend2 <- function ()
{
grid.newpage()
top.vp <- viewport(w = 0.8, h = 0.8)
push.viewport(top.vp)
x <- runif(10)
y1 <- runif(10)
y2 <- runif(10)
lty <- 1:3
labels <- c("Girls", "Boys", "Other")
lf <- grid.frame(draw = TRUE)
plot <- grid.collection(grid.rect(draw = FALSE),
grid.lines(x, y1, gp=gpar(lty = 1),
draw = FALSE),
grid.lines(x, y2, gp=gpar(lty=2),
draw = FALSE),
grid.xaxis(draw = FALSE), grid.yaxis(
draw = FALSE),
draw = FALSE)
grid.pack(lf, plot)
grid.pack(lf, grid.legend2(NULL, lty, labels, draw = FALSE),
height = unit(1, "null"), side = "right")
grid.draw(lf)
}
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at
stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._