[R] error with persp()- increasing 'x' and 'y' values expected

1,574 views
Skip to first unread message

adick

unread,
Feb 20, 2012, 2:15:40 PM2/20/12
to r-h...@r-project.org
I want to use persp to graph my data and it keeps giving me the error
increasing 'x' and 'y' values expected, even though my data is in increasing
order with respect to x and y.

Here is the code I'm currently using:
bob= scan ("SBA3dataTaxonB.txt",what="char")
labels = bob[1:3]
bob=bob[-c(1,2,3)]
bob=as.numeric(bob)
bob=array(bob,dim=c(3, length(bob)/3))
bob=t(bob)
data.frame(bob) -> bob
bob = bob[order(bob[,1], bob[,3]),]
persp(bob[,1], bob[,3], bob[,2], log="bob[,1]")

The first few lines of the txt file SBA3dataTaxonB.txt look like this:
variable TaxonB central
10 19 0.002
10 25 0.002
10 26 0.002
1 17 0.002
1 23 0.002
1 16 0.002
0.1 48 0.002
0.1 47 0.002
0.1 57 0.002
10 15 0.004
10 22 0.004
10 22 0.004
1 21 0.004
1 22 0.004
1 27 0.004
0.1 73 0.004
0.1 62 0.004

Can anyone help me figure out why I am getting this error? And also a
possible solution would be greatly appreciated.
-Thanks, Amanda

--
View this message in context: http://r.789695.n4.nabble.com/error-with-persp-increasing-x-and-y-values-expected-tp4404834p4404834.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-h...@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

David Winsemius

unread,
Feb 20, 2012, 3:22:35 PM2/20/12
to adick, r-h...@r-project.org

On Feb 20, 2012, at 2:15 PM, adick wrote:

> I want to use persp to graph my data and it keeps giving me the error
> increasing 'x' and 'y' values expected, even though my data is in
> increasing
> order with respect to x and y.


Do you have missing entries? (Running your data fragment through your
code produces the same error.) You may want to use rep with each and
times arguments to construct a regular grid and then population the
missing entries in matrix with NA's.

>
> Here is the code I'm currently using:
> bob= scan ("SBA3dataTaxonB.txt",what="char")
> labels = bob[1:3]
> bob=bob[-c(1,2,3)]
> bob=as.numeric(bob)
> bob=array(bob,dim=c(3, length(bob)/3))
> bob=t(bob)
> data.frame(bob) -> bob
> bob = bob[order(bob[,1], bob[,3]),]
> persp(bob[,1], bob[,3], bob[,2], log="bob[,1]")

Any way .... The z argument to `persp` is supposed to be a matrix of
dimension == length(x)*length(y):

?matrix

David Winsemius, MD
West Hartford, CT

adick

unread,
Feb 20, 2012, 5:49:04 PM2/20/12
to r-h...@r-project.org

On Feb 20, 2012, at 2:15 PM, adick wrote:

Do you have missing entries? (Running your data fragment through your
code produces the same error.) You may want to use rep with each and
times arguments to construct a regular grid and then population the
missing entries in matrix with NA's.

Yes, there are missing entries. I don't know what rep and times arguments
are or how to do anything you just said, because I'm knew to R.

I did however figure out how to take the average of my three replicates and
try to plot that, which returns the same error. Here is the updated code:

bob= scan ("SBA3dataTaxonB.txt",what="char")
labels = bob[1:3]
bob=bob[-c(1,2,3)]
bob=as.numeric(bob)
bob=array(bob,dim=c(3, length(bob)/3))
bob=t(bob)
data.frame(bob) -> bob

vary=0
varx= 0
varz=0

for (r in 1:(length(bob[,1])/3)) {
vary[r] <- mean(bob[(1+((r-1)*3)):(3+((r-1)*3)),2])
varx[r] <- bob[(1+((r-1)*3)),1]
varz[r] <- bob[(1+((r-1)*3)),3]
}
rob <- data.frame(varx=varx,varz=varz,vary=vary)

rob = rob[order(rob[,1], rob[,2]),]
persp(rob[,1], rob[,2], rob[,3], log="rob[,1]")

--
View this message in context: http://r.789695.n4.nabble.com/error-with-persp-increasing-x-and-y-values-expected-tp4404834p4405380.html

David Winsemius

unread,
Feb 20, 2012, 11:08:33 PM2/20/12
to adick, r-h...@r-project.org

On Feb 20, 2012, at 5:49 PM, adick wrote:

>
> On Feb 20, 2012, at 2:15 PM, adick wrote:
>
> Do you have missing entries? (Running your data fragment through your
> code produces the same error.) You may want to use rep with each and
> times arguments to construct a regular grid and then population the
> missing entries in matrix with NA's.
>
>
>
> Yes, there are missing entries. I don't know what rep and times
> arguments
> are or how to do anything you just said, because I'm knew to R.

?rep

> rep(1:3, times=3)
[1] 1 2 3 1 2 3 1 2 3
> rep(1:3, each=3)
[1] 1 1 1 2 2 2 3 3 3

You can also use gl

?gl

--
David.

David Winsemius, MD
West Hartford, CT

______________________________________________

David L Carlson

unread,
Feb 21, 2012, 12:07:47 PM2/21/12
to David Winsemius, adick, r-h...@r-project.org
You do not have your data organized in a way that persp() can use it. Here
is a simple example. The function will not work with x, y, z vectors. X and
Y are vectors in order with no repeated values, Z is a matrix containing one
value for every combination of x and y:

> x <- 1:10
> y <- 1:15
> z <- outer(x, y, "*")
> persp(x, y, z)

You must define a grid and specify a single value at each point on that
grid.

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352

Reply all
Reply to author
Forward
0 new messages