Working With Span In Ggplot2 / Geom_smooth
I am using ggplot2 to create a plot with several data sets. For not all datasets have the same amount of datapoints (or have breaks), I would like to adjust the span. But I am not
Solution 1:
Please make a short reproductible example.
The default stat for this geom is stat_smooth
After reading the stat_smooth help (?stat_smooth), the function use statistical methods from lm, glmor loess functions from the stats base package. There is alos a a reference to the mgcv package for the gam method. So, the span argument of stat_smooth use these methods to control the degree of smoothing.
But the easy way to verify that is to use the loessfunction of the stats package and to compare with your results obtained with stat_smooth.
With this this example the results semm to be the same:
loess:
period <- 120
x <- 1:120
y <- sin(2*pi*x/period) + runif(length(x),-1,1)
plot(x,y, main="Sine Curve + 'Uniform' Noise")
y.loess <- loess(y ~ x, span=0.75, data.frame(x=x, y=y))
y.predict <- predict(y.loess, data.frame(x=x))
lines(x,y.predict)
geom_smooth:
xy <- cbind(x,y)
gp <- ggplot(as.data.frame(xy), aes(x=x,y=y)) + geom_point()
gp + geom_smooth(aes(y=y,x=x), data=as.data.frame(xy), method = "loess", span = 0.75)
Post a Comment for "Working With Span In Ggplot2 / Geom_smooth"