[R] Problem adding lines to a plot, when y is defined

Enrico Schumann es at enricoschumann.net
Mon Sep 9 22:00:58 CEST 2013


On Mon, 09 Sep 2013, "Cech, Christian" <Christian.Cech at fh-vie.ac.at> writes:

> 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.

Hard to say without a reproducible example, but try 

  lines(as.Date(var[, 1], origin="1899-12-30"),  var[, 5])

instead.

>
> I do very much appreciate your help!
> Kind regards,
> Christian Cech
>

[...]


-- 
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net



More information about the R-help mailing list