[R] Constructing an additional key inside of a lattice panel

Deepayan Sarkar deepayan.sarkar at gmail.com
Wed Aug 17 14:57:41 CEST 2011


On Tue, Aug 16, 2011 at 4:35 PM, Fredrik Karlsson <dargosch at gmail.com> wrote:
> Hi,
>
> I would like to add an additional key inside of a panel based on a factor
> that is not the "groups" argument.
> I've tried using the panel.key function in latticeExtras, but I cannot get
> the line types the way I want it.
>
> Using my factor myGroups, I've tried this:
>
> panel.key(text=levels(myGroups),lines=TRUE,points=FALSE,corner =
> c(0,.98),key=list(lines=list(lty=1:length(levels(myGroups)))))
>
> I then get the key where I want it, the text is right, but line types are
> not correct (always lty=1, I think).
>
> Any ideas on how I could solve this?

Well, trying to add undocumented arguments and hoping they will
magically work usually doesn't help.

panel.key() works using simpleKey(), which by design is simple but not
flexible. In particular, it will not allow you to set 'lty' directly,
and instead use the values from the theme currently in use.

You _can_ change the theme also, using a different argument; e.g.,

library(lattice)
library(latticeExtra)

xyplot(1 ~ 1,
       panel = function(...) {
           panel.xyplot(...)
           panel.key(text = month.name[1:2],lines=TRUE,points=FALSE,
                     corner = c(0,.98))
       },
       par.settings = simpleTheme(lty = 1:2))

But I don't know if that interferes with the rest of your call.

If all else fails, panel.key() is not a very complicated function, so
you can take inspiration from it and write your own version that
replaces the line

    key <- simpleKey(text, ...)

with

    key <- <something else>

where <something else> describes the legend that you want.

-Deepayan



More information about the R-help mailing list