如何在xyplot(网格包)中添加标题到colorbar图例?

时间:2014-10-01 19:54:34

标签: r lattice

我在Deepayan Sarkar的Lattice书中复制了图5.6,尽管使用了一个单独的面板。目前,我正在努力在彩条上方找到标题......

这是一些可重现的代码。

x <- seq(1,20,.1)
y <- rnorm(length(x))
z <- seq(1,200,length.out=length(x))

cols <- rainbow(100)[cut(z,100,label=F)]

xyplot(y~x, aspect="iso", type=c("p","g"), col=1, pch=21,fill=cols,
   legend=list(right=
       list(fun = draw.colorkey,
            args=list(key=list(col = rainbow,at = do.breaks(range(z),100))))),
panel=function(x,y,...){
panel.xyplot(x,y,...)
}
)

有任何线索吗?谢谢,

A

1 个答案:

答案 0 :(得分:3)

由于某种原因,似乎draw.colorkey不尊重title=列表的key=属性。但是你可以指定自己的grob。在这里,我们使用packGrob(来自grid)修改标准colorkey grob。试试这个

library(grid)
my.legend <- packGrob(
    draw.colorkey(key=list(col = rainbow, at = do.breaks(range(z),100))), 
    textGrob("My title", x = 0, y = 0.5, just = c("left", "centre")), 
    height=unit(2, "lines"),side="top", dynamic=T)

xyplot(y~x, aspect="iso", type=c("p","g"), col=1, pch=21,fill=cols,
    legend=list(right=list(fun=my.legend)))

enter image description here