8

I want to plot two graphs on one plot. I read this post, but function lines does not work, nothing happens. I don't know what can be the problem. Any ideas?

Edit. reproducible example:

> tr_error
[1] 0.2314984 0.2314990 0.2314981 0.2314955 0.2314955 0.2314943 0.2314912
[8] 0.2314924
> tst_error
[1] 0.001461264 0.001461767 0.001461001 0.001459936 0.001459626 0.001458594
[7] 0.001457719 0.001458288
> plot(tst_error, type='l')
> lines(tr_error, type='l', col='red')

maybe there is second plot but it is higher?

Community
  • 1
  • 1
ashim
  • 24,380
  • 29
  • 72
  • 96
  • A Reproducible example please i.e. what you have tried, and what doesn't work. I'll remove the -1 when this is added – mnel May 29 '12 at 05:37

1 Answers1

15

It "doesn't work" because the y-limits do not include the range of the second vector.

 plot(tst_error, type='l', ylim=range( c(tst_error, tr_error) ) )
 lines(tr_error, type='l', col='red')

It's not going to be a particularly interesting plot since the scale of the two vectors are so different. The red line's going to look like a completely flat line.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • 1
    Thanks! I had a similar problem and that solved it. One little thing, the plot function is missing a closing ). If you try to run the code as is, R will throw an error since plot is trying to take on arguments it has no idea what to do with. – FloppyDisk Aug 28 '12 at 14:25