annotation_fields {ggfields}R Documentation

Annotate a ggplot with vector fields

Description

Functions exactly the same as geom_fields(), with that difference that this function does not train the x and y scales. This makes the data central, and uses this layer to support it. Consequently, annotation_fields() does not accept a stat argument.

Usage

annotation_fields(
  mapping = NULL,
  data = NULL,
  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 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.

position

Position adjustment, either as a string naming the adjustment (e.g. "jitter" to use position_jitter), or the result of a call to a position adjustment function. Use the latter if you need to change the settings of the adjustment.

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(). These are often aesthetics, used to set an aesthetic to a fixed value, like colour = "red" or size = 3. They may also be parameters to the paired geom/stat.

Value

A ggplot2::layer_sf().

Author(s)

Pepijn de Vries

Examples

if (requireNamespace("stars") && requireNamespace("ggplot2")) {
  library(stars)
  library(ggplot2)

  data("seawatervelocity")
  sw_sub <- seawatervelocity[,8:13,1:5]
  
  ## Note that the `seawatervelocity` spans a much larger area,
  ## but the plot only focuses on `sw_sub`
  ggplot() +
    geom_stars(data = sw_sub) +
    annotation_fields(data = seawatervelocity,
                      aes(angle = as.numeric(atan2(vo, uo)),
                          radius = as.numeric(pythagoras(uo, vo)))) +
    labs(radius = "v [m/s]")
}

[Package ggfields version 0.0.6 Index]