Skip to content

Instantly share code, notes, and snippets.

@juanchiem
Last active April 26, 2020 18:06
Show Gist options
  • Save juanchiem/fc7fd2b46e84b4087af5d1a25e6dce12 to your computer and use it in GitHub Desktop.
Save juanchiem/fc7fd2b46e84b4087af5d1a25e6dce12 to your computer and use it in GitHub Desktop.
library(tidyverse)
library(zoo)
df <- data.frame(
date = seq(as.Date("2002-01-01"), by="1 day", length.out=50) ,
var = 100 + c(0, cumsum(runif(49, -20, 20))))
df %>%
mutate(ma5=rollmean(var,5,align='right',fill=NA))%>%
mutate(sum5=rollsum(var,2,align='right',fill=NA))%>%
ggplot() +
geom_col(aes(x=date,y=sum5, group = 3)) +
geom_line(aes(x=date,y=var, group=1, colour = 'red')) +
# geom_point(aes(x=date,y=ma5, group = 2, colour = 'blue')) +
geom_line(aes(x=date,y=ma5, group = 2, colour = 'blue')) +
ylab('Values')+xlab('date')+
scale_x_date(date_breaks = "5 day", date_labels = "%d%b")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment