0

I am producing a levelplot from a matrix. I have another matrix, of the same dimensions of the first one, with labels. I would like to add the labels to the plot -the square plotting a color for the value should also show the label. Is there a way to do that? Thanks in advance.

As an example, here is the following code. I would like each label in matrix source to be shown on top of the part of the graph corresponding to the same data in matrix value with the same coordinates.

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")
user2345448
  • 159
  • 2
  • 11
  • 1
    Please provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and what you mean by "add the labels". Do you mean labels of `contourplot`? Something else? –  Feb 02 '15 at 02:45

1 Answers1

4

Here is one possibility:

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)

enter image description here