[R] Alignment and Labeling of a color key in a xyplot?

Deepayan Sarkar deepayan.sarkar at gmail.com
Tue Jan 1 17:50:25 CET 2008

On 1/1/08, Hofert Marius <m_hofert at web.de> wrote:
> Happy New Year to all R users!
> I have two short questions concerning a xyplot with a color key:
> 1) How do I properly place (align) the color key beside the xyplot?
> As you can see from the code listed below, the placement of the color
> key is not correct. I would like the upper and lower end point of the
> color key to be perfectly aligned with the upper and lower line of
> the xyplot. Adjusting the viewport-arguments below is tedious and
> also does not help if you want the print the plot in a postscript
> file (see the commented lines below).

It's easier if you include the legend in the call (so that
print.trellis can do the layout management); e.g., see


> 2) How do I put a label on the color key?
> I would like to give the color key a label as the label 'x' for the y-
> axis, so it should be vertically printed and located to the right of
> the color key. I tried to give the 'key' argument in 'draw.colorkey'
> some arguments, but I could not manage to put a label on the color key.

This is not supported by draw.colorkey(). You can still define your
own legend, of course.


> Thanks very much.
> Marius
> library(lattice)
> library(grid)
> x=c(1,2,3)
> colorseq=c(0.2,0.5,0.8)
> mycolors=gray(colorseq)
> #trellis.device(postscript,horizontal=F,onefile=F,file="~/testplot.ps")
> xyplot(x~x,col=mycolors,aspect=1)
> draw.colorkey(key=list(at=seq(1,3,length=4),col=mycolors,text=list(c
> ("test"))),draw=TRUE,vp=viewport(x=1,y=0.6,height=0.8))
> #dev.off()
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list