stat_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, ...)
loess
. For datasets with 1000 or more
observations defaults to gam, see gam
for more details.y ~ x
, y ~ poly(x, 2)
, y ~ log(x)
FALSE
(the default), removes
missing values with a warning. If TRUE
silently
removes missing values.aes
or aes_string
. Only
needs to be set at the layer level if you are overriding
the plot defaults.a data.frame with additional columns ypredicted value yminlower pointwise confidence interval around the mean ymaxupper pointwise confidence interval around the mean sestandard error
Aids the eye in seeing patterns in the presence of overplotting.
Calculation is performed by the (currently undocumented)
predictdf
generic function and its methods. For
most methods the confidence bounds are computed using the
predict
method - the exceptions are
loess
which uses a t-based approximation, and for
glm
where the normal confidence interval is
constructed on the link scale, and then back-transformed
to the response scale.
stat_smooth
understands the following aesthetics (required aesthetics are in bold):
x
y
geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method. geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method. geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method. geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method. geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.# 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)geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method. geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.# The colour of the line can be controlled with the colour aesthetic c + stat_smooth(fill="blue", colour="darkblue", size=2)geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method. geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method. geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.# Smoothers for subsets c <- ggplot(mtcars, aes(y=wt, x=mpg)) + facet_grid(. ~ cyl) c + stat_smooth(method=lm) + 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()geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.# Example with logistic regression data("kyphosis", package="rpart") qplot(Age, Kyphosis, data=kyphosis)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))