How to show matrix values ​​on Levelplot

I have matrix data here and I visualized it using levelplot . The plot is located below. But I just could not put the meaning into the plot, I mean that I read this question , but still could not understand.

How can i do this? Thanks.

+3
r data-visualization visualization lattice
source share
3 answers

The problem with the code in the answer you contacted is that it only works when the objects in the levelplot formula are called x , y and z .

Here is an example that uses a more standard idiom to process arguments passed to a panel custom function, and therefore becomes more generally applicable:

 library("lattice") ## Example data x <- seq(pi/4, 5*pi, length.out=10) y <- seq(pi/4, 5*pi, length.out=10) grid <- expand.grid(X=x, Y=y) grid$Z <- runif(100, -1, 1) ## Write a panel function (after examining 'args(panel.levelplot) to see what ## will be being passed on to the panel function by levelplot()) myPanel <- function(x, y, z, ...) { panel.levelplot(x,y,z,...) panel.text(x, y, round(z,1)) } ## Try it out levelplot(Z ~ X*Y, grid, panel = myPanel) 

enter image description here

+9
source share
 mat <- read.csv("J_H2S1T6_PassTraffic.csv", header=F) y <- as.numeric(mat[1,-1]) mat <- mat[-1,-1] n <- dim(mat)[1] 

Here's a modification, I'm creating 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 is a modification. I change the dimension of the matrix to a vector to fit it into 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

+4
source share

Another option is to use layer() from the Extra grid . This allows you to overlay one graph 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) 
+2
source share

All Articles