Skip to content
Prev 278351 / 398502 Next

Adding units to levelplot's colorkey

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: