| Title: | Draw Springs in 'ggplot2' to Illustrate Least Squares |
|---|---|
| Description: | Implements an extension of 'ggplot2' which draws springs instead of straight lines between pairs of points. Aside from possible artistic use, the main impetus for this is to illustrate least squares problems by their physical analog: each observation is attached by a spring to a fitted point or line, and the stretch of each spring shows the size of its residual. Examples include the sample mean, the multivariate centroid, linear and non-linear regression, and principal components analysis. |
| Authors: | Michael Friendly [aut, cre] (ORCID: <https://orcid.org/0000-0002-3237-0941>) |
| Maintainer: | Michael Friendly <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.5.0 |
| Built: | 2026-11-29 03:08:59 UTC |
| Source: | https://github.com/friendly/ggsprings |
Computes the coordinates of a spring from (x, y) to (xend, yend),
in the same units as the coordinates. This is the geometry used by
geom_spring(), which however computes it in mm on the screen. Use
create_spring() to draw a spring in data coordinates, e.g. with
ggplot2::geom_path() and ggplot2::coord_equal().
create_spring( x, y, xend, yend, diameter = 1, coils = 10, n = 50, shape = c("zigzag", "wave", "coil") )create_spring( x, y, xend, yend, diameter = 1, coils = 10, n = 50, shape = c("zigzag", "wave", "coil") )
x, y
|
Starting coordinates. |
xend, yend
|
Ending coordinates. |
diameter |
Diameter of the coils, in the units of |
coils |
Number of coils (turns) in the spring. |
n |
Number of points used to draw each coil. |
shape |
How the spring is drawn: |
A data frame with columns x and y. It has zero rows when the
spring has zero length.
spring <- create_spring(x = 4, y = 2, xend = 10, yend = 6, diameter = 1, coils = 8) library(ggplot2) ggplot(spring, aes(x, y)) + geom_path() + coord_equal()spring <- create_spring(x = 4, y = 2, xend = 10, yend = 6, diameter = 1, coils = 8) library(ggplot2) ggplot(spring, aes(x, y)) + geom_path() + coord_equal()
Draws a short horizontal spring as the legend key for geom_spring().
draw_key_spring(data, params, size)draw_key_spring(data, params, size)
data, params, size
|
A grob.
library(ggplot2) df <- data.frame(x = 1:3, y = 1, xend = 1:3, yend = 2:4, g = c("a", "b", "c")) ggplot(df, aes(x, y, xend = xend, yend = yend, colour = g)) + geom_spring(key_glyph = draw_key_spring)library(ggplot2) df <- data.frame(x = 1:3, y = 1, xend = 1:3, yend = 2:4, g = c("a", "b", "c")) ggplot(df, aes(x, y, xend = xend, yend = yend, colour = g)) + geom_spring(key_glyph = draw_key_spring)
geom_spring() is similar to ggplot2::geom_segment() in that it connects
a start point (x, y) to an end point (xend, yend), but it draws a
spring instead of a straight line.
geom_spring( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., shape = c("zigzag", "wave", "coil"), n = 50, arrow = NULL, lineend = "butt", linejoin = "round", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )geom_spring( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., shape = c("zigzag", "wave", "coil"), n = 50, arrow = NULL, lineend = "butt", linejoin = "round", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this layer.
When using a
|
position |
A position adjustment to use on the data for this layer. This
can be used in various ways, including to prevent overplotting and
improving the display. The
|
... |
Other arguments passed on to
|
shape |
How each spring is drawn:
With |
n |
Number of points used to draw each coil of a spring (ignored for
|
arrow |
specification for arrow heads, as created by |
lineend |
Line end style (round, butt, square). |
linejoin |
Line join style (round, mitre, bevel). |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
Each spring has a fixed number of coils, so a longer spring is drawn more
stretched out than a shorter one. This is the visual analog of Hooke's law:
the force exerted by a spring is proportional to how far it is stretched.
Springs connecting data points to a fitted line or point therefore show,
directly, the forces whose balance gives the least squares solution.
The coils are computed when the plot is drawn, in physical units (mm), so
they are always round and look the same in every facet panel and group,
whatever the scales of x and y. Only the endpoints are transformed by
the coordinate system; in non-linear coordinate systems such as
ggplot2::coord_polar() the axis of each spring is a straight line on the
screen.
A ggplot2 layer
geom_spring() understands the following aesthetics (required aesthetics are in bold):
x
y
xend
yend
colour
linewidth
linetype
alpha
diameter
coils
weight
The spring-specific aesthetics are:
diameter: Diameter of the spring coils, in mm (default 3).
coils: Number of coils (turns) in each spring (default 10). Need not
be a whole number.
weight: The stiffness (spring constant) of each spring (default 1).
As with a real spring, whose stiffness is inversely proportional to its
number of coils, a stiffer spring is drawn with fewer coils
(coils / weight, but at least 1), and thicker (linewidth * sqrt(weight)).
Used with a weighted fit, this shows weighted least squares: the heavily
weighted observations have short residuals, drawn as short, stiff springs.
geom_residual_springs() to draw springs from the observations to
a fitted smooth.
library(ggplot2) # springs connecting points to their mean set.seed(1234) df <- data.frame(x = runif(10, 0, 10), y = runif(10, 0, 10)) xbar <- mean(df$x) ybar <- mean(df$y) ggplot(df, aes(x, y)) + geom_spring(aes(xend = xbar, yend = ybar), colour = "blue") + geom_point(size = 3, colour = "red") + annotate("point", x = xbar, y = ybar, size = 5, shape = 15) # spring aesthetics, with groups and facets df <- data.frame( x = runif(6, max = 10), y = runif(6, max = 10), xend = runif(6, max = 10), yend = runif(6, max = 10), class = rep(c("a", "b"), 3), coils = c(5, 10, 15, 5, 10, 15) ) ggplot(df, aes(x, y, xend = xend, yend = yend, colour = class)) + geom_spring(aes(coils = coils), diameter = 4) + facet_wrap(~class) # spring shapes df <- data.frame(x = 0, xend = c(2, 5, 10), y = 1:3) p <- ggplot(df, aes(x, y, xend = xend, yend = y)) p + geom_spring(diameter = 5) p + geom_spring(diameter = 5, shape = "wave") p + geom_spring(diameter = 5, shape = "coil")library(ggplot2) # springs connecting points to their mean set.seed(1234) df <- data.frame(x = runif(10, 0, 10), y = runif(10, 0, 10)) xbar <- mean(df$x) ybar <- mean(df$y) ggplot(df, aes(x, y)) + geom_spring(aes(xend = xbar, yend = ybar), colour = "blue") + geom_point(size = 3, colour = "red") + annotate("point", x = xbar, y = ybar, size = 5, shape = 15) # spring aesthetics, with groups and facets df <- data.frame( x = runif(6, max = 10), y = runif(6, max = 10), xend = runif(6, max = 10), yend = runif(6, max = 10), class = rep(c("a", "b"), 3), coils = c(5, 10, 15, 5, 10, 15) ) ggplot(df, aes(x, y, xend = xend, yend = yend, colour = class)) + geom_spring(aes(coils = coils), diameter = 4) + facet_wrap(~class) # spring shapes df <- data.frame(x = 0, xend = c(2, 5, 10), y = 1:3) p <- ggplot(df, aes(x, y, xend = xend, yend = y)) p + geom_spring(diameter = 5) p + geom_spring(diameter = 5, shape = "wave") p + geom_spring(diameter = 5, shape = "coil")
GeomSpring is the ggproto class behind geom_spring(). It is
used for extending ggplot2 and is not intended to be used directly.
These layers use the same fitting machinery as ggplot2::geom_smooth(),
but return the fitted values at the observed x values (or at xseq),
keeping the observed y as yend. This makes it easy to draw the residuals
from any smoothing method and formula as points, segments or springs.
layer_smooth_fit( mapping = NULL, data = NULL, stat = StatSmoothFit, geom = GeomPointAccent, position = "identity", ..., show.legend = NA, inherit.aes = TRUE ) stat_smooth_fit(..., geom = GeomPointAccent) geom_smooth_fit(...) geom_residuals(...) geom_residual_springs(...)layer_smooth_fit( mapping = NULL, data = NULL, stat = StatSmoothFit, geom = GeomPointAccent, position = "identity", ..., show.legend = NA, inherit.aes = TRUE ) stat_smooth_fit(..., geom = GeomPointAccent) geom_smooth_fit(...) geom_residuals(...) geom_residual_springs(...)
mapping, data, position, show.legend, inherit.aes
|
See |
stat |
The stat; defaults to |
geom |
The geom used to draw the result. |
... |
Other arguments passed to the layer, e.g. |
geom_smooth_fit() draws the fitted values as points.
geom_residuals() draws the residuals as line segments.
geom_residual_springs() draws the residuals as springs.
stat_smooth_fit() and layer_smooth_fit() are the general constructors.
A weight aesthetic is used as case weights in the fit (as in
ggplot2::geom_smooth()) and, in geom_residual_springs(), as the
stiffness of each spring (fewer, thicker coils; see geom_spring()), so
that a weighted least squares fit can be shown.
A ggplot2 layer
library(ggplot2) p <- ggplot(mtcars, aes(wt, mpg)) + geom_point() + geom_smooth(method = lm, formula = y ~ x, se = FALSE) p + geom_residual_springs(method = lm, formula = y ~ x) p + geom_residuals(method = lm, formula = y ~ x) # by group ggplot(mtcars, aes(wt, mpg, colour = factor(cyl))) + geom_point() + geom_smooth(method = lm, formula = y ~ x, se = FALSE) + geom_residual_springs(method = lm, formula = y ~ x)library(ggplot2) p <- ggplot(mtcars, aes(wt, mpg)) + geom_point() + geom_smooth(method = lm, formula = y ~ x, se = FALSE) p + geom_residual_springs(method = lm, formula = y ~ x) p + geom_residuals(method = lm, formula = y ~ x) # by group ggplot(mtcars, aes(wt, mpg, colour = factor(cyl))) + geom_point() + geom_smooth(method = lm, formula = y ~ x, se = FALSE) + geom_residual_springs(method = lm, formula = y ~ x)