#Prepare Data
n = 1000
x = seq(-pi,pi,length.out=n)
y = sin(x) + (runif(length(x))*0.1) #NOISY DATA
ys = smth(y,window = 0.1,method = "gaussian") #SMOOTHING
#If ggplot2 is installed, plot data using ggplot2
if(require(ggplot2)){
df = data.frame(X,y,ys)
ggplot(data=df) +
geom_path(aes(x,y),color="darkgray") +
geom_path(aes(x,ys),color="red",size=1) +
theme_bw() +
labs(title="Example Smoothing of Noisy Data")
#Else use standard graphics
}else{
plot(x,y,type="l",col="red")
lines(x,ys,col="black",lwd=3)
title("Example Smoothing of Noisy Data")
}
Run the code above in your browser using DataLab