Skip to content

Instantly share code, notes, and snippets.

@whitead
Last active December 26, 2015 20:39
Show Gist options
  • Select an option

  • Save whitead/7209844 to your computer and use it in GitHub Desktop.

Select an option

Save whitead/7209844 to your computer and use it in GitHub Desktop.
Moving average with ghosted lines
plot_ma <- function(d, start_lwd=0.9, start_gray=0.5, ...) {
if(!is.data.frame(d))
d <- as.data.frame(d)
iter <- as.integer(floor(log(nrow(d), base=10)))
dlwd <- (1.5 - start_lwd) / iter
dgray <- (0.9 - start_gray) / iter
pargs <- substitute(...)
if(ncol(d) >= 2)
plot(d[,1],d[,2], type="l", lwd=start_lwd, col=gray(start_gray), pargs )
else
plot(d[,1], type="l", lwd=start_lwd, col=gray(start_gray), pargs )
for(i in 1:iter) {
ma <- function(x,n=10**(i)){filter(x,rep(1/n,n), sides=2)}
if(ncol(d) >= 2)
lines(d[,1], ma(d[,2]), col=gray(-i *dgray + start_gray), lwd=(start_lwd + i * dlwd))
else
lines(seq(1,nrow(d)), ma(d[,1]), col=gray(-i *dgray + start_gray), lwd=(start_lwd + i * dlwd))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment