Learn R Programming

coursekata (version 0.19.0)

gf_resid: Add Residual Lines to a Plot

Description

This function adds vertical lines representing residuals from a linear model to a ggformula plot. The residuals are drawn from the observed data points to the predicted values from the model.

Usage

gf_resid(plot, model, linewidth = 0.2, ...)

Value

A ggplot object with residual lines added.

Arguments

plot

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

model

A fitted linear model object created using lm().

linewidth

A numeric value specifying the width of the residual lines. Default is 0.2.

...

Additional aesthetics passed to geom_segment(), such as color, alpha, linetype.

Examples

Run this code
Height_model <- lm(Thumb ~ Height, data = Fingers)
gf_point(Thumb ~ Height, data = Fingers) %>%
  gf_model(Height_model) %>%
  gf_resid(Height_model, color = "red", alpha = 0.5)

Run the code above in your browser using DataLab