stat_lm {ggformula} | R Documentation |
Linear Model Displays
Description
Adds linear model fits to plots. geom_lm()
and stat_lm()
are essentially
equivalent. Use geom_lm()
unless you want a non-standard geom.
Usage
stat_lm(
mapping = NULL,
data = NULL,
geom = "lm",
position = "identity",
interval = c("none", "prediction", "confidence"),
level = 0.95,
formula = y ~ x,
lm.args = list(),
backtrans = identity,
...,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
geom_lm(
mapping = NULL,
data = NULL,
stat = "lm",
position = "identity",
interval = c("none", "prediction", "confidence"),
level = 0.95,
formula = y ~ x,
lm.args = list(),
backtrans = identity,
...,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
Arguments
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
geom , stat |
Use to override the default connection between
|
position |
Position adjustment, either as a string naming the adjustment
(e.g. |
interval |
One of |
level |
The level used for confidence or prediction intervals |
formula |
a formula describing the model in terms of |
lm.args |
A list of arguments supplied to |
backtrans |
a function that transforms the response back to
the original scale when the |
... |
Other arguments passed on to |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
Details
Stat calculation is performed by the (currently undocumented)
predictdf
. Pointwise confidence or prediction bands are
calculated using the predict()
method.
See Also
lm()
for details on linear model fitting.
Examples
ggplot(data = mosaicData::KidsFeet, aes(y = length, x = width, color = sex)) +
geom_lm() +
geom_point()
ggplot(data = mosaicData::KidsFeet, aes(y = length, x = width, color = sex)) +
geom_lm(interval = "prediction", color = "skyblue") +
geom_lm(interval = "confidence") +
geom_point() +
facet_wrap(~sex)
# non-standard display
ggplot(data = mosaicData::KidsFeet, aes(y = length, x = width, color = sex)) +
stat_lm(aes(fill = sex),
color = NA, interval = "confidence", geom = "ribbon",
alpha = 0.2
) +
geom_point() +
facet_wrap(~sex)
ggplot(mpg, aes(displ, hwy)) +
geom_lm(
formula = log(y) ~ poly(x, 3), backtrans = exp,
interval = "prediction", fill = "skyblue"
) +
geom_lm(
formula = log(y) ~ poly(x, 3), backtrans = exp, interval = "confidence",
color = "red"
) +
geom_point()