您可以嘗試獲取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。