[R] Multi-line plots - max Y?

Mark Knecht markknecht at gmail.com
Fri Jul 3 22:13:04 CEST 2009


Hi all,
   OK, I'm struggling a bit with getting good basic plots so I put
together this little example. It works - sort of - but it seems a bit
hokey to me. Can someone suggest how a pro would do it?

   The goal is to take data from 4 columns of a data.frame (data read
in by read.csv) and generate a plot with the borders and text in black
and three lines in different colors. The code below generates that but
loses data because the second and third lines have values greater than
the first which set the Y axis max. I can use something like max(X$d)
to get the max size required but I don't see how to set the Y
dimension it in the first plot command. How do I do that? I looked in
?par but didn't spot it.

   Last, how would I add a legend to explain the 3 lines? I haven't
studied it yet but maybe someone can point me at the right stuff.

   So, how could this little example be made to have all the data on
the plot and how could I code it better? I don't think those
'as.vector' coercions are really the right way.

Thanks,
Mark


X<- data.frame(a=1:10, b=cumsum(X$a), c=cumsum(X$b), d=cumsum(X$c))
X

plot(X$b ~ X$a, type="l", col="blue")
lines(as.vector(X$a),as.vector(X$c), col="green")
lines(as.vector(X$a),as.vector(X$d), col="red")




More information about the R-help mailing list