0

我正在从矩阵生成水平图。我有另一个矩阵,与第一个矩阵的尺寸相同,带有标签。我想将标签添加到绘图中 - 为值绘制颜色的正方形也应该显示标签。有没有办法做到这一点?提前致谢。

例如,这里是以下代码。我希望矩阵中的每个标签都显示在图形部分的顶部,该部分对应于具有相同坐标的矩阵值中的相同数据。

library(lattice)

value <- matrix(data=2^seq(from=0.5,to=2,length.out=9),ncol=3,nrow=3)

colnames(value)<-c("wheat","barley","rice")

rownames(value)<-c("1970","1980","1990")

source <- matrix(data=c("A","A","B","A","B","C","C","B","C"),ncol=3,nrow=3)

levelplot(value,xlab="year",ylab="comodity",main="some plot")
4

1 回答 1

4

这是一种可能性:

library(lattice)
library(latticeExtra)

dat <- data.frame(expand.grid(x = c(1970, 1980, 1990), y = c("wheat","barley","rice")), 
              value = 2^seq(from=0.5,to=2,length.out=9), source = c("A","A","B","A","B","C","C","B","C"))

Obj <- 
  levelplot(value ~ x+y, data = dat, xlab = "year", ylab = "comodity", main = "some plot") + 
  xyplot(y ~ x, data = dat,
   panel = function(y, x, ...) {
           ltext(x = x, y = y, labels = dat$source, cex = 1, font = 2,
           fontfamily = "HersheySans")
   })

print(Obj)

在此处输入图像描述

于 2015-02-02T06:01:18.953 回答