2015-10-20 67 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。

相关问题