2016-05-30 11 views
2

Ich habe eine Matrix mit Position (X, Y, Elevation). Ich füge eine Spalte zu der Matrix hinzu, die ich "Index" nenne. Ich erstelle einen X- und einen Y-Vektor aus der Matrix. Sie enthalten beide die Indexspalte. Ich sortiere dann aufsteigend den X- und Y-Vektor, den ich gerade gemacht habe. Ich konstruiere dann eine Z-Matrix, die die Höhe enthält, und ich verknüpfe sie mithilfe des Index mit der Position. Ich versuche dann, die Befehlskontur zu verwenden (ich möchte ein Konturdiagramm zeichnen) und bekomme den Fehler, dass X und Y aufsteigende Reihenfolge sein sollten ... was ich gerade gemacht habe !!! Was habe ich falsch gemacht?Kontur - Plot - aufsteigende Reihenfolge

noeud<-read.table("position.out") 
Matrice_Noeud<-matrix(ncol = ncol(noeud), nrow=nrow(noeud)) 
for (i in 1:nrow(noeud)) { 
    for (j in 1:ncol(noeud)) { 
    Matrice_Noeud[i,j]<-noeud[i,j] 
    } 
} 

Matrice_Noeud <- cbind(Matrice_Noeud, c(seq(1,nrow(noeud),1))) 


x<-data.frame(x=Matrice_Noeud[,1],Index=Matrice_Noeud[,4]) 
y<-data.frame(y=Matrice_Noeud[,2],Index=Matrice_Noeud[,4]) 

X<-x[order(x$x),] 
Y<-y[order(y$y),] 


Z<-matrix(NA, ncol=nrow(noeud),nrow=nrow(noeud)) 
for (x_i in 1:nrow(noeud)) { 
    for (y_i in 1:nrow(noeud)) { 
    if (Y$Index[y_i]==X$Index[x_i]) { 
     niveau<-which(Matrice_Noeud[,4]==Y$Index[y_i]) 
     Z[x_i,y_i]<-Matrice_Noeud[niveau,3] 
    } 


    } 
    } 

Xx<-array(X[,1]) 
Yy<-array(Y[,1]) 
Zz<-data.frame(Z) 

contour(Xx,Yy,Zz) 
+0

'Länge (unique (noeud [1])) * Länge (unique (noeud [2]))' '= nRow (noeud)' ?? Wenn nicht, kann contour() die Daten nicht behandeln. – cuttlefish44

+0

Nein, sind sie nicht. Länge (einmalig (noeud [, 1]) = 19; Länge (einmalig (noeud [, 2]) = 12 nrow (noeud) = 121 – John

+0

Ich verstehe nicht ganz, warum sie gleich sein müssten? Matrix Z so, dass sie einem Gitter mit X und Y entspricht und entsprechende Werte von Z für ihre Anfangspositionierung relevant platziert werden ... Ich bin verwirrt. Würdest du mir das erklären? (tut mir leid wenn die Frage ist ein kleiner Dummkopf, aber ich bin gerade sehr verwirrt.) – John

Antwort

3

OK, seit ich damit angefangen habe, habe ich es getan.

#### making example data 
## assumptions: length(unique(x))=19, length(unique(y))=12, nrow(data)=121 
## (They mean the number of grid points is 19 * 12 = 228, but z.value is only 121.) 
xyz.f <- function(m, n) - m + (n - 7)^2 + 16   # make z from x and y (it means nothing special) 
xyz <- cbind(xyz <- expand.grid(x = round(seq(11,15,,19), 2), y = round(seq(6,10,,12), 2)), 
       z = apply(xyz, 1, function(k) xyz.f(k[1], k[2]))) 
set.seed(1); ind <- sample(19*12, 121)    # decide to use the 121 z of 19*12 
noeud <- as.matrix(xyz[ind,])      # example data maked out 

#### making contour()'s arguments 
Xx <- sort(unique(noeud[,1])) 
Yy <- sort(unique(noeud[,2])) # nrow(noeud); length(Xx); length(Yy) # OK (121, 19, 12) 

Zz <- matrix(NA, ncol=length(Yy), nrow=length(Xx))   # make 19 x 12 Z matrix (empty) 
# In each row, calculate x (y) value is what number in Xx (Yy) (= the position in Z matrix) 
X0 <- as.numeric(factor(noeud[,1])) # (edit) using Mr.Tufte's code in R help mailing. 
Y0 <- as.numeric(factor(noeud[,2])) 
apply(cbind(X0, Y0, noeud[,3]), 1, function (a) Zz[ a[1], a[2] ] <<- a[3]) 
## contour()'s arguments (Xx, Yy, Zz) maked out  
contour(Xx, Yy, Zz, xlab="including NAs") # length(Zz); length(Zz[!is.na(Zz)]) # OK (228,121) 

#### interpolating 
## I know few packages having interpolation functions. 
library(akima)      # use cubic spline interpolation methods of H. Akima 
NOEUD <- interp(noeud[,1], noeud[,2], noeud[,3]) 

#### results 
par.old <- par(no.readonly=T); par(mfrow=c(1,3), mar=c(4,0,1,0)) 
contour(Xx, Yy, Zz, xlab="including NAs", yaxt="n") # the including NAs data 
contour(NOEUD, xlab="Akima interpolation", yaxt="n") # the Akima interpolation data 
contour(Xx, Yy, matrix(xyz[,3], nrow=19), xlab="origin", yaxt="n") # the origin data 
# (edit) I noticed some interp()'s arguments make a difference (default: linear=T, extrap=F). 
contour(interp(noeud[,1], noeud[,2], noeud[,3], linear=T, extrap=F), xlab="Akima interp() default") 
contour(interp(noeud[,1], noeud[,2], noeud[,3], linear=F, extrap=F), xlab="interp(linear=F)") 
contour(interp(noeud[,1], noeud[,2], noeud[,3], linear=F, extrap=T), xlab="interp(linear=F, extrap=T)") 
par(par.old) 

### supplement (using the same data, output is about the same) 
noeud2 <- data.frame(x=noeud[,1], y=noeud[,2], z=noeud[,3])  # equal to the including NAs data 
NOEUD2 <- cbind(expand.grid(x=NOEUD$x, y=NOEUD$y), z=c(NOEUD$z)) # equal to the Akima interpolation data 
ggplot2::ggplot(noeud2, aes(x, y, z = z)) + geom_contour() 
lattice::contourplot(z ~ x * y, NOEUD2) 

plot plot

+0

Vielen Dank! Ich hatte es geschafft, aus Ihrem letzten Kommentar die richtige Z-Matrix zu erstellen (228), aber ich hatte Probleme mit der Interpolation. Sehr hilfreich, nochmals vielen Dank! – John

+0

Ich frage mich auch, ob die Art, wie Sie die Z-Matrix erstellen, der Art und Weise entspricht, wie ich es mache. Meine ist sehr lang (mit drei für Schleife), während Sie sehr einfach scheint. [Bearbeitet]: Der Code ist hässlich in dem Kommentar ... wo sollte ich es posten, um klarer zu sein? [Bearbeitet]: Ich habe nur überprüft und sie sind gleichwertig. Danke für die Hilfe, ich werde versuchen, Ihre Art der Programmierung zu verstehen, es ist viel effizienter !! – John

+0

Ich bin froh, dass es dir gefällt. Ich fühle auch, dass der Code, der X0 und Y0 macht, technisch ist. 'X0 <- sapply (noeud [, 1], Funktion (a) welche (Xx == a))' ist äquivalent und wird dir nutzen. – cuttlefish44