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

David Winsemius dwinsemius at comcast.net
Mon Sep 9 19:57:40 CEST 2013


On Sep 9, 2013, at 9:00 AM, Cech, Christian 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.

This puzzles me. At the moment yu are using postional matching and it is var[,3] that would be matched to the y argument.

You need to post output of str(var) so that we can see the class of var[,1].

> 
-- 

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list