teunbrand / ggh4x

ggplot extension: options for tailored facets, multiple colourscales and miscellaneous
https://teunbrand.github.io/ggh4x/
Other
541 stars 33 forks source link

Rolling statistics #10

Closed teunbrand closed 4 years ago

teunbrand commented 4 years ago

There should probably be a stat_roll() that can calculate rolling statistics given some kernel function. The kernel function should then accept a measure of centrality and a measure of spread. A simple moving average would then be a uniform kernel like dunif() but parameterised slightly the mean and distance to extremes.

Rough sketch for rolling Gaussian kernel here: https://github.com/teunbrand/tidytuesdayscripts/blob/452451997d819564dc201c7c7f93bbf806a2564c/scripts/2020_04_28_Broadway_Grosses.R#L39-L69

teunbrand commented 4 years ago

This now works

library(ggplot2)
library(ggh4x)

ggplot(mpg, aes(displ, hwy, colour = class)) +
  geom_point() +
  stat_rollingkernel(aes(alpha = after_stat(scaled)))

Created on 2020-06-02 by the reprex package (v0.3.0)