How to show matrix values on Levelplot

后端 未结 3 979
甜味超标
甜味超标 2020-12-05 17:04

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

3条回答
  •  一向
    一向 (楼主)
    2020-12-05 17:22

    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

提交回复
热议问题