Skip to content

persp() problem

4 messages · John Benning, David Winsemius, Rolf Turner +1 more

#
On Nov 19, 2011, at 3:19 PM, John Benning wrote:

            
Unable to reproduce. Maybe z is not a 5 x 5 matrix?

 > x <-scan()
1:  0  5 10 15 20
6:
Read 5 items
 >  y <-scan()
1:   0  5 10 15 20
6:
Read 5 items
 >  z <-matrix(scan(),5,5);
1:  4.538 9.169  23.518 32.794 39.322
6:  5.013 13.104 33.282 42.202 45.474
11:  5.296 17.728 36.989 43.89  46.57
16:  5.366 20.94  39.244 44.987 47.177
21:  5.642 23.396 40.842 45.489 47.126
26:
Read 25 items
 > persp(x,y,z)

Attached.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Rplot.pdf
Type: application/pdf
Size: 21985 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20111119/6cd5cfc0/attachment.pdf>
-------------- next part --------------


David Winsemius, MD
West Hartford, CT
#
Your call to persp() is fine, and works just fine for me.
Obviously there is something funny about your data
(x, y, and z).   They must not be numeric (despite appearances).
There is something you haven't told us here; how did you
obtain/construct x, y, and z?

Try the following:

x <- y <- 5*(0:4)
z <- matrix(scan(textConnection(
         "4.538 9.169  23.518 32.794 39.322
          5.013 13.104 33.282 42.202 45.474
          5.296 17.728 36.989 43.89  46.57
          5.366 20.94  39.244 44.987 47.177
          5.642 23.396 40.842 45.489 47.126")),5,5,byrow=TRUE)
closeAllConnections()
persp(x,y,z)
readline("Go? ")

# Or better:
persp(x,y,z,theta=-30,phi=40,d=4)

     cheers,

         Rolf Turner
On 20/11/11 09:19, John Benning wrote:
#
The trailing space (instead of '0') in the
third line of the printout of z, the fact that
the decimal points are not aligned in the columns,
and the left- justification of the column labels
are hints that z is not a matrix of numbers, but just
prints something like one.
  *> z*
  *     [,1]  [,2]   [,3]   [,4]   [,5]  *
  *[1,] 4.538 9.169  23.518 32.794 39.322*
  *[2,] 5.013 13.104 33.282 42.202 45.474*
  *[3,] 5.296 17.728 36.989 43.89  46.57 *
E.g., compare the printouts of the following:
  > pNumeric <- matrix((1:6)*5.125, 3, 2)
  > pCharacter <- noquote(matrix(as.character((1:6)*5.125), 3, 2))
  > pNumeric
         [,1]   [,2]
  [1,]  5.125 20.500
  [2,] 10.250 25.625
  [3,] 15.375 30.750
  > pCharacter
       [,1]   [,2]  
  [1,] 5.125  20.5  
  [2,] 10.25  25.625
  [3,] 15.375 30.75
This is why R-help encourages people to show datasets
using the output of dput(), which shows more of the
actual structure of the data.
  > dput(pNumeric)
  structure(c(5.125, 10.25, 15.375, 20.5, 25.625, 30.75), .Dim = c(3L, 
  2L))
  > dput(pCharacter)
  structure(c("5.125", "10.25", "15.375", "20.5", "25.625", "30.75"
  ), .Dim = c(3L, 2L), class = "noquote")

persp() gives odd error messages when x, y, or
z is not numeric; it does not just say that character
data is not allowed.

Try reentering your data and looking at the
output of str(z), str(x), and str(y) to make
sure they are all numeric and that z is a matrix.
  > str(pNumeric)
   num [1:3, 1:2] 5.12 10.25 15.38 20.5 25.62 ...
  > str(pCharacter)
   'noquote' chr [1:3, 1:2] "5.125" "10.25" "15.375" "20.5" "25.625" "30.75"

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com