I hope you could help me with this. I have a dataframe with 5 columns, the first column determining the X values, and the rest four determining four separate Y vectors. How can I plot them on the same graph, overlaying each other? Thanks. Elena Zheleva
Here's one idea: df <- ... # Create an empty plot big enough to fit all data points xlim <- range(df[,1], na.rm=TRUE) ylim <- range(df[,-1], na.rm=TRUE) plot(NA, xlim=xlim, ylim=ylim, xlab="x", ylab="y") # For each Y column, plot the data against the X column for (k in 2:ncol(df)) points(df[,1], df[,k], col=k-1) # Works with lines() too! and here is another one that assumes a scatter plot: xs <- rep(df[,1], times=ncol(df)-1) ys <- as.vector(as.matrix(df[,-1])) col <- rep(1:5, each=nrow(df)) plot(xs,ys, col=col, xlab="x", ylab="y") Hope that helps Henrik Bengtsson> -----Original Message----- > From: r-help-admin at stat.math.ethz.ch > [mailto:r-help-admin at stat.math.ethz.ch] On Behalf Of Hi from Zynnel > Sent: den 26 februari 2003 17:41 > To: r-help at stat.math.ethz.ch > Subject: [R] multiple plot overlay - dataframe > > > I hope you could help me with this. I have a dataframe > with 5 columns, the first column determining the X > values, and the rest four determining four separate Y > vectors. How can I plot them on the same graph, > overlaying each other? > Thanks. > > Elena Zheleva > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > http://www.stat.math.ethz.ch/mailman/listinfo/> r-help > >
Hi On 25 Feb 2003 at 22:41, Hi from Zynnel wrote:> I hope you could help me with this. I have a dataframe > with 5 columns, the first column determining the X > values, and the rest four determining four separate Y > vectors. How can I plot them on the same graph, > overlaying each other?It depends little bit on range of your Y values. If it is similar magnitude, then simple matplot(your.df[,1],your.df[,3:6],type="l") see ?matplot will do it. But if the range of your Y values is big you have to do some "flattening" or to use subsequent plot commands with par(new=T). See ?plot and ?par> Thanks. > > Elena Zheleva > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > http://www.stat.math.ethz.ch/mailman/listinfo/r-helpCheers.Petr Pikal petr.pikal at precheza.cz p.pik at volny.cz
>I hope you could help me with this. I have a dataframe >with 5 columns, the first column determining the X >values, and the rest four determining four separate Y >vectors. How can I plot them on the same graph, >overlaying each other?Use matplot() to plot several plots in the same window. help(matplot) for more info. Regards Jose Lozano