[R] both lines and plot characters in a lattice plot key

Deepayan Sarkar deepayan.sarkar at gmail.com
Tue Dec 25 22:45:39 CET 2007


On 12/25/07, Hofert Marius <m_hofert at web.de> wrote:
> Hello,
>
> I have two short questions concerning a key (legend) in a lattice
> plot. I have points represented by two different plot characters
> (crosses and circles) and they are colored from light to dark gray. I
> would like to have 3 parts in my legend showing the two different
> plot characters in the first two lines and the last line should
> consist of a line segment for explaining the different shades of
> gray. Below you can see what I reached so far.
> My first problem is, that I do not get the 2 plot characters and the
> line segment vertically aligned to each other (I chose the vector of
> plotting characters to contain "26" as last element, because this
> leaves out a plotting character for the last line, where the line
> segment should be placed). So how do I get these aligned?

The 'lines' components allow a 'type' specification, so:

       key =
       list(x=0.2,y=0.2,
            lines=list(pch=c(1,3,1), type = c("p", "p", "l")),
            text=list(c("key 1","key 2","key 3")),
            align=T,transparent=T)

> My second question is, if it's possible to have a line segment shown
> in the key, which is colored from light to dark gray (instead of a
> single color), matching the colors in my specified variable "colors"
> if possible, i.e. a single line segment starting from light gray and
> ending with a dark gray.

Unfortunately, no, at least not using the 'key' argument.  However,
using the 'legend' argument gives you more flexibility, especially if
you are willing to write some grid code. Even without grid, you can
use draw.key() and draw.colorkey() together to produce one legend for
the plotting characters and a different one for the color gradient.

-Deepayan

>
> Thanks very much in advance!
>
> Jan
>
> library(lattice)
> x=c(1,2,3,4,5)
> y=c(5,4,3,2,1)
> symbolvec=c(3,3,3,1,3)
> colorseq=seq(0.25,0.88,length=5)
> colors=gray(colorseq)
> colorvec=colors
> xyplot(y~x,
>         panel=function(...){
>                 panel.xyplot(...,type="n")
>                 lpoints(x,y,pch=symbolvec,col=colorvec)
>         },
> key=list(x=0.2,y=0.2,lines=list(lty=c(0,0,1)),points=list(pch=c
> (1,3,26)),text=list(c("key 1","key 2","key 3")),align=T,transparent=T))
>
> ______________________________________________
> 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