On Aug 15, 2010, at 12:21 PM, James Toll wrote:
Hi,
I am trying to add a rectangular colored background area to a plot
of a time series of relative price changes. I believe that what I'm
trying to do is very similar to the question and example given here:
http://www.mail-archive.com/r-h...@stat.math.ethz.ch/msg73948.html
http://www.mayin.org/ajayshah/KB/R/html/g5.html
My problem/difference is that my time series looks like so:
library(quantmod)
getSymbols("^GSPC", from="2008-01-01")
xtsPR <- diff(log(Ad(GSPC)))
head(xtsPR)
GSPC.Adjusted
2008-01-02 NA
2008-01-03 0.000000000
2008-01-04 -0.024857950
2008-01-07 0.003218041
2008-01-08 -0.018522677
2008-01-09 0.013532064
So my dates along the x-axis are in the format "2008-01-02" rather
than "2008", as in the example.
To keep things simple, I've simply been trying to plot the time
series using the plot command:
plot(xtsPR)
and then adding either a polygon, or preferably, a rect:
rect(2009-01-02, -0.10, 2009-12-31, 0.10, border = 0, col = "blue")
There would be a problem here:
a) In R: 2009-01-02 == 2006, since numbers separated by minus signs
would get parsed as, well, ... numbers.
b) Even if you had but quotes around 2009-01-02, you most probably
would not have gotten a proper response from rect, since it would not
have intuited that you were giving it a Date object. (The level of
abstraction of computers and interpreters is currently behind that of
the human brain.) It seems very doubtful that there is a rect.Date
function (i.e., a Date method for rect(), although I haven't checked)
c) Date objects are represented internally by the number of seconds
after a stating date, conventionally "1970-01-01". It is possible that:
rect(as.Date("2009-01-02"), -0.10, as.Date("2009-12-31"), 0.10,
border = 0, col = "blue")
.... might have worked as you expected but this was untested.
But nothing is added to the plot when I try to add the rectangle,
but I don't get an error message either.
My question is how do I specify my x coordinates to draw the polygon
or rect, given that my scale is of the form "2008-01-02" rather than
simply "2008"?
Additionally, do I have to use polygon, even if I'm trying to
drawing a rectangle, as was the case in the examples linked above?
I'd like to be able to use this with chartSeries, rather than plot,
but I thought it would be easiest to figure it out using plot
first. Clearly that hasn't helped me.
Grothendieck has given you an alternate solution, but even if it
"works" for you, you would be well advised to read up on Date and Date-
time objects ... they are different.
Thanks for any advice you can provide.
> ?Date
> ?DateTime
No documentation for 'DateTime' in specified packages and libraries:
you could try '??DateTime'
> ?DateTimeClasses # works
Read, and learn.
David Winsemius, MD
West Hartford, CT
______________________________________________
R-help@r-project.org 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.