[R] Multi-line plots - max Y?

Dylan Beaudette dylan.beaudette at gmail.com
Fri Jul 3 22:38:56 CEST 2009


Hi,

how about something like this:

a <- 1:10
b <- cumsum(a)
c <- cumsum(b)
d <- cumsum(c)

X<- data.frame(a,b,c,d)


plot(b ~ a, data=X, type="l", col="blue", ylim=c(0,max(X)))
lines(c ~ a, data=X, col="green")
lines(d ~ a, data=X, col="red")
legend('topleft', legend=c('a', 'b', 'c'), col=c('blue', 'green', 'red'), lty=1)



On Fri, Jul 3, 2009 at 1:13 PM, Mark Knecht<markknecht at gmail.com> wrote:
> 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")
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>




More information about the R-help mailing list