[R] Adding units to levelplot's colorkey

Carlisle Thacker Carlisle.Thacker at noaa.gov
Thu Nov 24 01:59:24 CET 2011


Here is an example with what I've been able to manage for a vertical 
colorkey:

library(lattice)        # make levelplot available
library(openair)      # make drawOpenKey available
# construct data
x = 1:10
y = rep(x,rep(10,10))
x = rep(x,rep(10))
z = x+y  # in centimeters

# try work-around, attaching annotation to 1st tick label
levelplot(z~x*y, 
colorkey=list(at=1:21,labels=c("1\n\n(cm)",as.character(2:21))))
# \n works but 1 is raised while (cm) is lowered

# try work-around, attaching annotation as 1st tick label
levelplot(z~x*y, 
colorkey=list(at=1:21,labels=c("(cm)",as.character(2:20),"")))
# not bad, best so far

# try work-around, attaching annotation as 0th tick label
levelplot(z~x*y, 
colorkey=list(at=0:21,labels=c("(cm)",as.character(1:20),"")))
# not better, but OK

# change colors using best so far
levelplot(z~x*y,
        
colorkey=list(at=1:21,labels=c("(cm)",as.character(2:20),""),col=rainbow(200)[1:180]),
        col.regions= rainbow(200)[1:180])
# rainbow had red at both ends, so used [1:180]

# start labels with additional spaces to line them up with units
levelplot(z~x*y,
      colorkey=list(at=1:21,labels=c("(cm)",paste(" 
",as.character(2:20)),""),col=rainbow(200)[1:180]),
      col.regions= rainbow(200)[1:180])
# looks good

Would be nice to have better control.

On 11/23/11 2:20 AM, Deepayan Sarkar wrote:
> On Tue, Nov 15, 2011 at 6:53 PM, Carlisle Thacker
> <Carlisle.Thacker at noaa.gov>  wrote:
>> Sorry that I was not clear.  I was asking how to add annotation to
>> levelplot's colorkey, not the levelplot itself.  The only entry I can
>> find from the help pages is via its labels.
>>
>> Googling did yield this:  " draw.colorkey() doesn't support a title for
>> the legend".  So I presume there is also no support for any other
>> annotation.
> That is correct. Note that having support for more annotation is not
> technically difficult, but it's not clear what a good design would be.
>
> For the purpose you describe, I typically do something like
>
> levelplot(volcano, colorkey = list(space = "top"),
>            main = "Height (cm)")
>
> But I don't know how to do this nicely with a vertical color key.
>
> -Deepayan



More information about the R-help mailing list