Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Based on code in the <a href="https://r-forge.r-project.org/scm/viewvc.php/pkg/R/TA.R?view=markup&amp;root=quantmod" rel="nofollow noreferrer">TA.R</a> file of the <a href="http://cran.r-project.org/package=quantmod" rel="nofollow noreferrer">quantmod</a> package, here is code that uses <code>rle</code> to find the starts and ends of the rectangles.</p> <pre><code>runs &lt;- rle(as.logical(spy[, 1] &gt; spy[, 2])) l &lt;- list(start=cumsum(runs$length)[which(runs$values)] - runs$length[which(runs$values)] + 1, end=cumsum(runs$lengths)[which(runs$values)]) rect &lt;- data.frame(xmin=l$start, xmax=l$end, ymin=-Inf, ymax=Inf) </code></pre> <p>Combine that with some <code>ggplot2</code> code from the <a href="https://stackoverflow.com/a/4733446/967840">accepted answer</a> to the question you linked to:</p> <pre><code>ggplot(spy,aes(x=index(spy),y=spy$SPY.Adjusted))+geom_line()+geom_line(aes(x=index(spy),y=spy$sma))+geom_rect(data=rect, aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax), color="grey20", alpha=0.5, inherit.aes = FALSE) </code></pre> <p>And you get:</p> <p><img src="https://i.stack.imgur.com/cdRGP.png" alt="enter image description here"></p> <p>If you reverse the order of plotting and use <code>alpha=1</code> in <code>geom_rect</code> it may (or may not) look more like you desire:</p> <pre><code>ggplot(spy,aes(x=index(spy),y=spy$SPY.Adjusted))+geom_rect(data=rect, aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax), border=NA, color="grey20", alpha=1, inherit.aes = FALSE)+geom_line()+geom_line(aes(x=index(spy),y=spy$sma)) </code></pre> <p><img src="https://i.stack.imgur.com/1g7is.png" alt="enter image description here"></p> <hr> <p>Since you have an <code>xts</code> object. You may not even want to convert to a <code>data.frame</code>. Here is how you could plot it using the brand new <code>plot.xts</code> method in the <a href="https://r-forge.r-project.org/scm/viewvc.php/pkg/xtsExtra/?root=xts" rel="nofollow noreferrer">xtsExtra</a> package created by Michael Weylandt as part of a Google Summer of Code <a href="http://rwiki.sciviews.org/doku.php?id=developers:projects:gsoc2012:xts" rel="nofollow noreferrer">project.</a></p> <pre><code>spy &lt;- as.xts(spy) require(xtsExtra) plot(spy, screens=1, blocks=list(start.time=paste(index(spy)[l$start]), end.time=paste(index(spy)[l$end]), col='lightblue'), legend.loc='bottomright', auto.legend=TRUE) </code></pre> <p><img src="https://i.stack.imgur.com/GFY76.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload