title: plot stockprice with exponential moving average in r
body: <p>I have the following df + code example: (my df is from 01/2016 - 04/2020)</p>
  
  <pre><code>         Date A01_Price A02_Price A03_Price A04_Price A05_Price A06_Price A07_Price A08_Price A09_Price A10_Price A11_Price A12_Price A13_Price A14_Price A15_Price
  1  2016-01-04   49.5010   21.6400   90.0100    93.676   81.6110   27.6450   28.4600   44.1930   33.0140  216.5460    36.201   41.7360    25.495    16.200    69.197
  2  2016-01-05   49.7855   21.9870   88.5695    92.329   82.4590   28.2235   28.2790   44.8180   34.0180  218.5370    35.929   40.8520    25.431    16.157    69.828
  3  2016-01-06   49.0595   21.5060   87.4735    88.601   81.4320   28.1725   27.4720   43.5065   36.2670  207.5960    35.256   39.9220    25.076    16.061    66.947
  4  2016-01-07   47.7785   20.8415   82.8735    83.725   78.9340   26.7820   26.3485   39.0500   34.3570  203.4775    33.967   38.2115    24.062    15.738    64.135
  5  2016-01-08   47.7435   20.2600   82.9275    82.609   79.0000   26.4495   26.1980   41.0055   33.1400  199.9250    33.361   37.3630    23.993    15.641    63.434
  6  2016-01-09   47.8160   20.3800   83.0530    83.503   79.3925   26.4900   26.2460   41.0680   33.1910  200.9835    33.447   37.4530    24.110    15.734    63.510
  7  2016-01-10   47.7770   20.4750   82.9860    83.325   79.6450   26.4680   26.2200   41.0340   33.1640  201.3680    33.401   37.4050    23.998    15.713    63.469
  8  2016-01-11   48.8095   20.8440   83.0320    83.513   78.6720   26.1275   26.5150   40.3455   33.9770  202.6630    33.516   37.5030    23.947    15.753    65.583
  9  2016-01-12   48.9545   21.0340   83.7325    85.732   81.0900   27.0205   27.5920   41.3570   35.6450  205.9610    34.443   38.0980    24.004    15.939    66.032
  10 2016-01-13   48.0195   20.4640   82.6305    81.151   81.1780   27.1925   26.8050   39.2130   35.3825  197.9070    33.023   37.5945    23.423    15.737    64.682
  </code></pre>
  
  <p>I would like to have the long and short exponential moving average for each "Axy_Price" displayed in the plot for my function. Like in <a href="https://i.stack.imgur.com/fqwjY.png" rel="nofollow noreferrer">this</a> graphic. It would be great if I could somehow implement it in ggplot, because I want to show more variables in the plot. Anybody got an idea? I've tried this so far:</p>
  
  <pre><code>library(plyr)
  library(tidyverse)
  library(quantmod)
  library(TTR)
  library(zoo)
  library(PerformanceAnalytics)
  library(xts)
  
  #### EMA function ####
  EMAf &lt;- function (price,n){
    ema &lt;- c()
    ema[1:(n-1)] &lt;- NA
    ema[n]&lt;- mean(price[1:n])
    beta &lt;- 2/(n+1)
    for (i in (n+1):length(price)){
      ema[i]&lt;-beta * price[i] + 
        (1-beta) * ema[i-1]
    }
    ema &lt;- reclass(ema,price)
    return(ema)
  }
  #
  df_A01 &lt;- df %&gt;%
    dplyr::select(Date, A01_Price)
  #
  EMAf_A01 &lt;- ddply(df_A01, "A01", f)
  #
  library(ggplot2)
  ggplot(df_A01, aes(x=Year, y=Value)) +
    geom_line(mapping=aes(shape=Type), size=0.5) +
    theme_bw() +
    geom_line(data = madf, mapping=aes(x = Date, y = EMAf_A01, linetype=Type,
                                       color = A01), size = 1) +
    ylab(expression(paste("mean",mu, "g",C~L^{-1}, day^{-1}))) +
    theme(legend.key = element_blank())
  </code></pre>
  
  <p>I also tried it with ChartSeries but i get a zoo error:</p>
  
  <pre><code>#
  xts_stock01L &lt;- as.xts(df_A01[, c(2)], order.by=df_A01[[1]])
  #
  chartSeries(xts_stock01L,
              subset="2016-01::2020-04",
              theme=chartTheme("white"))
  addEMA(n=30,on=1,col = "blue")
  addEMA(n=200,on=1,col = "red")
  </code></pre>
  
  <p>Maybe I need to approach this whole thing differently</p>