[R] Problem adding lines to a plot, when y is defined
MacQueen, Don
macqueen1 at llnl.gov
Mon Sep 9 22:22:38 CEST 2013
In the second example, in the three functions that create the plot:
plot()
lines()
points()
all must have two arguments, the first of which is the x-values (dates),
and the second of which is the y-values associated with the dates.
For example, in your second version, where you have
points(i, var[i, 2], pch=4)
you want to replace i with the date associated with the i'th row in your
data.
You may also be slightly confused about which argument to plot() is the
"y-argument". In your second example, the y-argument is the second
argument, not the first.
Compare the results of these two commands:
plot( c(5,3,6) )
plot( c(2,9,10) , c(5,3,6) )
-Don
--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
On 9/9/13 9:00 AM, "Cech, Christian" <Christian.Cech at fh-vie.ac.at> wrote:
>Dear all,
>
>I want to create a line-plot with two lines and some additional
>scatter-plots.
>However, adding a line to the plot does not work when I specify the
>y-argument in the plot command, while it does work when y is not
>specified.
>
>I first send you an example that works:
>plot(var[, 3],
> type="l",
> ylim=c(-0.04, 0),
> ylab = 'portfolio returns',
> xlab = 'time')
>lines(var[, 5], type="l", lty=3)
>for (i in 1:nrow(var)) {
> if(var[i, 4] | var[i,6])
> points(i, var[i, 2], pch=4)
>}
>
>--> the result is displayed in attachment "Plot1.pdf"
>
>What does not work is the following code, where as the first argument of
>plot (ie the y-argument) is defined:
>plot(as.Date(var[, 1], origin="1899-12-30"),
> var[, 3],
> type="l",
> ylim=c(-0.04, 0),
> ylab='portfolio returns',
> xlab='time')
>lines(var[, 5], type="l", lty=3)
>for (i in 1:nrow(var)) {
> if(var[i, 4] | var[i,6])
> points(i, var[i, 2], pch=4)
>}
>
>--> the result is displayed in attachment "Plot2.pdf"
>
>The same problem appears if instead of
>
>as.Date(var[, 1], origin="1899-12-30")
>
>I use
>
>var[, 1]
>
>to define the y-argument.
>
>I do very much appreciate your help!
>Kind regards,
>Christian Cech
>
>________________________________
>
>
>Firmenwortlaut: Fachhochschule des bfi Wien Gesellschaft m.b.H
>Firmenbuchnummer: 148597 a
>Firmenbuchgericht: Handelsgericht Wien
>Firmensitz: Wohlmutstraße 22, 1020 Wien
>
>This message contains confidential information and is i...{{dropped:12}}
More information about the R-help
mailing list