5

我在这里有一个矩阵数据,我用 levelplot. 地块放在下面。但我只是无法将这些值放入情节中,我的意思是我读了这个问题,但仍然无法弄清楚。

我怎样才能做到这一点 ?谢谢。

4

3 回答 3

12

您链接到的答案中的代码问题在于,它仅在 levelplot 公式中的对象命名为xy和时才有效z

这是一个使用更标准的习惯用法来处理传入自定义面板函数的参数的示例,因此变得更普遍适用:

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)

在此处输入图像描述

于 2012-12-15T20:10:07.017 回答
4
mat <- read.csv("J_H2S1T6_PassTraffic.csv", header=F)

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

这里有一个修改,我生成一个新的比例

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

这里修改。我将矩阵的维度更改为向量以将其放入网格中。

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)

在此处输入图像描述

于 2012-12-15T20:31:04.883 回答
2

另一种选择是使用layer()from 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)
于 2012-12-15T20:27:44.840 回答