[R] Creating a flat legend 'grob' for lattice xyplot

David Winsemius dwinsemius at comcast.net
Wed Jul 27 20:01:02 CEST 2011

On Jul 27, 2011, at 1:31 PM, Mark wrote:

> Hi,
> I want my xyplot legend to be flat, not tall, and there seems to be  
> no way
> for xyplot's auto.key and key elements to do this: I tried many, many
> permutations of what I could find in the archives and reading the
> documentation.  If there there's a way to make it flat, please tell  
> me what
> the magic incantation is.
> Here's a simple example of what I like to see.  The xyplot will be a
> multi-panelled one with a single,
> flat legend below.  It seems the xyplot's legend element is the way  
> to go --
> the option I haven't tried because I don't know what or how to  
> create a grob
> of this kind.  Where to start?
> xyplot(1~1)   # a lattice function
> legend(0.15,-0.125,    # a base graphics function
> bty='n',pch=rep(c(15,17), 
> 2),col=c('brown','green','blue','red'),ncol=4,
>          legend=c('How','to make','this into', 'a grob?'))

It is possible to define a legend object inside xyplot but using the  
base graphics legend() is NOT how it is done. You should provide a  
sample data.frame and someone will show you how to do it. The example  
in Sarkar's Lattice book that does it uses barchart and specifies  
auto.key with a columns= argument. It's fig 10.2 and you can find the  
examples on the website. It's also described in the key section of ? 
xyplot .... I just looked.

David Winsemius, MD
West Hartford, CT

More information about the R-help mailing list