gf_square_resid_fun: Add Squared Residual Visualization from a Function to a Plot

View source: R/gf_square_resid_fun.R

gf_square_resid_funR Documentation

Add Squared Residual Visualization from a Function to a Plot

Description

[Experimental]

Usage

gf_square_resid_fun(plot, fun, aspect = 4/6, alpha = 0.1, ...)

Arguments

plot

A ggformula/ggplot object, typically created with gf_point().

fun

A function that takes a numeric vector x and returns predicted y.

aspect

A numeric value controlling the square's aspect ratio. Default is 4/6.

alpha

Transparency of the filled squares. Default 0.1.

...

Additional aesthetics passed to ggplot2::geom_polygon(), e.g., color, fill, linetype.

Details

Draws squared residual polygons between observed points and predicted values computed by a user-supplied function of x.

Value

A ggplot object with squared residual polygons added.

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_square_resid_fun(my_fun, color = "red", alpha = 0.3)

coursekata documentation built on March 11, 2026, 1:06 a.m.