On Sep 9, 2013, at 12:27 PM, Cech, Christian wrote:
>>> 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].
>>>
>>>
>
>> Dear David,
>>
>> thank you for the quick reply! Unfortunately I confused you because I mixed
>> up y-value and x-value. What I actually wanted to say is that the plot does
>> not work when x is specified (2nd example) while it does work if x is not
>> specified (1st example). Sorry for this!
>> y = var[, 3]
>> x = var[, 1]
>>
>> When I type str(var) i get the following output:
>> num [1:4747, 1:6] 33450 33451 33452 33455 33456 ...
>>
>> Is that the information you needed?
>
> Maybe. 'var' is a numeric matrix which may not be what you think it was if
> it started out life as having dates. (On this list it is requested that you
> post in context which means bottom posting in many cases. So I moved your
> reply.)
>
> When you apply the as.Date function with an origin, you will get an different
> number as the internal representation of the Date value than what you put in:
>
> as.numeric(as.Date(c(33450, 33451, 33452, 33455, 33456), origin="1899-01-01"))
> [1] 7518 7519 7520 7523 7524
> I suspect that you have a misregistration of the x values. Since it is a
> matrix we might have better view of it with:
>
> dput(head(var))
>
> I also supect that you will end up using the `axis` function to do your
> labeling.
>
> --
>
> David Winsemius
> Alameda, CA, USA
>
>> dput(head(var))
> structure(c(33450, 33451, 33452, 33455, 33456, 33457, -0.00299369609998333,
> -0.000882805884818571, -0.000570821462061429, 0.00735101809395667,
> -0.000574642535598095, -0.000464474459045714, -0.010510137214196,
> -0.0105173750100507, -0.0104987866171353, -0.0104808796029616,
> -0.0104561013745608, -0.0104562836793838, 0, 0, 0, 0, 0, 0,
> -0.0115901618181903,
> -0.0115901618181903, -0.0115901618181903, -0.0115901618181903,
> -0.0115901618181903, -0.0115901618181903, 0, 0, 0, 0, 0, 0), .Dim = c(6L,
> 6L), .Dimnames = list(NULL, c("col1", "col2", "col3", "col4",
> "col5", "col6")))
>
>
You need to get the x-axis values registered corectly for `lines` and `points`
calls:
plot(as.Date(var[, 1], origin="1899-12-30"),
var[, 3],
type="l",
ylim=c(-0.04, 0),
ylab='portfolio returns',
xlab='time')
dts <- as.numeric(as.Date(var[, 1], origin="1899-12-30"));lines(dts, var[, 5],
type="l", lty=3)
for (i in 1:nrow(var)) {
if(var[i, 4] | var[i,6])
points(dts[i], var[i, 2], pch=4)
--
David Winsemius
Alameda, CA, USA
______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.