geom_vector: Vector Layers for ggplot2

View source: R/geom_vector.R

geom_vectorR Documentation

Vector Layers for ggplot2

Description

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 include x and y; in addition, either xend and yend or one of the alternative specifications (fx/ fy or angle/ angle_deg and norm) 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 is TRUE for geom_vector() and FALSE for geom_vector2()).

normalize

Logical. If TRUE, the vector endpoints are scaled to unit length before being scaled by L (default: TRUE).

tail_point

Logical. If TRUE, a point is drawn at the tail (the starting point) of each vector (default is FALSE for geom_vector() and TRUE for geom_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. If NULL (the default), a value is computed automatically based on the plot’s x and y limits. When length is mapped, as it is by default in geom_vector2(), the drawn lengths are set by the length scale instead (see scale_length_continuous()), and L does 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() and stat_vector(): normalize and center the vectors and add a default mapping for color = after_stat(norm), so color shows each vector's norm.

  • geom_vector2() and stat_vector2(): use the same underlying stat (StatVector) but add a default mapping for length = after_stat(norm) instead, so length shows each vector's norm.

Value

A ggplot2 layer that can be added to a plot.

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; distance is 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}). With normalize = TRUE the vectors are drawn at a common length and norm keeps 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()


ggvfields documentation built on Nov. 16, 2026, 1:06 a.m.