R:格函数中的图例

R: legends in lattice function

我用R的lattice包可视化了一个矩阵,得到了一个10x10的网格。

不幸的是,我在微调方面确实遇到了一些问题。

颜色条需要一个解释性标题(垂直写在它旁边)。

这里是一个示例代码,看看它现在的样子加上一张图片。

library(lattice)

#Build the horizontal and vertical axis information

hor=c("0.0005", "0.001", "0.005", "0.01", "0.05", "0.1", "0.5", "1", "5", "10")
ver=c("1000","2000","3000","4000","5000","6000","7000","8000","9000","10000")

nrowcol=length(ver)
cor = matrix(runif(nrowcol*nrowcol, min=0.4), nrow=nrowcol, ncol=nrowcol, dimnames = list(hor, ver))
for (i in 1:nrowcol) cor[i,i] = 1

rgb.palette <- colorRampPalette(c("blue", "yellow"), space = "rgb")
levelplot(cor, col.regions=rgb.palette(120), cuts=100, at=seq(0,1,0.01),
          xlab=expression("DAG depletion rate k"[B49] *" [ s"^"-1"*" ]"),
          ylab=expression("PKC activation rate k"[D5] *" [ l / (mol*s) ]"))

你猜我该如何解决这些小问题?

最好,非常感谢!

我不知道如何使用 levelplot() 添加图例,但是使用 ggplotgeom_tile 重新创建您的情节非常简单。这是我得到的:

library("ggplot2")
this = melt(cor, id.vars = row.names(cor))
ggplot(this) + 
  geom_tile(aes(x = factor(Var1), y = factor(Var2), fill = value)) + 
  scale_fill_continuous(high = "yellow", low = "slateblue4", name = "Your Legend Title") +
  ggtitle("Your Title") + 
  xlab(expression("DAG depletion rate k"[B49] *" [ s"^"-1"*" ]")) + 
  ylab(expression("PKC activation rate k"[D5] *" [ l / (mol*s) ]")) + 
  theme_bw()

ggplot 中的微调有很好的记录,因此您可能能够做您需要的事情。完全披露:如果您的 x 和 y 坐标不是均匀分布的,geom_tile 将不会总是知道如何填充空间。注意到我必须将 Var1 和 Var2 转换为因子。 post 给出了一些关于如何处理这个问题的好主意。

根据需要添加这些行来调整 x 和 y 位置:

library(grid)
grid.text("Here is some text explaining the legend", .77, .5, rot = 270)