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
baptiste Augui?-2 wrote:> > 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) > >dataL = melt(dataS,id="x") qplot(x,value,data=dataL) qplot(x,value,data=dataL,colour=variable) cheers Ben Bolker -- View this message in context: http://www.nabble.com/data.frame%2C-ggplot-vs-matplot-tf4721042.html#a13498585 Sent from the R help mailing list archive at Nabble.com.