[R] Plotting (vector,matrix)

Raymond Wan rwan at kuicr.kyoto-u.ac.jp
Sun Sep 16 06:04:42 CEST 2007


Hi all,

I'm still a bit new to R and I'm trying to figure something out.  I have 
the solution but the solution is what a C programmer would do :-) and I 
was wondering if someone could tell me the R way of doing it...

What I have is a vector x of length |x|.  And a matrix y of size m rows 
and n columns where n = |x|.  I want to plot m * n points on a graph so 
that the x-coordinates are from x and the y coordinates are from y.

I can plot x with the mean of each column of y as:  plot (x,mean(y)).  
But, to do what I want,  I had to write this pair of for loops:

for (i in 1:dim(y)[1]) {
  for (j in 1:length(x)) {
    points(x[j],y[i,j])
  }
}

to add the points.  I think there must be a better way using some 
variant of apply or matlines.  Even after reading the docs, I'm still 
scratching my head a bit...  Or maybe I copy x out so that it is of size 
m by n?!?  Any thoughts on what an R user would do here?  Thanks!

Ray



More information about the R-help mailing list