Skip to contents

[Experimental]

Usage

gf_resid_fun(
  object = NULL,
  gformula = NULL,
  data = NULL,
  ...,
  fun,
  linewidth = 0.2,
  xlab,
  ylab,
  title,
  subtitle,
  caption,
  geom = coursekata::GeomResid,
  stat = coursekata::StatResid,
  position = "identity",
  show.legend = NA,
  show.help = NULL,
  inherit = TRUE,
  environment = parent.frame()
)

Arguments

object

A ggformula plot object, typically created with gf_point().

gformula

Not used. gf_resid_fun() measures a function, not an aesthetic formula; a function given positionally lands here and is moved to fun.

data

Not used. The residuals are measured over the data the plot was built from. Anything supplied here is left for ggformula and ggplot2 to answer, exactly as it is for any other gf_ layer.

...

Additional arguments. Typically these are (a) ggplot2 aesthetics to be set with attribute = value, such as color, alpha or linetype, (b) ggplot2 aesthetics to be mapped with attribute = ~ expression, or (c) attributes of the layer as a whole.

fun

A function of one argument. It is called on the x values the plot draws and must return one predicted y for each of them. May be given positionally or as fun =.

linewidth

The width of the residual lines. Default is 0.2. Must be named.

xlab, ylab, title, subtitle, caption

Labels for the plot.

geom, stat, position

Not set by the caller. A residual is drawn by its own geom and stat, and moved by the position the observations are already drawn with, so that a segment stays on the point it belongs to.

show.legend

Whether this layer contributes to the legend.

show.help

Print the layer's own help instead of drawing.

inherit

Whether the layer inherits the plot's aesthetics. The axes and the prediction are stated outright; everything else – a mapped color, for instance – is inherited from the plot.

environment

The environment mappings are resolved in.

Value

A ggplot object with residual lines added.

Details

Draws residual lines from observed points to the values predicted by a user-supplied function of x (e.g., the function plotted with gf_function()). Where gf_resid() measures a fitted model, this measures a function you wrote: it is called on the x values the plot draws, and each residual runs from an observation to what the function predicts for it.

Examples

set.seed(1)
df <- data.frame(X = 1:10, Y = 2 + 3 * (1:10) + rnorm(10))
my_fun <- function(x) 2 + 3 * x

gf_point(Y ~ X, data = df) %>%
  gf_function(my_fun) %>%
  gf_resid_fun(my_fun, color = "red", alpha = 0.5)