如何在水平图上显示矩阵值?

时间:2023-01-18 12:51:24

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.

我该怎么做呢?谢谢。

如何在水平图上显示矩阵值?

3 个解决方案

#1


9  

The problem with the code in the answer you linked to is that it only works when the objects in the levelplot's formula are named x, y, and z.

在你链接到的答案中,代码的问题是它只在levelplot的公式中的对象被命名为x、y和z时才有效。

Here is an example that uses a more standard idiom for processing the arguments passed in to the custom panel function and so 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)

如何在水平图上显示矩阵值?

#2


4  

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)

如何在水平图上显示矩阵值?

#3


2  

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:

另一个选项是使用latticeExtra的图层()。它允许你在另一个图上覆盖一个图,使用与ggplot2爱好者熟悉的+操作符:

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)

#1


9  

The problem with the code in the answer you linked to is that it only works when the objects in the levelplot's formula are named x, y, and z.

在你链接到的答案中,代码的问题是它只在levelplot的公式中的对象被命名为x、y和z时才有效。

Here is an example that uses a more standard idiom for processing the arguments passed in to the custom panel function and so 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)

如何在水平图上显示矩阵值?

#2


4  

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)

如何在水平图上显示矩阵值?

#3


2  

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:

另一个选项是使用latticeExtra的图层()。它允许你在另一个图上覆盖一个图,使用与ggplot2爱好者熟悉的+操作符:

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)