Skip to contents

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,
  ...
)

Format

An object of class GeomFields (inherits from GeomSegment, Geom, ggproto, gg) of length 8.

Arguments

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

Can be one of four things:

  • NULL: in that case data from the parent ggplot call is inherited.

  • data.frame: you need to assign the x and y aesthetics.

  • sf object: it should contain a geometry column with only POINT geometries.

  • stars object: it will be converted automatically to an sf object.

stat

The statistical transformation to use on the data for this layer. By default it is set to GeomFields() ("fields").

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.

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.

max_radius

Maximum radius to which the radius aesthetic is scaled in the plot. You can use absolute ("e.g., "cm", "in", "pt") and relative ("npc") units to set its value. Default is 0.5 cm.

.angle_correction

Function to correct the angle in the aesthetics for the projection and/or aspect ratio used in the plot. When set to NULL the angle is not corrected and is treated as the angle in the final plot. A custom function can be provided which should accept at least three arguments (data, panel_params and coord). See angle_correction() and vignette("angle_correction") for more details.

arrow

specification for arrow heads, as created by grid::arrow().

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().

...

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.

Value

A layer which can be added to a ggplot.

Details

Adds a layer with vector fields to a ggplot. In order to achieve this two special aesthetic are required: radius and angle.

Aesthetics

  • geometry|x: Either a geometry column or x coordinate. In case of geometry the column should be of class sf::sfc_POINT. In case of x, it should be a numeric vector, and the aesthetic y 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 parameter max_radius. See vignette("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. See vignette("angle_corrections) for more details.

  • y: This aesthetic needs to be used in combination with the x aesthetic. It needs to be a numeric vector.

  • fill: See vignette("ggplot2-specs", "ggplot2")

  • colour: See vignette("ggplot2-specs", "ggplot2")

  • linetype: See vignette("ggplot2-specs", "ggplot2")

  • linewidth: See vignette("ggplot2-specs", "ggplot2")

  • alpha: A variable to control the opacity of an element.

Author

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()
}
#> Angle correction between 0.00 and 0.00 radials