Skip to content
Prev 67678 / 398506 Next

Legend in xyplot two columns

On Thursday 14 April 2005 05:30, Gesmann, Markus wrote:
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