Question

The problem:

I don't understand what format of x wireframe(x) is expecting. (wireframe is in the 'lattice' package)

This is the explanation in Help: "Both wireframe and cloud have methods for matrix objects, in which case x provides the z vector described above, while its rows and columns are interpreted as the x and y vectors respectively. This is similar to the form used in persp."

But I don't understand what that MEANS- can somebody please show me the example format of data that x should be.

The Details:

I am trying to make a 3-D surface plot using the lattice package. Why lattice?

-I have gotten a 3D scatterplot to work with the 'scatterplot3d' package, but as far as I can tell that package won't make surfaces.

-I cannot get the 'rgl' package to load on my computer. each time it says I need to download X11 and aborts my R session (what is X11?? It's some mac thing?)

My data is in a 66 row by 3 column matrix, where the columns are x, y, and z respectively. x and y are integers that form a grid, they go from 0:10 and 0:5 and cover every possible combination (so they are already an evenly spaced grid- I don't need to transform)

For example, the top of my matrix looks like this:

         X    Y    Z
[1,]      0    0 17437
[2,]      0    1  3627
[3,]      0    2  2329
[4,]      0    3  1790
[5,]      0    4  1426
[6,]      0    5  1207
[7,]      1    0  2851
[8,]      1    1  1343
[9,]      1    2   967

[10,] 1 3 794 ...and etc. with every X/Y combination

How do I need to transform my matrix so that it does not give me something totally screwy when I type wireframe(MyMatrix).

Thanks!!

Was it helpful?

Solution 2

The matrix I used was 3 columns with x, y, and z values. The correct format to enter into persp() or wireframe() is one with only the z values in the body of the matrix, x and y values are only the row and column labels. For example:

if I had:

          x y     z
     [1,] 0 0 17437
     [2,] 0 1  3627
     [3,] 0 2  2329
     [4,] 1 0  2851
     [5,] 1 1  1343
     [6,] 1 2   967
     [7,] 2 0  1681
     [8,] 2 1   906
     [9,] 2 2   689
    [10,] 3 0  1241
    [11,] 3 1   733
    [12,] 3 2   582

I needed to change it to:

        0   1   2
    0 1343 967 794
    1  906 689 591
    2  733 582 505
    3  630 509 438

where row names correspond to x values and column names correspond to y values. To enter this into the commands just type: persp(MatrixName) wireframe(MatrixName)

OTHER TIPS

If you have a grid of values that can be naturally coerced to a data.frame in long format, then why not use the formula version of wireframe?

 wireframe( Z ~ X + Y, data=dat)

You didn't give a reproducible example, but if that method choked then you could just wrap as.data.frame around the matrix object name. This would create a matrix that could be used for the matrix method:

M <- matrix(NA, 11,6)
M[dat[,1:2] <- M[,3]
rownames(M) <- 0:10
colnames(M) <- 0:5
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top