[R] Legend in xyplot two columns

Deepayan Sarkar deepayan at stat.wisc.edu
Thu Apr 14 17:00:43 CEST 2005


On Thursday 14 April 2005 05:30, Gesmann, Markus wrote:
> Dear R-Help
>
> I have some trouble to set the legend in a xyplot into two rows.
> The code below gives me the legend in the layout I am looking for, I
> just rather have it in two rows.
>
> library(lattice)
> schluessel <- list(
>                points=list( col="red", pch=19, cex=0.5 ),
>                text=list(lab="John"),
>                lines=list(col="blue"),
>                text=list(lab="Paul"),
>                lines=list(col="green"),
>                text=list(lab="George"),
>                lines=list(col="orange"),
>                text=list(lab="Ringo"),
>                rectangles = list(col= "#FFFFCC", border=FALSE),
>                text=list(lab="The Beatles"),
>      )
>
> xyplot(1~1, key=schluessel)
>
> The next code gives me two rows, but repeates all the points,lines,
> and rectangles.
>
> schluessel2 <- list(
>                points=list( col="red", pch=19, cex=0.5 ),
>                lines=list(col=c("blue", "green", "orange")),
>                rectangles = list(col= "#FFFFCC", border=FALSE),
>                text=list(lab=c("John","Paul","George","Ringo", "The
> Beatles")),
>                columns=3,
>       )
>
> xyplot(1~1, key=schluessel2)
>
> So I think each list has to have 6 items, but some with "no" content.
> How do I do this?

You could try using col="transparent" to suppress things, but that's not 
a very satisfactory solution. The function to create the key is simply 
not designed to create unstructured legends like this. However, you can 
create an use an arbitrary ``grob'' (grid graphics object) for a 
legend, e.g.:

##-----------------

library(grid)
library(lattice)

fl <-
    grid.layout(nrow = 2, ncol = 6,
                heights = unit(rep(1, 2), "lines"),
                widths =
                unit(c(2, 1, 2, 1, 2, 1),
                     c("cm", "strwidth", "cm",
                       "strwidth", "cm", "strwidth"),
                     data = list(NULL, "John", NULL,
                     "George", NULL, "The Beatles")))

foo <- frameGrob(layout = fl)
foo <- placeGrob(foo,
                 pointsGrob(.5, .5, pch=19,
                            gp = gpar(col="red", cex=0.5)),
                 row = 1, col = 1)
foo <- placeGrob(foo,
                 linesGrob(c(0.2, 0.8), c(.5, .5),
                           gp = gpar(col="blue")),
                 row = 2, col = 1)
foo <- placeGrob(foo,
                 linesGrob(c(0.2, 0.8), c(.5, .5),
                           gp = gpar(col="green")), 
                 row = 1, col = 3)
foo <- placeGrob(foo,
                 linesGrob(c(0.2, 0.8), c(.5, .5),
                           gp = gpar(col="orange")), 
                 row = 2, col = 3)
foo <- placeGrob(foo,
                 rectGrob(width = 0.6, 
                          gp = gpar(col="#FFFFCC",
                          fill = "#FFFFCC")), 
                 row = 1, col = 5)
foo <- placeGrob(foo,
                 textGrob(lab = "John"), 
                 row = 1, col = 2)
foo <- placeGrob(foo,
                 textGrob(lab = "Paul"), 
                 row = 2, col = 2)
foo <- placeGrob(foo,
                 textGrob(lab = "George"), 
                 row = 1, col = 4)
foo <- placeGrob(foo,
                 textGrob(lab = "Ringo"), 
                 row = 2, col = 4)
foo <- placeGrob(foo,
                 textGrob(lab = "The Beatles"), 
                 row = 1, col = 6)

xyplot(1 ~ 1, legend = list(top = list(fun = foo)))

##-----------------

HTH,

Deepayan




More information about the R-help mailing list