2015-10-20 34 views
0

我有一個288000x3的矩陣(288000行,3列)x和y笛卡爾座標從-60到60,小數點跟隨8個位置以及這些座標值。我可以使用filled.contour繪製十進制笛卡爾座標數據嗎?

例 -

 y.cart  x.cart  value 
[1,] 0.001308930 0.07498858 -49.36752 
[2,] 0.002617462 0.07495431 -48.33903 
[3,] 0.003925197 0.07489722 -51.42450 
[4,] 0.005231736 0.07481730 -51.93874 
[5,] 0.006536681 0.07471460 -513.73075 
[6,] 0.007839635 0.07458914 -52.45299 
[7,] 0.009140201 0.07444096 -51.93874 
[8,] 0.010437983 0.07427011 -48.85327 
[9,] 0.011732585 0.07407663 -49.36752 
[10,] 0.013023613 0.07386058 -50.91025 

這是氣象雷達的反射率數據,我需要繪製它看起來像filled.contour創建輸出,但爲了使用filled.contour,該值需要在一個矩陣中,因爲該函數使用矩陣位置作爲繪圖的座標,而這與我的數據組織方式無關。有沒有辦法在這種形式下使用filled.contour中的數據,或者有另一種方法來做到這一點?我已經擺弄了兩天,並沒有走得很遠。任何幫助將不勝感激。

回答

1

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