stat_smoothstat_smooth(mapping=NULL, data=NULL, geom="smooth", position="identity", method="auto", formula=y ~ x, se=TRUE, n=80, fullrange=FALSE, level=0.95, na.rm=FALSE, ...)
Add a smoother
Aids the eye in seeing patterns in the presence of overplotting.This page describes stat_smooth, see layer and qplot for how to create a complete plot from individual components.
What do you think of the documentation? Please let me know by filling out this short online survey.
The following aesthetics can be used with stat_smooth. Aesthetics are mapped to variables in the data with the aes function: stat_smooth(aes(x = var)). Note that you do not need quotes around the variable name.
Scales control how the variable is mapped to the aesthetic and are listed after each aesthetic.
| Aesthetic | Default | Related scales |
|---|---|---|
| x | required | continuous, date, datetime, discrete |
| y | required | continuous, date, datetime, discrete |
Layers are divided into groups by the group aesthetic. By default this is set to the interaction of all categorical variables present in the plot.
To use these variables in an aesthetic mapping, you need to surrond them with .., like aes(x = ..output..). This tells ggplot that the variable isn't the original dataset, but has been created by the statistic.
y, predicted valueymin, lower pointwise confidence interval around the meanymax, upper pointwise confidence interval around the meanse, standard errorParameters control the appearance of the stat. In addition to the parameters listed below (if any), any aesthetic can be used as a parameter, in which case it will override any aesthetic mapping.
method: smoothing method (function) to use, eg. lm, glm, gam, loess, rlmformula: formula to use in smoothing function, eg. y ~ x, y ~ poly(x, 2), y ~ log(x)se: display confidence interval around smooth? (true by default, see level to control)n: number of points to evaluate smoother atfullrange: should the fit span the full range of the plot, or just the dataxseq: exact points to evaluate smooth at, overrides nlevel: level of confidence interval to use (0.95 by default)na.rm: NULL...: other arguments are passed to smoothing functionThis function returns a layer object.
> c <- ggplot(mtcars, aes(qsec, wt)) > c + stat_smooth()> c + stat_smooth() + geom_point()
> > # Adjust parameters > c + stat_smooth(se = FALSE) + geom_point()
> > c + stat_smooth(span = 0.9) + geom_point()
> c + stat_smooth(method = "lm") + geom_point()
> > library(splines) > c + stat_smooth(method = "lm", formula = y ~ ns(x,3)) + + geom_point()
> c + stat_smooth(method = MASS::rlm, formula= y ~ ns(x,3)) + geom_point()
> > # The default confidence band uses a transparent colour. > # This currently only works on a limited number of graphics devices > # (including Quartz, PDF, and Cairo) so you may need to set the > # fill colour to a opaque colour, as shown below > c + stat_smooth(fill = "grey50", size = 2, alpha = 1)
> c + stat_smooth(fill = "blue", size = 2, alpha = 1)
> > # The colour of the line can be controlled with the colour aesthetic > c + stat_smooth(fill="blue", colour="darkblue", size=2)
> c + stat_smooth(fill="blue", colour="darkblue", size=2, alpha = 0.2)
> c + geom_point() + + stat_smooth(fill="blue", colour="darkblue", size=2, alpha = 0.2)
> > # Smoothers for subsets > c <- ggplot(mtcars, aes(y=wt, x=mpg)) + facet_grid(. ~ cyl) > c + stat_smooth(method=lm) + geom_point()
> c + stat_smooth(method=lm, fullrange=T) + geom_point()
> > # Geoms and stats are automatically split by aesthetics that are factors > c <- ggplot(mtcars, aes(y=wt, x=mpg, colour=factor(cyl))) > c + stat_smooth(method=lm) + geom_point()
> c + stat_smooth(method=lm, aes(fill = factor(cyl))) + geom_point()
> c + stat_smooth(method=lm, fullrange=TRUE, alpha = 0.1) + geom_point()
> > # Use qplot instead > qplot(qsec, wt, data=mtcars, geom=c("smooth", "point"))
> > # Example with logistic regression > data("kyphosis", package="rpart") > qplot(Age, Kyphosis, data=kyphosis)
> qplot(Age, data=kyphosis, facets = . ~ Kyphosis, binwidth = 10)
> qplot(Age, Kyphosis, data=kyphosis, position="jitter")
> qplot(Age, Kyphosis, data=kyphosis, position=position_jitter(y=5)) Error: unused argument(s) (y = 5) > > qplot(Age, as.numeric(Kyphosis) - 1, data = kyphosis) + + stat_smooth(method="glm", family="binomial")
> qplot(Age, as.numeric(Kyphosis) - 1, data=kyphosis) + + stat_smooth(method="glm", family="binomial", formula = y ~ ns(x, 2))
![]()
What do you think of the documentation? Please let me know by filling out this short online survey.