I would like to add a 5year moving average to my plot (dummy data and code below). I have multiple plots in one window and would like to have a separate moving average for each factor (stuff1, stuff2 and stuff3 in code below).
I would very much appreciate any help you could give me. The original data points need to stay in the plot with the moving averages overlaid.
#### Question: how to add moving average (over 5 years) overlaid on the data points.
mydata<- data.frame(
Year = factor(rep(c("1980","1981", "1982", "1983", "1984", "1985", "1986", "1987", "1988", "1989",
"1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999",
"2000", "2001", "2002", "2003",
"2004", "2005", "2006", "2007", "2008", "2009"), each = 1)), #better way to do this? i am terrible at basic R
Type = factor(rep(c("stuff1", "stuff2", "stuff3"), each = 10*3)),
Value = rnorm(90, mean = rep(c(1:90), each = 1),
sd = rep(c(6, 7, 3), each = 10)))
Value=c(1:90))
library(ggplot2)
p <- ggplot(mydata, aes(x=Year, y=Value))+
geom_point(mapping=aes(shape=Type), size=2)+
theme_bw()+
geom_line(mapping=aes(linetype=Type))+
ylab(expression(paste("mean ",mu, "g",C~L^{-1}, day^{-1})))+
theme(legend.key = element_blank())
p
Anna Zakrisson Braeunlich
PhD student
Department of Ecology, Environment and Plant Sciences
Stockholm University
Svante Arrheniusv. 21A
SE-106 91 Stockholm
Sweden/Sverige
Lives in Berlin.
For paper mail:
Katzbachstr. 21
D-10965, Berlin