| gf_model | R Documentation |
When teaching about regression it can be useful to visualize the data as a point plot with the
outcome on the y-axis and the explanatory variable on the x-axis. For regression models, this is
most easily achieved by calling ggformula::gf_lm(), with empty models
ggformula::gf_hline() using the mean, and a more complicated call to
ggformula::gf_segment() for group models. This function simplifies this
by making a guess about what kind of model you are plotting (empty/null, regression, group) and
then making the appropriate plot layer for it.
gf_model(
object = NULL,
gformula = NULL,
data = NULL,
...,
model,
xlab,
ylab,
title,
subtitle,
caption,
geom = "model",
stat = "model",
position = "identity",
show.legend = NA,
show.help = NULL,
inherit = TRUE,
environment = parent.frame()
)
object |
A plot created with the |
gformula |
Not used. |
data |
Not used. The layer draws the model's predictions, which are computed from the data the plot was built from. |
... |
Additional arguments. Typically these are (a) ggplot2 aesthetics to be set with
With no With a supplied model,
|
model |
The model to draw. Either a model already fit by |
xlab, ylab, title, subtitle, caption |
Labels for the plot. |
geom |
Not set by the caller. The geometry is derived from the model. |
stat, position |
With a named |
show.legend |
Whether this layer contributes to the legend. |
show.help |
Print the layer's own help instead of drawing. |
inherit |
Not set by the caller. A named intercept stands alone; other
named models inherit compatible plot aesthetics while stating their own
predicted outcome. With no |
environment |
The environment mappings are resolved in. |
This function only works with models that have a continuous outcome measure.
A ggplot object with the model added. With no model, and a plot
whose positional mapping is an expression rather than a bare variable (shuffle(body_mass_kg),
log(flipper_length_m)), the RETURNED plot is pinned to the values it drew when gf_model()
was called – its data gains a fixed column and its mapping names it, while its axis titles
and everything else about how it reads keep your own words. The plot passed IN is untouched.
gf_model() is built for and tested against plots made with
ggformula::gf_point(), ggformula::gf_jitter(), ggformula::gf_boxplot(),
ggformula::gf_violin() and ggformula::gf_histogram(). Other plots may
work if they map their variables the same way, but they are not tested.
# the empty model predicts the same value (the mean) for every observation
empty_model <- lm(body_mass_kg ~ NULL, data = penguins)
gf_histogram(~body_mass_kg, data = penguins, binwidth = 0.25) %>%
gf_model(empty_model)
# a two-group model (categorical explanatory variable) on a jitter plot
gentoo_model <- lm(body_mass_kg ~ gentoo, data = penguins)
gf_jitter(body_mass_kg ~ gentoo, data = penguins, width = .1) %>%
gf_model(gentoo_model)
# a three-group model works the same way
species_model <- lm(body_mass_kg ~ species, data = penguins)
gf_jitter(body_mass_kg ~ species, data = penguins, width = .1) %>%
gf_model(species_model)
# group models can also be layered onto faceted histograms
gf_histogram(~body_mass_kg, data = penguins, binwidth = 0.25) %>%
gf_facet_grid(species ~ .) %>%
gf_model(species_model)
# a regression model (quantitative explanatory variable) on a scatter plot
flipper_model <- lm(body_mass_kg ~ flipper_length_m, data = penguins)
gf_point(body_mass_kg ~ flipper_length_m, data = penguins) %>%
gf_model(flipper_model)
# layer the empty model and the regression model in different colors to
# compare the two models on the same plot
gf_point(body_mass_kg ~ flipper_length_m, data = penguins) %>%
gf_model(empty_model, color = "dodgerblue") %>%
gf_model(flipper_model, color = "firebrick")
# with a categorical and a quantitative predictor, the model is drawn
# as one line for each group
ancova_model <- lm(body_mass_kg ~ species + flipper_length_m, data = penguins)
gf_point(body_mass_kg ~ flipper_length_m, color = ~species, data = penguins) %>%
gf_model(ancova_model)
# a model that has not been fit yet can be written as a formula, and is fit
# against the data the plot was built from
gf_point(body_mass_kg ~ flipper_length_m, data = penguins) %>%
gf_model(body_mass_kg ~ flipper_length_m)
# the empty model, written as a formula
gf_histogram(~body_mass_kg, data = penguins, binwidth = 0.25) %>%
gf_model(body_mass_kg ~ NULL)
# with no model, gf_model() draws the model the plot implies: a numeric
# predictor draws the regression line gf_lm() would fit
gf_point(body_mass_kg ~ flipper_length_m, data = penguins) %>%
gf_model()
# a categorical predictor draws one mark at each group's mean
gf_jitter(body_mass_kg ~ species, data = penguins, width = .1) %>%
gf_model()
# a plot that draws only its outcome implies the grand mean
gf_histogram(~body_mass_kg, data = penguins, binwidth = 0.25) %>%
gf_model()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.