Adding Dynamic Lines to a Potly Plot in R

Since Plotly made their interactive graphing platform available for R, I’ve been trying to incorporate it into more of my projects. Recently I was working on a stock graph that included multiple moving averages and thought it could be useful to use vertical lines to help define buy and sell points. While ggplot2 would allow me to pass a single vector of values into the geom_vline() argument, Plotly appears to require a separate list of arguments for each one (presumably because of the complete control they give you over the positioning of your shapes). Below is a snippect of the data I used to build the graph. Full data available here.

Date GE Adj Price 48.5 day EMA 13 day EMA 48.5d greater Lag Signal
1997-02-04 10.15 9.88 10.11 0 0
1997-02-05 10.06 9.89 10.10 0 0
1997-02-06 10.16 9.90 10.11 0 0
1997-02-07 10.26 9.91 10.13 0 0
1997-02-10 10.15 9.92 10.13 0 0
1997-02-11 10.22 9.93 10.15 0 0
1997-02-12 10.34 9.95 10.17 0 0
1997-02-13 10.48 9.97 10.22 0 0
1997-02-14 10.39 9.99 10.24 0 0
1997-02-18 10.50 10.01 10.28 0 0

Below is an example of arguments necessary to build a vertical line from the x axis to the adjusted price for GE on 2-11-1997.

Obviously drawing more than a couple lines in this manner would be very clumsy. My goal was to point out crossover points between the 2 EMAs, which could mean many vertical lines would need to be drawn. To make this task easier, I created a simple for loop to identify crossovers indicating a “buy” (in green) or a “sell” (in red).

First I filtered out all the days where the EMAs did not cross:

Then I created a list of lists, with each of the nested lists containing the arguments for one line. To make it easier for the end user to distinguish between “buy” and “sell” days, I used an ifelse statement to alter the line color for each iteration depending on the signal.

And voila!

Leave a Comment

Your email address will not be published. Required fields are marked *