[R] Sos! "Lines" doesn't add plots to an existing plot...
(Ted Harding)
Ted.Harding at nessie.mcc.ac.uk
Tue Jan 31 08:54:06 CET 2006
On 31-Jan-06 Michael wrote:
> I am lost:
>
>> plot(testError, col="red")
>> lines(testVar, col="black")
>
> Only one plot (the red one) appear on the Window, the black line did
> not
> appear...what's wrong?
>
> Thanks a lot!
Check the ranges of values of your two variables (in both x and y
directions in general, though x is probably not relevant to your
case since your implicit x-values are the indices of the y-values).
The first "plot" determines the ranges of values which will be
plotted, according to the range of values in the variables in
that plot, and these are held fixed when "lines" is called.
If, therefore, the y-values of "testVar" lie beyond the range
in the frame drawn by "plot", nothing will be plotted inside
that frame.
There are several possible ways of looking at this issue, but
they all come down finally to setting ylim=c(yMin,yMax)
and/or xlim=c(xMin,xMax) as arguments to the first "plot".
They vary in how you arrive at values for xMin, xMax, yMin, yMax.
For example, suppose you have daily temperatures for (in England)
the month of January (days 1-31 of the year) and July (182-212):
xJan<-(1:31)
yJan<-c( 2, 3, 6, 7, 5, 8,10,11, 8, 4,
-1,-2, 0, 2, 6, 8, 9, 8, 6, 9,
12,11, 9, 8, 5, 6, 8,10,11,10, 9)
xJul<-(182:212)
yJul<-c(17,20,21,20,19,22,25,26,28,27,
25,24,22,25,26,23,19,20,19,22,
20,23,25,24,21,19,22,25,28,26,27)
Now:
plot(yJan,type="l",col="blue")
lines(yJul,col="red")
exactly as you found: no red line! The plotted y-range is (-2,12),
and the yJul values lie outside this.
Next, note that the max value of yJul is 28, so:
plot(yJan,type="l",col="blue",ylim=c(-2,28))
lines(yJul,col="red")
so now you have the Jan and Jul temperatures for days 1:31 of
the month in each case.
But now:
plot(xJan,yJan,type="l",col="blue",ylim=c(-2,28))
lines(xJul,yJul,col="red")
and again the July plot is off the graph, because the x limits
were still set by the January x-values, and the July x-values
lie beyond this range. So, finally, noting that the x-values
range from 1 to 212 overall:
plot(xJan,yJan,type="l",col="blue",xlim=c(1,212),ylim=c(-2,28))
lines(xJul,yJul,col="red")
and now you have everything.
Hoping this helps,
Ted.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 31-Jan-06 Time: 07:54:01
------------------------------ XFMail ------------------------------
More information about the R-help
mailing list