geom_stream_field() creates a ggplot2 layer that integrates a user-defined
vector field function \(f(x, y) \to (dx, dy)\) over a grid of seed points
within a specified domain. The function numerically integrates the field
starting from these seeds, producing streamlines that visualize the flow.
This is useful for visualizing vector fields, flow patterns, or trajectories,
such as in fluid dynamics or gradient fields.
Usage
geom_stream_field(
mapping = NULL,
data = NULL,
stat = StatStreamField,
position = "identity",
...,
na.rm = FALSE,
show.legend = NA,
inherit.aes = FALSE,
fun,
xlim = NULL,
ylim = NULL,
n = 11,
args = list(),
max_it = 1000L,
tol = sqrt(.Machine$double.eps),
T = NULL,
L = NULL,
center = TRUE,
type = "stream",
normalize = TRUE,
tail_point = FALSE,
eval_point = FALSE,
grid = NULL,
domain = NULL,
method = "rk4",
lineend = "butt",
linejoin = "round",
linemitre = 10,
arrow = grid::arrow(angle = 30, length = unit(0.02, "npc"), type = "closed")
)
stat_stream_field(
mapping = NULL,
data = NULL,
geom = GeomStream,
position = "identity",
...,
na.rm = FALSE,
show.legend = NA,
inherit.aes = FALSE,
fun,
xlim = NULL,
ylim = NULL,
n = 11,
args = list(),
max_it = 1000,
tol = sqrt(.Machine$double.eps),
T = NULL,
L = NULL,
center = TRUE,
type = "stream",
normalize = TRUE,
tail_point = FALSE,
eval_point = FALSE,
grid = NULL,
domain = NULL,
method = "rk4",
lineend = "butt",
linejoin = "round",
linemitre = 10,
arrow = grid::arrow(angle = 30, length = unit(0.02, "npc"), type = "closed")
)
geom_stream_field2(
mapping = NULL,
data = NULL,
stat = StatStreamField,
position = "identity",
...,
na.rm = FALSE,
show.legend = NA,
inherit.aes = FALSE,
fun,
xlim = NULL,
ylim = NULL,
n = 11,
args = list(),
max_it = 1000,
tol = sqrt(.Machine$double.eps),
L = NULL,
center = FALSE,
type = "stream",
tail_point = TRUE,
eval_point = FALSE,
grid = NULL,
domain = NULL,
lineend = "butt",
linejoin = "round",
linemitre = 10,
method = "rk4"
)
stat_stream_field2(
mapping = NULL,
data = NULL,
geom = GeomStream,
position = "identity",
...,
na.rm = FALSE,
show.legend = NA,
inherit.aes = FALSE,
fun,
xlim = NULL,
ylim = NULL,
n = 11,
args = list(),
max_it = 1000,
tol = sqrt(.Machine$double.eps),
L = NULL,
center = FALSE,
type = "stream",
tail_point = TRUE,
eval_point = FALSE,
grid = NULL,
domain = NULL,
lineend = "butt",
linejoin = "round",
linemitre = 10,
method = "rk4"
)Arguments
- mapping
A set of aesthetic mappings created by
ggplot2::aes(). (Optional)- data
A data frame or other object, as in
ggplot2::layer(). (Optional)- stat
The statistical transformation to use on the data (default: StatStreamField).
- position
Position adjustment, either as a string or the result of a position adjustment function.
- ...
Other arguments passed to
ggplot2::layer()and the underlying geometry/stat.- na.rm
Logical. 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?
- inherit.aes
Logical. If
FALSE, overrides the default aesthetics rather than combining with them.- fun
A function of two variables,
fun(x, y), returning a two-element vector \((dx, dy)\) that defines the local flow direction at any point.- xlim
Numeric vector of length 2 specifying the domain limits in the \(x\)-direction. Defaults to \(c(-1, 1)\).
- ylim
Numeric vector of length 2 specifying the domain limits in the \(y\)-direction. Defaults to \(c(-1, 1)\).
- n
Integer or two-element numeric vector specifying the grid resolution (number of seed points) along each axis. Defaults to
11, producing an \(11 \times 11\) grid.- args
A list of additional arguments passed to
fun.- max_it
integer(1); Maximum number of integration steps per streamline (default:1000L).- tol
numeric(1); a tolerance used to determine if a sink has been hit, among other things (default:sqrt(.Machine$double.eps)).- T
Numeric. Used only with
normalize = FALSE. Each stream is run for timeTandLis ignored; withcenter = TRUEit runs forTforward andTbackward from the seed. Vectors (type = "vector") are always drawn asfun(x, y)scaled by a time of 1. Defaults toNULL.- L
Numeric. The arc length of each stream or vector. Streams are grown to length
L(withcenter = TRUE,L/2in each direction from the seed); a stream that reaches a sink first stops there. Withnormalize = FALSEand noT, all streams are then cut back to the time the fastest one took to reachL.Lis not used whenTis given for streams, or whennormalize = FALSEfor vectors. Defaults toNULL, which uses 85% of the spacing of the grid.- center
Logical. If
TRUE(default), centers the seed points (or resulting streamlines) so that the original (x, y) becomes the midpoint.- type
Character. Either
"stream"(default) or"vector"."stream"computes a full streamline by integrating in both directions (ifcenter = TRUE), while"vector"computes a single vector.- normalize
Logical. If
TRUE(the default), every stream or vector has the same lengthL, and speed is shown by color. IfFALSE, lengths show speed: vectors are drawn at their own size,fun(x, y), and streams are either run for timeTor, ifTis not given, grown toLand cut back to the time the fastest stream took to reachL.- tail_point
Logical. If
TRUE, draws a point at the tail (starting point) of each streamline. Defaults toFALSE.- eval_point
Logical. If
TRUE, draws a point at the evaluation point where the field was computed. Defaults toFALSE.- grid
A data frame containing precomputed grid points for seed placement. If
NULL(default), a regular Cartesian grid is generated based onxlim,ylim, andn. For streams,grid = "even"places evenly spaced streamlines instead (Jobard and Lefer, 1997): each stream is grown in both directions until it comes within half a grid spacing of another stream, reaches a sink or leaves the plot, and new streams are started a grid spacing to either side of earlier ones, so the streams fill the plot about one grid spacing apart.L,Tandnormalizeare not used then; increasenfor denser streams.- domain
A function that restricts the field to a region, or
NULL(the default) for the whole rectanglexlimbyylim. It is called likefun, with a pointc(x, y), and returnsTRUEfor points in the region. Seeds outside the region are dropped, and the field is taken to be undefined outside it, so streams stop at its boundary. Use it, for example, whenfunis only defined on part of the plot.- method
Character. Integration method.
"rk4"(Runge-Kutta 4, the default) and"euler"(Euler's method) step through the field in equal shares of the arc lengthLor, when streams are run to a timeT, of that time. A step is shortened where the field turns or changes speed sharply, so a stream that runs into a sink or a singularity (such as a point charge) stops there, short ofL, while a stream that meets a jump in the field (for example one defined withsign()orifelse()) carries on across it, unless the field on the far side points back. A stream that reaches a line the field converges on from both sides stops there; the exact solution would slide along the line. Sinks, singularities and jumps are told apart by how sharply the field turns and changes speed, so a field built to sit on those thresholds can be misjudged."lsoda"uses the adaptive solver fromdeSolve::ode(), stopping each stream when its arc length reachesL; it was the integrator in ggvfields 1.0.1.geom_stream_smooth()andgeom_gradient_smooth()always use"rk4".- lineend
Line end style (round, butt, square).
- linejoin
Line join style (round, mitre, bevel).
- linemitre
Line mitre limit (number greater than 1).
- arrow
A
grid::arrow()specification for adding arrowheads to the streamlines. Defaults to a closed arrow with a 30° angle and length0.02npc.- geom
The geometric object used to render the streamlines (defaults to GeomStream).
Details
There are two variants:
geom_stream_field()grows every stream to the same lengthLand maps its average speed,avg_spd, to color.geom_stream_field2()grows every stream to lengthL, finds the time the fastest one took, and crops all streams back to that time, so a stream's length shows its speed relative to the fastest one. Color is not mapped and each stream's seed is marked with a dot. Withtype = "vector"it draws vectors of equal length instead.
Aesthetics
geom_stream_field() (and its stat variant) inherit
aesthetics from GeomStream and understand the following:
x: x-coordinate of the seed point.y: y-coordinate of the seed point.color: Color, typically used to represent computed statistics (e.g. average speed).linetype: Type of line used to draw the streamlines.linewidth: Thickness of the streamlines.alpha: Transparency of the streamlines.
Details
The streamlines are generated by numerically integrating
the vector field defined by fun(x, y). With normalize = TRUE, each
stream stops once its arc length reaches L, and T is ignored. With
normalize = FALSE, each stream runs for time T if it is given (and L
is ignored); otherwise each is grown to L and all are cut back to the
time of the fastest one. The computed paths are rendered by GeomStream.
Computed Variables
The following variables are computed internally by StatStreamField during the integration of the vector field:
- avg_spd
The average speed along the stream: its arc length divided by the time it took. Streams run to a time
T(and, withnormalize = FALSE, streams cut back to the time of the fastest one) use the time they were given, even if they stopped early at a sink. A stream run to a lengthLthat stops short of it at a sink would take unbounded time to reach the sink, so its exact average speed is 0; instead its speed is measured up to 99% of the length it achieved, so that its color reflects how fast it moved while it visibly moved. This is the default color of streams.- t
The integration time at each computed point along a streamline.
- d
The distance between consecutive points along the computed path.
- l
The cumulative arc length along the streamline, calculated as the cumulative sum of
d.
Examples
f <- function(u) c(-u[2], u[1])
# the basic usage involves providing a fun, xlim, and ylim
ggplot() + geom_stream_field(fun = f, xlim = c(-1,1), ylim = c(-1,1))
# \donttest{
# if unspecified, xlim and ylim default to c(-1,1). we use this in what
# follows to focus on other parts of the code
ggplot() + geom_stream_field(fun = f)
ggplot() + geom_stream_field(fun = f, center = FALSE)
ggplot() + geom_stream_field(fun = f, normalize = FALSE)
ggplot() + geom_stream_field(fun = f, normalize = FALSE, center = FALSE)
# run systems until specified lengths
ggplot() + geom_stream_field(fun = f, normalize = TRUE, L = .8)
ggplot() + geom_vector_field(fun = f, normalize = TRUE, L = .3)
ggplot() + geom_vector_field(fun = f, normalize = FALSE, L = 2)
#> Warning: Specifying L with non normalized vectors is incompatible. Ignoring L.
# run systems for specified times
ggplot() + geom_stream_field(fun = f, normalize = FALSE, T = .1)
# tail and eval points
ggplot() + geom_stream_field(fun = f, tail_point = TRUE)
ggplot() + geom_stream_field(fun = f, eval_point = TRUE)
# changing the grid of evaluation
ggplot() + geom_stream_field(fun = f)
ggplot() + geom_stream_field(fun = f, grid = "hex")
ggplot() + geom_stream_field(fun = f, grid = "hex", n = 5)
ggplot() + geom_stream_field(fun = f, n = 5)
ggplot() + geom_stream_field(fun = f, xlim = c(-5, 5)) + coord_equal()
ggplot() + geom_stream_field(fun = f, xlim = c(-5, 5), n = c(21, 11)) + coord_equal()
ggplot() + geom_stream_field(fun = f)
ggplot() + geom_stream_field(fun = f, grid = grid_hex(c(-1,1), c(-1,1), .2))
# using other ggplot2 tools
f <- efield_maker()
ggplot() + geom_stream_field(fun = f, xlim = c(-2,2), ylim = c(-2,2))
ggplot() +
geom_stream_field(fun = f, xlim = c(-2,2), ylim = c(-2,2)) +
scale_color_viridis_c(trans = "log10")
ggplot() +
geom_stream_field(fun = f, xlim = c(-2,2), ylim = c(-2,2)) +
scale_color_viridis_c(trans = "log10") +
coord_equal()
# other vector fields
f <- function(u) u
ggplot() + geom_stream_field(fun = f, xlim = c(-1,1), ylim = c(-1,1))
f <- function(u) c(2,1)
ggplot() + geom_stream_field(fun = f, xlim = c(-1,1), ylim = c(-1,1))
# neat examples
f <- function(u) {
x <- u[1]; y <- u[2]
c(y, y*(-x^2 - 2*y^2 + 1) - x)
}
ggplot() + geom_stream_field(fun = f, xlim = c(-2,2), ylim = c(-2,2))
ggplot() + geom_stream_field(fun = f, xlim = c(-2,2), ylim = c(-2,2), type = "vector")
f <- function(u) {
x <- u[1]; y <- u[2]
c(y, x - x^3)
}
ggplot() + geom_stream_field(fun = f, xlim = c(-2,2), ylim = c(-2,2))
ggplot() + geom_stream_field(fun = f, xlim = c(-2,2), ylim = c(-2,2),
grid = grid_hex(c(-2,2), c(-2,2), .35))
f <- function(u) {
x <- u[1]; y <- u[2]
c(x^2 - y^2, x^2 + y^2 - 2)
}
ggplot() + geom_stream_field(fun = f, xlim = c(-2,2), ylim = c(-2,2))
ggplot() + geom_stream_field(fun = f, xlim = c(-2,2), ylim = c(-2,2),
grid = grid_hex(c(-2,2), c(-2,2), .35))
ggplot() +
geom_stream_field(fun = f, aes(alpha = after_stat(t)), xlim = c(-2,2), ylim = c(-2,2)) +
scale_alpha(range = c(0,1))
ggplot() +
geom_stream_field(
fun = f, xlim = c(-1,1), ylim = c(-1,1),
linewidth = .75, arrow = arrow(length = unit(0.015, "npc"))
)
# }