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.
Usage
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
)Arguments
- mapping
Set of aesthetic mappings created by
aes(). If specified andinherit.aes = TRUE(the default), it is combined with the default mapping at the top level of the plot. You must supplymappingif there is no plot mapping.- data
The data to be displayed in this layer. There are three options:
If
NULL, the default, the data is inherited from the plot data as specified in the call toggplot().A
data.frame, or other object, will override the plot data. All objects will be fortified to produce a data frame. Seefortify()for which variables will be created.A
functionwill be called with a single argument, the plot data. The return value must be adata.frame, and will be used as the layer data. Afunctioncan be created from aformula(e.g.~ head(.x, 10)).- stat
The statistical transformation to use on the data for this layer. When using a
geom_*()function to construct a layer, thestatargument can be used to override the default coupling between geoms and stats. Thestatargument accepts the following:A
Statggproto subclass, for exampleStatCount.A string naming the stat. To give the stat as a string, strip the function name of the
stat_prefix. For example, to usestat_count(), give the stat as"count".For more information and other ways to specify the stat, see the layer stat documentation.
- 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
positionargument accepts the following:The result of calling a position function, such as
position_jitter(). This method allows for passing extra arguments to the position.A string naming the position adjustment. To give the position as a string, strip the function name of the
position_prefix. For example, to useposition_jitter(), give the position as"jitter".For more information and other ways to specify the position, see the layer position documentation.
- ...
Other arguments passed on to
layer()'sparamsargument. These arguments broadly fall into one of 4 categories below. Notably, further arguments to thepositionargument, or aesthetics that are required can not be passed through.... Unknown arguments that are not part of the 4 categories below are ignored.Static aesthetics that are not mapped to a scale, but are at a fixed value and apply to the layer as a whole. For example,
colour = "red"orlinewidth = 3. The geom's documentation has an Aesthetics section that lists the available options. The 'required' aesthetics cannot be passed on to theparams. Please note that while passing unmapped aesthetics as vectors is technically possible, the order and required length is not guaranteed to be parallel to the input data.When constructing a layer using a
stat_*()function, the...argument can be used to pass on parameters to thegeompart of the layer. An example of this isstat_density(geom = "area", outline.type = "both"). The geom's documentation lists which parameters it can accept.Inversely, when constructing a layer using a
geom_*()function, the...argument can be used to pass on parameters to thestatpart of the layer. An example of this isgeom_area(stat = "density", adjust = 0.5). The stat's documentation lists which parameters it can accept.The
key_glyphargument oflayer()may also be passed on through.... This can be one of the functions described as key glyphs, to change the display of the layer in the legend.
- shape
How each spring is drawn:
"zigzag"(the default): the classic textbook spring."wave": a smooth wave, a coil seen exactly from the side."coil": round coils when compressed, opening into a wave when stretched.
With
"zigzag"and"wave"every spring has the same form, so only the spacing of its coils shows how far it is stretched. This makes springs of different lengths easy to compare.- n
Number of points used to draw each coil of a spring (ignored for
"zigzag", which needs only its turning points).- arrow
specification for arrow heads, as created by
grid::arrow().- lineend
Line end style (round, butt, square).
- linejoin
Line join style (round, mitre, bevel).
- na.rm
If
FALSE, the default, missing values are removed with a warning. IfTRUE, missing values are silently removed.- show.legend
logical. Should this layer be included in the legends?
NA, the default, includes if any aesthetics are mapped.FALSEnever includes, andTRUEalways includes. It can also be a named logical vector to finely select the aesthetics to display. To include legend keys for all levels, even when no data exists, useTRUE. IfNA, all levels are shown in legend, but unobserved levels are omitted.- inherit.aes
If
FALSE, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions that define both data and aesthetics and shouldn't inherit behaviour from the default plot specification, e.g.annotation_borders().
Details
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.
Aesthetics
geom_spring() understands the following aesthetics (required aesthetics are in bold):
xyxendyendcolourlinewidthlinetypealphadiametercoilsweight
The spring-specific aesthetics are:
diameter: Diameter of the spring coils, in mm (default3).coils: Number of coils (turns) in each spring (default10). Need not be a whole number.weight: The stiffness (spring constant) of each spring (default1). 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.
See also
geom_residual_springs() to draw springs from the observations to
a fitted smooth.
Examples
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")
