Add Squared Residual Visualization from a Function to a Plot
Source:R/gf_square_resid_fun.R
gf_square_resid_fun.RdUsage
gf_square_resid_fun(
object = NULL,
gformula = NULL,
data = NULL,
...,
fun,
aspect = 4/6,
alpha = 0.1,
xlab,
ylab,
title,
subtitle,
caption,
geom = coursekata::GeomSquareResid,
stat = coursekata::StatResid,
position = "identity",
show.legend = NA,
show.help = NULL,
inherit = FALSE,
environment = parent.frame()
)Arguments
- object
A ggformula plot object, typically created with
gf_point().- gformula
Not used.
gf_square_resid_fun()measures a function, not an aesthetic formula; a function given positionally lands here and is moved tofun.- 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 ascolororfill, (b) ggplot2 aesthetics to be mapped withattribute = ~ 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 =.- aspect
The square's aspect ratio. Default is
4/6. Must be named.- alpha
The transparency of the square's fill. Default is
0.1. Must be named.- xlab, ylab, title, subtitle, caption
Labels for the plot.
- geom, stat, position
Not set by the caller. A squared residual is drawn by its own geom and stat, and moved by the position the observations are already drawn with, so that a square 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.
FALSE, wheregf_resid_fun()isTRUE: a square is a filled region drawn in the geom's own colors, so inheriting a plot's mappedcoloroutlines every square in the color of the group it measures instead of leaving one neutral area per observation. The axes and the prediction are stated outright, so nothing the square needs is lost by not inheriting. Set it toTRUEto take the outline anyway.- environment
The environment mappings are resolved in.
Details
Draws squared residual polygons between observed points and the values
predicted by a user-supplied function of x. Where gf_square_resid()
measures a fitted model, this measures a function you wrote: it is called on
the x values the plot draws, and each square is built on the residual 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_square_resid_fun(my_fun, color = "red", alpha = 0.3)