[R] multiple plot overlay - dataframe

Henrik Bengtsson hb at maths.lth.se
Wed Feb 26 08:05:03 CET 2003

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

More information about the R-help mailing list