Parametrization of ggplot2::geom_segment either by location and displacement or by magnitude and angle with default arrows. geom_arrow() is the same as geom_vector() but defaults to preserving the direction under coordinate transformation and different plot ratios.
data
mapping: Set of aesthetic mappings created by aes(). If specified and inherit.aes = TRUE (the default), it is combined with the default mapping at the top level of the plot. You must supply mapping if 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 to ggplot().
A data.frame, or other object, will override the plot data. All objects will be fortified to produce a data frame. See fortify() for which variables will be created.
A function will be called with a single argument, the plot data. The return value must be a data.frame, and will be used as the layer data. A function can be created from a formula (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, the stat
argument can be used the override the default coupling between geoms and stats. The stat argument accepts the following:
A Stat ggproto subclass, for example StatCount.
A string naming the stat. To give the stat as a string, strip the function name of the stat_ prefix. For example, to use stat_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 position argument 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 use position_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()'s params argument. These arguments broadly fall into one of 4 categories below. Notably, further arguments to the position argument, 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"
or linewidth = 3. The geom's documentation has an Aesthetics
section that lists the available options. The 'required' aesthetics cannot be passed on to the params. 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 the geom part of the layer. An example of this is stat_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 the stat part of the layer. An example of this is geom_area(stat = "density", adjust = 0.5). The stat's documentation lists which parameters it can accept.
The key_glyph argument of layer() 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.
start: starting angle for rotation in degrees
direction: direction of rotation (counter-clockwise or clockwise)
pivot: numeric indicating where to pivot the arrow where 0 means at the beginning and 1 means at the end.
preserve.dir: logical indicating whether to preserve direction or not
min.mag: minimum magnitude for plotting vectors
skip, skip.x, skip.y: numeric specifying number of gridpoints not to draw in the x and y direction
arrow.length, arrow.angle, arrow.ends, arrow.type: parameters passed to grid::arrow
arrow: specification for arrow heads, as created by grid::arrow().
lineend: Line end style (round, butt, square).
na.rm: If FALSE, the default, missing values are removed with a warning. If TRUE, 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. FALSE never includes, and TRUE always includes. It can also be a named logical vector to finely select the aesthetics to display.
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. borders().
Details
Direction and start allows to work with different standards. For the meteorological standard, for example, use star = -90 and direction = "cw".
Aesthetics
geom_vector understands the following aesthetics (required aesthetics are in bold)
x
y
either mag and angle , or dx and dy
alpha
colour
linetype
size
lineend
Examples
library(data.table)library(ggplot2)data(seals)# If the velocity components are in the same units as the axis,# geom_vector() (or geom_arrow(preserve.dir = TRUE)) might be a better optionggplot(seals, aes(long, lat))+ geom_arrow(aes(dx = delta_long, dy = delta_lat), skip =1, color ="red")+ geom_vector(aes(dx = delta_long, dy = delta_lat), skip =1)+ scale_mag()data(geopotential)geopotential <- copy(geopotential)[date == date[1]]geopotential[, gh.z := Anomaly(gh), by = .(lat)]geopotential[, c("u","v"):= GeostrophicWind(gh.z, lon, lat)](g <- ggplot(geopotential, aes(lon, lat))+ geom_arrow(aes(dx = dlon(u, lat), dy = dlat(v)), skip.x =3, skip.y =2, color ="red")+ geom_vector(aes(dx = dlon(u, lat), dy = dlat(v)), skip.x =3, skip.y =2)+ scale_mag( guide ="none"))# A dramatic illustration of the difference between arrow and vectorg + coord_polar()# When plotting winds in a lat-lon grid, a good way to have both# the correct direction and an interpretable magnitude is to define# the angle by the longitud and latitude displacement and the magnitude# by the wind velocity. That way arrows are always parallel to streamlines# and their magnitude are in the correct units.ggplot(geopotential, aes(lon, lat))+ geom_contour(aes(z = gh.z))+ geom_vector(aes(angle = atan2(dlat(v), dlon(u, lat))*180/pi, mag = Mag(v, u)), skip =1, pivot =0.5)+ scale_mag()# Sverdrup transportlibrary(data.table)b <-10d <-10grid <- as.data.table(expand.grid(x = seq(1, d, by =0.5), y = seq(1, b, by =0.5)))grid[, My :=-sin(pi*y/b)*pi/b]grid[, Mx :=-pi^2/b^2*cos(pi*y/b)*(d - x)]ggplot(grid, aes(x, y))+ geom_arrow(aes(dx = Mx, dy = My))# Due to limitations in ggplot2 (see: https://github.com/tidyverse/ggplot2/issues/4291),# if you define the vector with the dx and dy aesthetics, you need# to explicitly add scale_mag() in order to show the arrow legend.ggplot(grid, aes(x, y))+ geom_arrow(aes(dx = Mx, dy = My))+ scale_mag()# Alternative, use Mag and Angle.ggplot(grid, aes(x, y))+ geom_arrow(aes(mag = Mag(Mx, My), angle = Angle(Mx, My)))