[R] Legend in xyplot two columns

Gesmann, Markus Markus.Gesmann at lloyds.com
Thu Apr 14 17:29:05 CEST 2005


Thanks Deepayan!

Your solution does excatly what I want. 
Further experiments and thoughts on my side brought me also to a
solution. 
If I use the option rep=FALSE, and plot the bullit with "lines" and
split the "lines" argument into two groups it gives me the same result,
as every item in the key list starts a new column.

library(lattice)
key <- list( rep=FALSE,
               lines=list(col=c("red", "blue"), type=c("p","l"),
pch=19),
               text=list(lab=c("John","Paul")),
               lines=list(col=c("green", "red"), type=c("l", "l")),
               text=list(lab=c("George","Ringo")),
               rectangles = list(col= "#FFFFCC", border=FALSE),
               text=list(lab="The Beatles"),
		   )

xyplot(1~1, key=key)


But your solution is much more felxible!

Kind Regards

Markus

-----Original Message-----

************LNSCNTMCS01***************************************************
The information in this E-Mail and in any attachments is CONFIDENTIAL and may be privileged.  If you are NOT the intended recipient, please destroy this message and notify the sender immediately.  You should NOT retain, copy or use this E-mail for any purpose, nor disclose all or any part of its contents to any other person or persons.

Any views expressed in this message are those of the individual sender, EXCEPT where the sender specifically states them to be the views of Lloyd's.

Lloyd's may monitor the content of E-mails sent and received via its
network for viruses or unauthorised use and for other lawful
business purposes."

Lloyd's is authorised under the Financial Services and Markets Act 2000
********************************************************************************

From: Deepayan Sarkar [mailto:deepayan at stat.wisc.edu] 
Sent: 14 April 2005 16:01
To: r-help at stat.math.ethz.ch
Cc: Gesmann, Markus
Subject: Re: [R] Legend in xyplot two columns


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