Skip to contents

geom_slice_subtitle() fills the plot subtitle with a description of the plot's geom_slice() layers: the model equation (lm_equation() style) on the first line, and the held values the plot does not otherwise show on the second ("held at: x2 = 2.507", formatted like geom_slice()'s console messages).

Usage

geom_slice_subtitle(model = TRUE, prepend = "", append = "", ..., wrap = TRUE)

Arguments

model

If TRUE (default), the subtitle's first line is the model equation; FALSE drops it, leaving only the held-values line.

prepend, append

Plain strings pasted before the first line and after the last line of the default subtitle.

...

Passed to lm_equation() when the equation is written, under its own argument names — chiefly style, which controls how factor terms are labelled.

Left unset, style is chosen to fit: with wrap on, the equation is tried spelled out (4.09*(Species="setosa")) and briefly (4.09*[setosa]), each with and without the hanging indent, preferring the spelled-out form and giving up the indent last. The first that fits wins, and a message says so if the brief form did. Because the indent goes last, a narrower plot can bring the full names back. The brief form is never offered when two terms would shorten to the same label — a bare [High] from two different factors no longer says which it meant.

Naming style yourself pins it and switches all of that off. Pin it (or give wrap a width) when the annotation must come out the same whatever size device it is drawn on.

wrap

Controls wrapping of lines too long to fit. TRUE (default) measures how much room the subtitle has in this plot on the current graphics device and breaks long lines to fit, between terms only, with continuation lines hanging under the right-hand side of the equal sign. FALSE leaves the text as one line per item, however far it runs off the plot. A number is a width in inches to wrap to instead of measuring — useful when the plot will be drawn at a size other than the current device, as with ggsave(width = ).

Value

An object that sets the plot subtitle when added to a ggplot.

Details

It takes no model or predict_vars — everything is borrowed from the plot's existing geom_slice() layers, so add it after them (and after any geom_slice_text()). The held-values line is conscious of what the plot already labels: variables labeled by geom_slice_text(), pinned by a grouping aesthetic (the legend covers those), or pinned by faceting are left out. User-chosen predict_vars values and imputed defaults are treated the same — both are unlabeled held values. When nothing is held, the line is dropped entirely.

See also

geom_slice() for the layers being described; geom_slice_caption() for the same text in the caption; geom_slice_text() to label the lines themselves.

Examples

library(ggplot2)

# Subtitle reports the model equation and the held Sepal.Width; Species
# is already shown by the legend, so it is left out
model <- lm(Petal.Length ~ Sepal.Length + I(Sepal.Length^2):Species +
              Sepal.Width, data = iris)
ggplot(iris, aes(Sepal.Length, Petal.Length, color = Species)) +
  geom_point() +
  geom_slice(model, interval = "confidence") +
  geom_slice_subtitle()
#> Value for `Sepal.Width` not specified - Used mean: 3.057
#>     To choose a slice, use 'predict_vars = list(Sepal.Width = 3.057)'.