[R] Plotting Two Lines

Jim Lemon jim at bitwrit.com.au
Sat Jan 24 10:53:20 CET 2009


Jim Lemon wrote:
> stats787 wrote:
>> Hi,
>>
>> I am trying to plot the following data such that both variable y and 
>> z vs x.
>> (ie two lines on a single plot). As the x variable is not numeric, 
>> how do I
>> go about it? Appreciate if any expert could help. I know I use plot() 
>> follow by lines() to add another line to the plot. But
>> my problem is i was unable to plot y vs x in the first place.
>>   
> Hi stats787,
> I think this is a replay of a previous problem where the sender 
> expected the values on the abcissa to be sorted before plotting. You 
> have the additional camoflage of a factor as that set of values. One 
> minor problem is that the separator is missing between elements 1 and 
> 2 in the eighth line. After fixing that, try this (assuming your data 
> frame is named "stats787":
>
> plot(as.numeric(stats787$x)[order(stats787$x)],stats787$y,type="l",col="red")                    
>
> lines(as.numeric(stats787$x)[order(stats787$x)],stats787$z,col="green")
>
Oops, that should be:


plot(as.numeric(stats787$x)[order(stats787$x)],stats787$y[order(stats787$x)],
 type="l",col="red")                   
lines(as.numeric(stats787$x)[order(stats787$x)],stats787$z[order(stats787$x)],
 col="green")


Jim




More information about the R-help mailing list