On Sun, Aug 15, 2010 at 2:04 PM, James Toll <ja...@jtoll.com> wrote:
> On Aug 15, 2010, at 10:40 AM, Gabor Grothendieck wrote:
>
>> On Sun, 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")
>>>
>>> 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.
>>>
>>
>> Try this for several examples:
>>
>> library(zoo)
>> example(xblocks)
>
> Thank you for the suggestion.  I couldn't get it to work with my time series, 
> but I did try some of the examples from ?xblocks which is pretty interesting, 
> and I'll probably end up using that elsewhere.
>
> Trying to apply xblocks to my time series, I get the error:
>
>> xblocks(xtsPR < 0.05, col = rgb[1])
> Error in rle(y) : 'x' must be an atomic vector

xblocks is part of zoo, not xts.  Try this:

z <- as.zoo(xtsPR)[, 1]
plot(z)
xblocks(z > 0.05, col = "red")



You have passed a logical vector to xblocks whereas you should have
passed a time series.  See help(xblocks)


>
> Even if I could get xblocks to work, I don't think it would do exactly what 
> I'm looking for.  I get the feeling that I need to find a way to get either 
> rect or polygon to work with my time series.  The question is how to define 
> the coordinates of a rect or polygon in terms of time along the x-axis?
>
> Thanks
>
> James
>
>

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

Reply via email to