Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show matrix values on Levelplot

I have a matrix data here, and I visualized it with levelplot. The Plot is placed below. But I just couldn't put the values into the plot, I mean I read this question, but still couldn't figure it out.

How can I do that ? Thanks.

like image 536
forochelian Avatar asked Dec 15 '12 19:12

forochelian


2 Answers

Another option is to use layer() from latticeExtra. It allows you to overlay one plot on top of another, using the + operator familiar to ggplot2 enthusiasts:

library(latticeExtra)

## Applied to the example data in my other answer, this will produce
## an identical plot
levelplot(Z ~ X*Y, data = grid) +
layer(panel.text(X, Y, round(Z, 1)), data = grid)
like image 156
Josh O'Brien Avatar answered Oct 07 '22 16:10

Josh O'Brien


mat <- read.csv("J_H2S1T6_PassTraffic.csv", header=F)

y        <- as.numeric(mat[1,-1])
mat      <- mat[-1,-1]
n        <- dim(mat)[1]

Here a modification, I generate a new scale

x <- seq(min(y), max(y), length.out=n)
grid     <- expand.grid(x=x, y=x)
mat      <- as.matrix(mat)
dim(mat) <- c(n*n,1)
grid$z   <- mat

Here the modification. I change the dimension of the matrix to a vector to put it in the grid .

mat <- as.matrix(mat)
dim(mat) <- c(n*n,1)
grid$z <- mat

p <- levelplot(z~x*y, grid, 
           panel=function(...) {
             arg <- list(...)
             panel.levelplot(...)
             panel.text(arg$x, arg$y,arg$z)},
           scales = list(y = list(at=y,labels=y),
                         x = list(at=y,labels=y)))

print(p)

enter image description here

like image 4
agstudy Avatar answered Oct 07 '22 15:10

agstudy