geom_vector_field {ggvfields} | R Documentation |
Vector Field Layers for ggplot2
Description
These functions provide convenient ggplot2 layers for drawing vector fields using streamlines.
Usage
geom_vector_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(),
center = TRUE,
normalize = TRUE,
tail_point = FALSE,
eval_point = FALSE,
grid = NULL,
lineend = "butt",
linejoin = "round",
linemitre = 10,
arrow = grid::arrow(angle = 30, length = unit(0.02, "npc"), type = "closed")
)
stat_vector_field(
mapping = NULL,
data = NULL,
stat = StatStreamField,
geom = GeomStream,
position = "identity",
...,
na.rm = FALSE,
show.legend = NA,
inherit.aes = FALSE,
fun,
xlim = NULL,
ylim = NULL,
n = 11,
args = list(),
center = TRUE,
normalize = TRUE,
tail_point = FALSE,
eval_point = FALSE,
grid = NULL,
lineend = "butt",
linejoin = "round",
linemitre = 10,
arrow = grid::arrow(angle = 30, length = unit(0.02, "npc"), type = "closed")
)
geom_vector_field2(
mapping = NULL,
data = NULL,
stat = StatStreamField,
position = "identity",
...,
na.rm = FALSE,
show.legend = TRUE,
inherit.aes = FALSE,
fun,
xlim = NULL,
ylim = NULL,
n = 11,
args = list(),
center = FALSE,
tail_point = TRUE,
eval_point = FALSE,
grid = NULL,
lineend = "butt",
linejoin = "round",
linemitre = 10,
arrow = NULL
)
stat_vector_field2(
mapping = NULL,
data = NULL,
geom = GeomStream,
position = "identity",
...,
na.rm = FALSE,
show.legend = TRUE,
inherit.aes = FALSE,
fun,
xlim = NULL,
ylim = NULL,
n = 11,
args = list(),
center = FALSE,
tail_point = TRUE,
eval_point = FALSE,
grid = NULL,
lineend = "butt",
linejoin = "round",
linemitre = 10,
arrow = NULL
)
Arguments
mapping |
A set of aesthetic mappings created by |
data |
A data frame containing the input data. |
stat |
The statistical transformation to use on the data for this layer. Defaults to StatStreamField. |
position |
Position adjustment, either as a string or the result of a call to a position adjustment function. |
... |
Other arguments passed on to |
na.rm |
Logical. If |
show.legend |
Logical. Should this layer be included in the legends? |
inherit.aes |
Logical. If |
fun |
A function that defines the vector field. It should take a numeric
vector of length 2 (representing |
xlim |
Numeric vector of length two. Specifies the limits of the x-axis
domain. Defaults to |
ylim |
Numeric vector of length two. Specifies the limits of the y-axis
domain. Defaults to |
n |
Integer. Grid resolution specifying the number of seed points along
each axis. Higher values produce a denser vector field. Defaults to |
args |
List of additional arguments passed on to the function defined by
|
center |
Logical. If |
normalize |
Logical. If |
tail_point |
Logical. If |
eval_point |
Logical. If |
grid |
A data frame containing precomputed grid points for seed
placement. If |
lineend |
Line end style (round, butt, square). |
linejoin |
Line join style (round, mitre, bevel). |
linemitre |
Line mitre limit (number greater than 1). |
arrow |
A |
geom |
The geometric object used to render the streamline (only used in
|
Details
A user-defined function (fun
) specifies the behavior of the vector field by
taking a numeric vector of length 2 (representing (x, y)
) and returning
a numeric vector of length 2 (representing (dx, dy)
). The underlying
StatStreamField computes the streamlines based on the vector field
function, and GeomStream renders them.
Two variants are provided:
-
geom_vector_field() uses a default mapping that sets
color = after_stat(norm)
. -
geom_vector_field2() uses a default mapping that sets
length = after_stat(norm)
(withcolor
unmapped by default).
Value
A ggplot2 layer that computes and plots a vector field using streamlines.
- norm
Calculated as the Euclidean distance between the starting point (
x
,y
) and the computed endpoint. Used to normalize the vector.
Aesthetics
geom_vector_field()
and geom_vector_field2()
understand the following aesthetics (required aesthetics are in bold):
-
x
: The x-coordinate of the vector's starting point. -
y
: The y-coordinate of the vector's starting point. -
fx
: The horizontal component of the vector displacement. -
fy
: The vertical component of the vector displacement. -
color
: The color of the vector lines (default mapping in geom_vector_field). -
length
: The computed vector norm (default mapping in geom_vector_field2). -
linetype
: The type of the vector line (e.g., solid, dashed). -
linewidth
: The thickness of the vector line. -
alpha
: The transparency of the vector.
See Also
Examples
f <- function(u) c(-u[2], u[1])
ggplot() + geom_vector_field(fun = f, xlim = c(-1,1), ylim = c(-1,1))
# xlim and ylim default to (-1,1), so for ease of illustration we remove them
ggplot() + geom_vector_field(fun = f)
ggplot() + geom_vector_field(fun = f, grid = "hex")
ggplot() + geom_vector_field2(fun = f)
ggplot() + geom_vector_field2(fun = f, grid = "hex")
f <- efield_maker()
ggplot() + geom_vector_field(fun = f, xlim = c(-2,2), ylim = c(-2,2))
ggplot() + geom_vector_field2(fun = f, xlim = c(-2,2), ylim = c(-2,2))