Skip to content

Instantly share code, notes, and snippets.

@abikoushi
Last active June 1, 2017 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abikoushi/c798780f7a6ecd24ee12a0fa1aebc698 to your computer and use it in GitHub Desktop.
Save abikoushi/c798780f7a6ecd24ee12a0fa1aebc698 to your computer and use it in GitHub Desktop.
StatMA <- ggproto("StatMA", Stat,
required_aes = c("x", "y"),
compute_group = function(data, scales,windowsize) {
grid <- data.frame(x = data$x)
grid$y <- stats::filter(data$y, rep(1,windowsize)) / windowsize
grid
}
)
stat_ma <- function(mapping = NULL, data = NULL, geom = "line",
position = "identity", na.rm = FALSE, show.legend = NA,
inherit.aes = TRUE, windowsize = 7, ...) {
layer(
stat = StatMA, data = data, mapping = mapping, geom = geom,
position = position, show.legend = show.legend, inherit.aes = inherit.aes,
params = list(windowsize = windowsize, na.rm = na.rm, ...)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment