[R] data.frame, ggplot vs matplot

baptiste Auguié ba208 at exeter.ac.uk
Tue Oct 30 21:22:05 CET 2007


Hello,


I've been trying to use ggplot, but I cannot seem to get my data in  
the correct form. I usually have a vector x, and a matrix y and I  
would usually use matplot(x,y) to plot the columns of y against x.

Now, if i understand correctly, I should cast x and y into a  
data.frame and do something like ggplot(x,y,data=data). I think I'm  
missing something here: do I really need to reshape y into a single  
vector, replicate x accordingly, and put both vectors in a data frame?

I've tried it, but the reshape function is still very obscure to me.  
Any help appreciated!

Small example:

x<-seq(from=1,by=1,length=10)
y<-sin(x)%*%t(x)
columns<-c(1:10)

matplot(x,y)

legend("topleft", "(x,y)",  columns)

# the version below is not working

require(ggplot2)

data.frame(cbind(x,y))->dataS
wide <- reshape(dataS, direction="wide",timevar=c(1:length(columns)))
dataL<-reshape(wide,direction="long") # this clearly doesn't work as  
I would have thought
qplot(x, y, data = dataL)

Best regards,

baptiste



More information about the R-help mailing list