0

I have created a plot using ggplot and plotly. I have two variables in my dataset and I would like to add a 12 month moving average of one of the variables (headline inflation in this case). I believe that geom_ma is not supported in plotly so I'm looking for a solution that would work with plotly

p1 <- ggplot(inflation, aes(Date)) +
  geom_line(aes(y=`Core Inflation`,col = '#75002B'), size = 1.5) +
  geom_line(aes(y=`Headline Inflation`,col = '#EFBE93'), size = 1.5) +
  geom_ma(aes(y = `Headline Inflation`, col = '#BD215B'), ma_fun = SMA,n=12, size=1.5) +
  theme_bw() +
  labs(y='Annual Inflation Rate (%)',color = "") +
  scale_color_manual(values = c("#75002B",'#BD215B', "#EFBE93"),
                     labels = c('Core','MA','Headline'))

p1 %>% ggplotly() 

EDIT - I have changed my dataset from long to wide format and ran each geom_line separately as I was struggling to run the geom_ma on only the headline inflation variable when my data was in long format.

structure(list(Date = structure(c(16983, 17014, 17045, 17075, 
17106, 17136, 17167, 17198, 17226, 17257, 17287, 17318, 17348, 
17379, 17410, 17440, 17471, 17501, 17532, 17563, 17591, 17622, 
17652, 17683, 17713, 17744, 17775, 17805, 17836, 17866, 17897, 
17928, 17956, 17987, 18017, 18048, 18078, 18109, 18140, 18170, 
18201, 18231, 18262, 18293, 18322, 18353, 18383, 18414, 18444
), class = "Date"), `Headline Inflation` = c(6.99, 6.83, 6.9, 
7.26, 7.34, 7.3, 8.2, 7.75, 7.05, 6.68, 6.28, 6.08, 5.45, 5.37, 
5.57, 5.16, 5.18, 5.17, 3.56, 3.52, 3.51, 3.58, 3.84, 3.96, 4.46, 
4.41, 4.79, 5.12, 5.56, 5.15, 4.66, 4.42, 4.5, 4.53, 4.08, 3.94, 
3.64, 3.71, 2.93, 3.02, 2.46, 2.59, 2.05, 2.45, 2.35, 1.64, 2.06, 
2.15, 2.09), `Core Inflation` = c(6.24, 6.13, 6.36, 6.77, 6.75, 
6.83, 7.84, 7.65, 7.13, 6.96, 6.61, 6.63, 6.34, 6.38, 6.2, 5.83, 
5.81, 5.51, 3.55, 3.55, 3.49, 3.48, 3.58, 3.41, 3.65, 3.38, 3.95, 
4.1, 4.42, 4.42, 4.25, 4.21, 4.27, 4.39, 3.89, 3.85, 3.48, 3.7, 
2.77, 3.18, 2.84, 2.85, 1.63, 1.74, 1.67, 1.22, 2.43, 2.77, 2.86
)), row.names = c(NA, -49L), class = c("tbl_df", "tbl", "data.frame"
))

EDIT - I've managed to make the plot in ggplot2 however when I run it in ggplotly I get an error which says that geom_ma is not supported in plotly

Warning message: In geom2trace.default(dots[[1L]][[1L]], dots[[2L]][[1L]], dots[[3L]][[1L]]) :   geom_GeomMA() has yet to be implemented in plotly.   If you'd like to see this geom implemented,   Please open an issue with your example code at   https://github.com/ropensci/plotly/issues
sa90210
  • 525
  • 2
  • 12
  • Were you able to get the geom_ma() to work in general without plotly first? – Daniel_j_iii Aug 16 '20 at 02:13
  • @DanielJachetta managed to get ```geom_ma``` to work in ```ggplot2``` but when I try to put in - ```ggplotly``` it gives the following error ```Warning message: In geom2trace.default(dots[[1L]][[1L]], dots[[2L]][[1L]], dots[[3L]][[1L]]) : geom_GeomMA() has yet to be implemented in plotly. If you'd like to see this geom implemented, Please open an issue with your example code at https://github.com/ropensci/plotly/issues``` – sa90210 Aug 16 '20 at 03:18
  • Can you update your post with the `geom_ma` code that works without `plotly` ? – Ronak Shah Aug 16 '20 at 04:41
  • @RonakShah I have updated it just now – sa90210 Aug 16 '20 at 05:27

1 Answers1

3

Since, geom_ma is not implemented in plotly yet. You could calculate moving average by using already existing functions. I am using zoo::rollapplyr with function as mean.

library(tidyverse)
p1 <- inflation %>%
    mutate(moving_avg = zoo::rollapplyr(`Headline Inflation`, 2, 
                        mean, partial = TRUE)) %>%
    pivot_longer(cols = -Date) %>%
    ggplot() +  aes(Date, y = value, color = name) +
    geom_line(size = 1.5) + 
    theme_bw() +
    labs(y='Annual Inflation Rate (%)',color = "") +
    scale_color_manual(values = c("#75002B",'#BD215B', "#EFBE93"),
                       labels = c('Core','MA','Headline'))
p1

enter image description here

Note that there is also zoo::rollmeanr which takes mean by default but it lacks partial = TRUE argument so it returns first 12 values as NA.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Thanks @Ronak Shah! Sorry one more question, is there no way I could change the ```MA``` line to a dashed one with my data in long format? – sa90210 Aug 16 '20 at 11:31
  • 1
    @sa90210 you can use `linetype = name` in `aes` and then add `scale_linetype_manual`. Something like `scale_linetype_manual(values=c("solid", "solid", "dashed"))` – Ronak Shah Aug 16 '20 at 12:54