Create layers for drawing vectors on ggplot2 plots. These functions accept
wide-format data with the required aesthetics x and y plus either
xend and yend or one of the alternative specifications: fx
and fy, or angle/ angle_deg and norm.
Usage
geom_vector(
mapping = NULL,
data = NULL,
stat = StatVector,
position = "identity",
...,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
center = TRUE,
normalize = TRUE,
tail_point = FALSE,
eval_point = FALSE,
L = NULL,
arrow = grid::arrow(angle = 25, length = unit(0.025, "npc"), type = "closed")
)
stat_vector(
mapping = NULL,
data = NULL,
geom = GeomStream,
position = "identity",
...,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
center = TRUE,
normalize = TRUE,
tail_point = FALSE,
eval_point = FALSE,
L = NULL,
arrow = grid::arrow(angle = 25, length = unit(0.025, "npc"), type = "closed")
)
geom_vector2(
mapping = NULL,
data = NULL,
stat = StatVector,
position = "identity",
...,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
center = FALSE,
tail_point = TRUE,
eval_point = FALSE,
L = NULL,
arrow = NULL
)
stat_vector2(
mapping = NULL,
data = NULL,
geom = GeomStream,
position = "identity",
...,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
center = FALSE,
tail_point = TRUE,
eval_point = FALSE,
L = NULL,
arrow = NULL
)Arguments
- mapping
A set of aesthetic mappings created by
ggplot2::aes(). Required: Must includexandy; in addition, eitherxendandyendor one of the alternative specifications (fx/fyorangle/angle_degandnorm) must be provided.- data
A data frame containing the vector data in wide format.
- stat
The statistical transformation to use on the data (default: StatVector).
- position
Position adjustment, either as a string or the result of a position adjustment function.
- ...
Other arguments passed on to
grid::layer().- na.rm
Logical. If
FALSE(the default), missing values are removed with a warning.- 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.- center
Logical. If
TRUE, the vector is recentered so that the original(x, y)becomes the midpoint (default isTRUEforgeom_vector()andFALSEforgeom_vector2()).- normalize
Logical. If
TRUE, the vector endpoints are scaled to unit length before being scaled byL(default:TRUE).- tail_point
Logical. If
TRUE, a point is drawn at the tail (the starting point) of each vector (default isFALSEforgeom_vector()andTRUEforgeom_vector2()).- eval_point
Logical. If
TRUE, a point is drawn at the evaluation point corresponding to the original (untransformed) seed point before any centering or normalization (default:FALSE).- L
Numeric scalar. The length of the vectors in data units when
normalize = TRUE. IfNULL(the default), a value is computed automatically based on the plot’s x and y limits. Whenlengthis mapped, as it is by default ingeom_vector2(), the drawn lengths are set by the length scale instead (seescale_length_continuous()), andLdoes not change them.- arrow
An optional
grid::arrow()specification to add arrowheads to the vectors (default:grid::arrow(angle = 25, length = unit(0.025, "npc"), type = "closed")).- geom
The geometric object used to render the streamline (only used in
stat_stream(); defaults to GeomStream).
Details
When specifying the vector direction using polar coordinates, you can provide either:
angle: the vector direction in radians.angle_deg: the vector direction in degrees (which is automatically converted to radians).
The endpoints are computed by translating the starting point using these
polar coordinates along with the supplied magnitude, norm (also
accepted under the name distance).
The data is converted to long format (two rows per vector) via StatVector and rendered with GeomStream. Optionally, arrowheads can be added to indicate direction.
There are two variants:
geom_vector()andstat_vector(): normalize and center the vectors and add a default mapping forcolor = after_stat(norm), so color shows each vector's norm.geom_vector2()andstat_vector2(): use the same underlying stat (StatVector) but add a default mapping forlength = after_stat(norm)instead, so length shows each vector's norm.
Aesthetics
geom_vector() and geom_vector2() understand the following aesthetics
(required aesthetics are in bold):
x
y
xend
yend
fx (alternative specification)
fy (alternative specification)
angle (vector direction in radians; alternative specification)
angle_deg (vector direction in degrees; alternative specification, converted to radians)
norm (the magnitude, with angle/angle_deg, used to compute endpoints;
distanceis accepted as another name for it)alpha
color
fill
group
linetype
size
Computed Variables
These are calculated by the 'stat' part of layers and can be accessed with delayed evaluation.
- norm
The length of the vector as given in the data, the Euclidean distance from (
x,y) to (xend,yend) (or \(\sqrt{f_x^2 + f_y^2}\)). Withnormalize = TRUEthe vectors are drawn at a common length andnormkeeps the original size.
Examples
set.seed(1234)
n <- 10
# Generate wind data in polar coordinates
data <- data.frame(
x = rnorm(n),
y = rnorm(n),
dir = runif(n, -pi, pi), # angle in radians
spd = rchisq(n, df = 2) # speed
) |>
transform(fx = spd * cos(dir), fy = spd * sin(dir))
# Using fx/fy to compute endpoints
ggplot(data, aes(x, y)) +
geom_vector(aes(fx = fx, fy = fy))
# Using angle (in radians) and norm to compute endpoints
ggplot(data, aes(x, y)) +
geom_vector(aes(angle = dir, norm = spd))
# Using angle_deg (in degrees) and norm to compute endpoints
vectors3 <- data.frame(
x = c(0, 1, 2),
y = c(0, 1, 2),
angle_deg = c(0, 90, 45),
angle = c(0, pi/2, pi/4),
norm = c(3, 4, 5)
)
ggplot(vectors3, aes(x, y)) +
geom_vector(aes(angle_deg = angle_deg, norm = norm))
# Basic usage with explicit start and end points:
vectors1 <- data.frame(
x = c(0, 1, 2),
y = c(0, 1, 2),
xend = c(3, 1, 5),
yend = c(0, 5, 6)
)
ggplot(vectors1, aes(x = x, y = y, xend = xend, yend = yend)) +
geom_vector()
# Using center = TRUE to recenter vectors:
ggplot(vectors1, aes(x = x, y = y, xend = xend, yend = yend)) +
geom_vector(center = TRUE)
# Using normalize = TRUE to adjust vectors to unit length:
ggplot(vectors3, aes(x = x, y = y, angle = angle, norm = norm)) +
geom_vector(normalize = TRUE)
# Using geom_vector2, which adds a default mapping for `length`
ggplot(vectors1, aes(x = x, y = y, xend = xend, yend = yend)) +
geom_vector2()