Not sure if this is a programming question or not...
If I have the data below, which produces a \'spiky\' chart, and I\'d like to produce a slightly smoothed one using g
If you want to avoid losing too much information from the data, below could be a better approach, that works good for large datasets:
library(zoo)
library(reshape)
a$smooth<-rollmean(a$values,3,fill="extend") # 2nd parameter defines smoothness
ggplot(melt(a),aes(x=year,y=value,color=variable,group=variable))+geom_line()
Here is a better example:
a <- data.frame(year=1:10,values=sin(1:10)+runif(10))
a$smooth<-rollmean(a$values,3,fill="extend")
ggplot(melt(a,id.vars="year"),aes(x=year,y=value,color=variable,
group=variable))+geom_line(size=2)