
Fitted values, residuals and residual springs from a smoother
Source:R/layer_smooth_fit.R
smooth_fit.RdThese layers use the same fitting machinery as ggplot2::geom_smooth(),
but return the fitted values at the observed x values (or at xseq),
keeping the observed y as yend. This makes it easy to draw the residuals
from any smoothing method and formula as points, segments or springs.
Usage
layer_smooth_fit(
mapping = NULL,
data = NULL,
stat = StatSmoothFit,
geom = GeomPointAccent,
position = "identity",
...,
show.legend = NA,
inherit.aes = TRUE
)
stat_smooth_fit(..., geom = GeomPointAccent)
geom_smooth_fit(...)
geom_residuals(...)
geom_residual_springs(...)Arguments
- mapping, data, position, show.legend, inherit.aes
See
ggplot2::layer().- stat
The stat; defaults to
StatSmoothFit.- geom
The geom used to draw the result.
- ...
Other arguments passed to the layer, e.g.
method,formula,method.args,span,xseq, and fixed aesthetics.
Details
geom_smooth_fit()draws the fitted values as points.geom_residuals()draws the residuals as line segments.geom_residual_springs()draws the residuals as springs.stat_smooth_fit()andlayer_smooth_fit()are the general constructors.
A weight aesthetic is used as case weights in the fit (as in
ggplot2::geom_smooth()) and, in geom_residual_springs(), as the
stiffness of each spring (fewer, thicker coils; see geom_spring()), so
that a weighted least squares fit can be shown.
Examples
library(ggplot2)
p <- ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
geom_smooth(method = lm, formula = y ~ x, se = FALSE)
p + geom_residual_springs(method = lm, formula = y ~ x)
p + geom_residuals(method = lm, formula = y ~ x)
# by group
ggplot(mtcars, aes(wt, mpg, colour = factor(cyl))) +
geom_point() +
geom_smooth(method = lm, formula = y ~ x, se = FALSE) +
geom_residual_springs(method = lm, formula = y ~ x)