我正在从矩阵中生成一个水平图。我有另一个矩阵,与第一个矩阵的尺寸相同,带有标签。我想将标签添加到绘图中 - 正方形绘制值的颜色也应该显示标签。有没有办法做到这一点?提前谢谢。
例如,以下是代码。我希望矩阵 source 中的每个标签都显示在图形部分的顶部,对应于具有相同坐标的矩阵 value 中的相同数据。
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")
答案 0 :(得分: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)