[R] Moving Averages in ggplot2
Gabor Grothendieck
ggrothendieck at gmail.com
Thu Dec 10 20:59:24 CET 2009
You probably want to use a time series package for this. There are
plotting facilities specifically aimed at time series in zoo, xts,
quantmod, timeSeries and latticeExtra. We illustrate with zoo which
has classic graphics and lattice graphics methods:
devAskNewPage(TRUE)
library(zoo)
set.seed(123)
z <- zoo(rnorm(100), Sys.Date() - 100:0)
plot(cbind(z, rollmean(z, 10)), screen = 1, col = 1:2)
library(lattice)
xyplot(cbind(z, rollmean(z, 10)), screen = 1, col = 1:2)
If you want the raw and the smooth in different panels omit screen =
1. See ?plot.zoo, ?xyplot.zoo, ?rollmean and the three vignettes that
come with zoo.
On Thu, Dec 10, 2009 at 2:15 PM, fruminator <michael at frumin.net> wrote:
>
> Hello all,
>
> Have some time series data stored in a data.frame, and am plotting it with
> ggplot2 (which is totally awesome). I have explored the documentation and
> mailing list archives, and I can't see any way to plot a 'smoother' that is
> just the K-step moving average.
>
> For example, imagine I had a data.frame called 'sleep' with 'date' as the
> date (from as.Date()) and 'hours' as the # hours I slept that night, I would
> love to do something like:
>
> qplot(date, hours, data = sleep) + stat_smooth(method = 'movingaverage', k =
> 7);
>
> does such a thing exist? If not, I know the package is extensible, so any
> guidance as to how make it to do so would be very much appreciated.
>
> Thanks,
> Mike
> --
> View this message in context: http://n4.nabble.com/Moving-Averages-in-ggplot2-tp958867p958867.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list