您可以尝试获取Matrix中的值列。这可以在for循环中完成。但为此,我做了一个假设,即在您的数据中,变量y.cart和x.cart中的y和x值不是唯一的。我这样做是因为我认为你有类似地图的东西,并且在这张地图上,网格中的每个点都是一对坐标。
这是正确的,你可以试试这个代码:
# Some sample data:
y.cart <- x.cart <- seq(-60,60,length.out = 600)
# Bring it in the form like your data are:
DF <- data.frame(x.cart = sample(x = x.cart, length(x.cart)^2, replace = TRUE),
y.cart = sample(x = y.cart, length(y.cart)^2, replace = TRUE),
value = rnorm(length(y.cart)^2))
# Also works for a Matrix:
DF <- as.matrix(DF)
# Define the Matrix Z. In this Matrix are just NAs, because if a value on a
# special coordinate doesn't exist there should be nothing drawn:
Z <- matrix(rep(NA,length(DF[,1])^2), nrow = length(DF[,1]))
# Get the unique points which represent the x and y coordinate. It's important
# to use the unique points for getting the index for the Matrix out of this vectors:
x <- sort(unique(DF[,1]))
y <- sort(unique(DF[,2]))
# In this loop every row in de data.frame (or matrix) is matched with the vector
# x for the i-th row in the Matrix and with the vector y for the j-th column in
# the Matrix Z[i,j]:
for(i in seq(along = DF[,1])) {
Z[which(x == DF[i,1]),which(y == DF[i,2])] <- DF[i,3]
}
# Now you can use persp or filled.contour with the following call:
persp(x,y,Z)
filled.contour(x,y,Z)
这适用于我的样本数据,尽管它没有任何意义了他们。请牢记,for循环不是很快,并且对于您的数据可能需要一段时间。您可以在进程条建立从与环CONTROLE状态:
pb <- txtProgressBar(min = 1, max = length(DF[,1]), style = 3)
for(i in seq(along = DF[,1])) {
Z[which(x == DF[i,1]),which(y == DF[i,2])] <- DF[i,3]
setTxtProgressBar(pb, i)
}
而且这是必要的,X和Y具有相同的长度和矩阵Z与尺寸lenght(x)和长度的矩阵( Y)。
我希望这对你有用。如果我对数据的想法不是真的,那么可以给出关于数据的更多细节。并且不要忘记用您的矩阵的名称替换DF。