[R] Key colour problem with lattice plot

Deepayan Sarkar deepayan.sarkar at gmail.com
Tue Oct 24 00:52:20 CEST 2006


On 10/23/06, Alex Brown <alex at transitive.com> wrote:
> >>
> >>         Can anyone explain why the key is inverted versus the bar
> >> order?
> >
> > Because the key goes from top to bottom, while cartesian coordinates
> > go from bottom to top. Neither can really be any other way by default,
> > if you think about it. I did promise someone that I would add an
> > option to reverse the order in key, but looks like I haven't done that
> > yet.
> >
>
> In the case of barchart, what would be the justification for not
> ordering the bars (in a group) as for the key, or vice versa?  Please
> forgive my ignorance - I am curious to understand the design
> principles.  I am certainly very pleased to have the power of
> auto.key in lattice - it's the main reason why I stopped using the
> default plot function.

No particular reason, it's just (I think) one of the weaknesses in the
Trellis design. There is no actual communication between legends and
panel functions. Basically, the flexibility of Trellis comes from
almost completely separating the various elements of a graphic, and
you get an illusion of consistency if all the elements are allowed to
derive their defaults from the same settings. This is the reason there
will never be (without wrappers or other kinds of hacks) any hope of
'auto.key' ``detecting'' when you give a

col=c('green', 'red')

argument to xyplot (or panel.xyplot) and changing colors automatically
in response.

If you think about it, panel.barchart does the only reasonable thing,
namely order levels in the "right" direction. If you want things to be
ordered from top to bottom, you can reverse the axes:

barchart(as.matrix(example), stack = FALSE, ylim = c(14, 0))

which will lose the nice labels, but that's again a whole different
element of the graph. Changing the order for bars within y-levels is
one option, but would you really want that when the bars are vertical?

Similarly, the legend also does the only reasonable thing. It should
have an option to reverse the order, and it will in the next version
of lattice (which I plan to upload within a couple of days), where you
should be able to say:

barchart(as.matrix(example), stack = FALSE,
         auto.key = list(reverse.rows = TRUE))

-Deepayan



More information about the R-help mailing list