GeomFields {ggfields} | R Documentation |
Arrows depicting a vector field
Description
Visualise vector fields (such as, electric/magnetic fields, wind speed, or water currents) with arrows as a layer in a ggplot.
Usage
GeomFields
geom_fields(
mapping = NULL,
data = NULL,
stat = "fields",
position = "identity",
na.rm = FALSE,
show.legend = NA,
max_radius = ggplot2::unit(0.5, "cm"),
.angle_correction = angle_correction,
arrow = grid::arrow(length = ggplot2::unit(0.2, "cm")),
inherit.aes = TRUE,
...
)
Arguments
mapping |
Set of aesthetic mappings created by |
data |
Can be one of four things:
|
stat |
The statistical transformation to use on the data for this layer. By default it is
set to |
position |
Position adjustment, either as a string naming the adjustment
(e.g. |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
max_radius |
Maximum radius to which the |
.angle_correction |
Function to correct the angle in the aesthetics for the projection and/or
aspect ratio used in the plot. When set to |
arrow |
specification for arrow heads, as created by |
inherit.aes |
If |
... |
Other arguments passed on to |
Format
An object of class GeomFields
(inherits from GeomSegment
, Geom
, ggproto
, gg
) of length 8.
Details
Adds a layer with vector fields to a ggplot. In order to achieve this
two special aesthetic are required: radius
and angle
.
Value
A layer which can be added to a ggplot.
Aesthetics
-
geometry|x
: Either ageometry
column orx
coordinate. In case ofgeometry
the column should be of class sf::sfc_POINT. In case ofx
, it should be anumeric
vector
, and the aestheticy
needs to be specified as well. It specifies the location of the origin of each vector. -
radius
: This aesthetic will be used to scale the radius of the vector arrows in the field you wish to display. The maximum radius of the arrows is given by parametermax_radius
. Seevignette("radius_aes")
for more details. -
angle
: This aesthetic represent the angles of the vectors in your field in radians. Contrary to the mathematical definition, an angle of 0 radians will point upwards (instead of to the right). This was chosen such because in most geographical applications an angle of zero degrees points Northwards. Before plotting these angles are corrected by the function passed to the.angle_correction
argument. Seevignette("angle_corrections)
for more details. -
y
: This aesthetic needs to be used in combination with thex
aesthetic. It needs to be anumeric
vector
. -
fill
: Seevignette("ggplot2-specs", "ggplot2")
-
colour
: Seevignette("ggplot2-specs", "ggplot2")
-
linetype
: Seevignette("ggplot2-specs", "ggplot2")
-
linewidth
: Seevignette("ggplot2-specs", "ggplot2")
-
alpha
: A variable to control the opacity of an element.
Author(s)
Pepijn de Vries
Examples
data(seawatervelocity)
if (requireNamespace("ggplot2") && requireNamespace("stars") &&
requireNamespace("scales")) {
library(ggplot2)
library(stars)
sw_df <- as.data.frame(seawatervelocity)
ggplot(sw_df, aes(x = x, y = y, radius = as.numeric(v), angle = as.numeric(angle))) +
geom_fields(max_radius = unit(0.5, "cm"), na.rm = TRUE)
ggplot() +
geom_fields(data = seawatervelocity,
mapping = aes(radius = as.numeric(v),
angle = as.numeric(angle),
col = as.numeric(v)),
max_radius = unit(0.5, "cm")) +
scale_colour_viridis_c()
}