Title: | Arrows for 'ggplot2' |
---|---|
Description: | A 'ggplot2' extension that adds specialised arrow geometry layers. It offers more arrow options than the standard 'grid' arrows that are built-in many line-based geom layers. |
Authors: | Teun van den Brand [aut, cre] |
Maintainer: | Teun van den Brand <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.0.9000 |
Built: | 2024-11-03 04:35:53 UTC |
Source: | https://github.com/teunbrand/ggarrow |
This function mirrors annotate()
with the following
changes. First, the geom
argument is pre-populated with "arrow"
. Second,
several parameters from ggarrow are special-cased, because no warning needs
to be issued when they don't have length 1.
annotate_arrow( geom = "arrow", x = NULL, y = NULL, xmin = NULL, xmax = NULL, ymin = NULL, ymax = NULL, xend = NULL, yend = NULL, ..., na.rm = FALSE )
annotate_arrow( geom = "arrow", x = NULL, y = NULL, xmin = NULL, xmax = NULL, ymin = NULL, ymax = NULL, xend = NULL, yend = NULL, ..., na.rm = FALSE )
geom |
name of geom to use for annotation |
x , y , xmin , ymin , xmax , ymax , xend , yend
|
Positioning aesthetics. At least one of these must be specified. |
... |
Other arguments passed on to
|
na.rm |
If |
A <Layer>
ggproto object that can be added to a plot.
Other arrow geoms:
geom_arrow()
,
geom_arrow_chain()
,
geom_arrow_curve()
,
geom_arrow_segment()
# Annotate an arrow ggplot() + annotate_arrow( x = c(0, 1), y = c(0, 1), arrow_head = arrow_head_line(), arrow_fins = arrow_fins_line(), length_head = unit(5, "mm"), length_fins = unit(5, "mm") ) # Still works with other geoms as well ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + annotate_arrow("text", x = 4, y = 25, label = "Some text")
# Annotate an arrow ggplot() + annotate_arrow( x = c(0, 1), y = c(0, 1), arrow_head = arrow_head_line(), arrow_fins = arrow_fins_line(), length_head = unit(5, "mm"), length_fins = unit(5, "mm") ) # Still works with other geoms as well ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + annotate_arrow("text", x = 4, y = 25, label = "Some text")
There are two types of arrow ornament functions: functions for arrow heads, and functions for arrow fins. The heads and fins can be used interchangeably, but the name makes it clearer what is suitable.
arrow_head_wings(offset = 20, inset = 30) arrow_fins_feather(indent = 0.3, outdent = indent, height = 0.5) arrow_head_line(angle = 30, lineend = "butt") arrow_fins_line(angle = 30, lineend = "butt") arrow_cup(lineend = "round", angle = NULL) arrow_head_minimal(angle = 45) arrow_fins_minimal(angle = 45)
arrow_head_wings(offset = 20, inset = 30) arrow_fins_feather(indent = 0.3, outdent = indent, height = 0.5) arrow_head_line(angle = 30, lineend = "butt") arrow_fins_line(angle = 30, lineend = "butt") arrow_cup(lineend = "round", angle = NULL) arrow_head_minimal(angle = 45) arrow_fins_minimal(angle = 45)
offset , angle
|
A |
inset |
A |
indent , outdent
|
A |
height |
A |
lineend |
A |
The convention for these functions is that the arrow shaft is fused to the ornament at the (0,0) point and the ornaments ends at the (1,0) point.
A <matrix[n, 2]>
with x
and y
columns describing a polygon.
It has a notch_angle
attribute that is used fusing the fins/head to the
shaft of the arrow. They can be given to an arrow plotting function.
arrow_head_wings()
: Places two triangles at either side of the line. Let ABC be a triangle,
where A is at the end of the line, B is on the line and C is the arrow
wingtip. Then offset
is the angle at corner A and inset
is the angle at
corner C.
arrow_fins_feather()
: Places trapezoids at either side of the line. Let ABCD be a quadrilateral
shape, where A is at the end of the line, B is on the line, and CD is
parallel to AB, but offset from the line. Then, indent
is the distance
along the line between A and D and outdent
is the distance along the line
between B and C.
arrow_head_line()
: A line as an arrow head.
arrow_fins_line()
: A line as an arrow fin.
arrow_cup()
: A curved line some fixed distance away from the point to be resected,
resembling a 'cup' shape.
arrow_head_minimal()
: This is a 'fake' arrow head who in practice doesn't draw anything, but
sets the notch_angle
attribute such that the arrow shaft is whittled into
a triangular point.
arrow_fins_minimal()
: This is a 'fake' arrow head who in practise doesn't draw anything, but
sets the notch_angle
attribute such that a triangle is taken out of the
arrow shaft.
# Plotting winged head plot(c(-0.5, 1), c(-0.6, 0.6), type = "n") polygon(arrow_head_wings(), col = "gray") # Plotting feather fins plot(c(0, 1), c(-0.25, 0.25), type = "n") polygon(arrow_fins_feather(), col = "gray")
# Plotting winged head plot(c(-0.5, 1), c(-0.6, 0.6), type = "n") polygon(arrow_head_wings(), col = "gray") # Plotting feather fins plot(c(0, 1), c(-0.25, 0.25), type = "n") polygon(arrow_fins_feather(), col = "gray")
These scales can map continuous input to an argument of an arrow generator. The arrow head, arrow fins and middle arrows have separate scales and by default use different generators.
scale_arrow_head_continuous( name = waiver(), breaks = waiver(), labels = waiver(), limits = NULL, generator = arrow_head_wings, map_arg = "offset", other_args = list(), range = c(10, 80), transform = "identity", guide = "legend" ) scale_arrow_fins_continuous( name = waiver(), breaks = waiver(), labels = waiver(), limits = NULL, generator = arrow_fins_feather, map_arg = "indent", other_args = list(), range = c(0, 1), transform = "identity", guide = "legend" ) scale_arrow_mid_continuous( name = waiver(), breaks = waiver(), labels = waiver(), limits = NULL, generator = arrow_head_wings, map_arg = "offset", other_args = list(), range = c(10, 80), transform = "identity", guide = "legend" )
scale_arrow_head_continuous( name = waiver(), breaks = waiver(), labels = waiver(), limits = NULL, generator = arrow_head_wings, map_arg = "offset", other_args = list(), range = c(10, 80), transform = "identity", guide = "legend" ) scale_arrow_fins_continuous( name = waiver(), breaks = waiver(), labels = waiver(), limits = NULL, generator = arrow_fins_feather, map_arg = "indent", other_args = list(), range = c(0, 1), transform = "identity", guide = "legend" ) scale_arrow_mid_continuous( name = waiver(), breaks = waiver(), labels = waiver(), limits = NULL, generator = arrow_head_wings, map_arg = "offset", other_args = list(), range = c(10, 80), transform = "identity", guide = "legend" )
name |
The name of the scale. Used as the axis or legend title. If
|
breaks |
One of:
|
labels |
One of:
|
limits |
One of:
|
generator |
A |
map_arg |
An argument of the |
other_args |
Additional, fixed, arguments to pass to the |
range |
The range that |
transform |
For continuous scales, the name of a transformation object or the object itself. Built-in transformations include "asn", "atanh", "boxcox", "date", "exp", "hms", "identity", "log", "log10", "log1p", "log2", "logit", "modulus", "probability", "probit", "pseudo_log", "reciprocal", "reverse", "sqrt" and "time". A transformation object bundles together a transform, its inverse,
and methods for generating breaks and labels. Transformation objects
are defined in the scales package, and are called |
guide |
A function used to create a guide or its name. See
|
A <Scale>
that can be added to a plot.
base <- ggplot(whirlpool(5), aes(x, y, colour = group)) + coord_fixed() p <- base + geom_arrow( aes(arrow_head = as.integer(group)), length_head = 10 ) # A typical scale p + scale_arrow_head_continuous() # Change other arguments passed to the generator p + scale_arrow_head_continuous(other_args = list(inset = 90)) # Using another argument of the generator p + scale_arrow_head_continuous(name = "inset", map_arg = "inset") # Using a different generator p + scale_arrow_head_continuous( generator = arrow_head_line, map_arg = "angle", range = c(20, 80) ) # The same goes for other arrow aesthetics, but the `generator()` might # differ. base + geom_arrow( aes(arrow_fins = as.integer(group), arrow_mid = as.integer(group)), length_fins = 10, arrow_head = NULL ) + scale_arrow_fins_continuous(map_arg = "height", range = c(0.1, 1)) + scale_arrow_mid_continuous(map_arg = "inset")
base <- ggplot(whirlpool(5), aes(x, y, colour = group)) + coord_fixed() p <- base + geom_arrow( aes(arrow_head = as.integer(group)), length_head = 10 ) # A typical scale p + scale_arrow_head_continuous() # Change other arguments passed to the generator p + scale_arrow_head_continuous(other_args = list(inset = 90)) # Using another argument of the generator p + scale_arrow_head_continuous(name = "inset", map_arg = "inset") # Using a different generator p + scale_arrow_head_continuous( generator = arrow_head_line, map_arg = "angle", range = c(20, 80) ) # The same goes for other arrow aesthetics, but the `generator()` might # differ. base + geom_arrow( aes(arrow_fins = as.integer(group), arrow_mid = as.integer(group)), length_fins = 10, arrow_head = NULL ) + scale_arrow_fins_continuous(map_arg = "height", range = c(0.1, 1)) + scale_arrow_mid_continuous(map_arg = "inset")
These scales can map discrete input to various sorts of arrow shapes. The arrow head, arrow fins and middle arrows have separate scales.
scale_arrow_head_discrete(values = NULL, aesthetics = "arrow_head", ...) scale_arrow_fins_discrete(values = NULL, aesthetics = "arrow_fins", ...) scale_arrow_mid_discrete(values = NULL, aesthetics = "arrow_mid", ...)
scale_arrow_head_discrete(values = NULL, aesthetics = "arrow_head", ...) scale_arrow_fins_discrete(values = NULL, aesthetics = "arrow_fins", ...) scale_arrow_mid_discrete(values = NULL, aesthetics = "arrow_mid", ...)
values |
One of the following:
|
aesthetics |
The names of the aesthetics that this scale works with |
... |
Arguments passed on to
|
A <Scale>
that can be added to a plot.
# A standard arrow plot p <- ggplot(whirlpool(5), aes(x, y, colour = group)) + geom_arrow(length_head = 10, length_fins = 10, arrow_head = NULL) # A character vector naming arrow shapes as arrow head scale p + aes(arrow_head = group) + scale_arrow_head_discrete(values = c( "head_wings", "head_line", "head_minimal", "fins_line", "fins_feather" )) # A mixed list with arrows as arrow fins scale p + aes(arrow_fins = group) + scale_arrow_fins_discrete(values = list( "head_wings", # Using a character arrow_head_wings(20, 100), # Using an arrow function NULL, # No arrow matrix(c(1, 0, 0, 0, 0.5, -0.5), ncol = 2), # A matrix "fins_feather" ))
# A standard arrow plot p <- ggplot(whirlpool(5), aes(x, y, colour = group)) + geom_arrow(length_head = 10, length_fins = 10, arrow_head = NULL) # A character vector naming arrow shapes as arrow head scale p + aes(arrow_head = group) + scale_arrow_head_discrete(values = c( "head_wings", "head_line", "head_minimal", "fins_line", "fins_feather" )) # A mixed list with arrows as arrow fins scale p + aes(arrow_fins = group) + scale_arrow_fins_discrete(values = list( "head_wings", # Using a character arrow_head_wings(20, 100), # Using an arrow function NULL, # No arrow matrix(c(1, 0, 0, 0, 0.5, -0.5), ncol = 2), # A matrix "fins_feather" ))
Like any legend key glyphs, this key can be used to display arrows in a legend.
draw_key_arrow(data, params, size)
draw_key_arrow(data, params, size)
data |
A single row data frame containing the scaled aesthetics to display in this key |
params |
A list of additional parameters supplied to the geom. |
size |
Width and height of key in mm. |
An <arrow_path>
grob
ggplot(mpg, aes(displ, colour = factor(cyl))) + geom_density(key_glyph = draw_key_arrow)
ggplot(mpg, aes(displ, colour = factor(cyl))) + geom_density(key_glyph = draw_key_arrow)
Using the theme system, draws arrows in places where
element_line()
are valid theme elements. Note
that the default use of element_arrow()
does not actually draw an arrow
unless one of the arrow_
arguments is set.
element_arrow( colour = NULL, linewidth = NULL, linewidth_head = NULL, linewidth_fins = NULL, stroke_colour = NULL, stroke_width = NULL, arrow_head = NULL, arrow_fins = NULL, arrow_mid = NULL, length = NULL, length_head = NULL, length_fins = NULL, length_mid = NULL, resect = NULL, resect_head = NULL, resect_fins = NULL, justify = NULL, force_arrow = NULL, mid_place = NULL, lineend = NULL, linejoin = NULL, linemitre = NULL, inherit.blank = FALSE )
element_arrow( colour = NULL, linewidth = NULL, linewidth_head = NULL, linewidth_fins = NULL, stroke_colour = NULL, stroke_width = NULL, arrow_head = NULL, arrow_fins = NULL, arrow_mid = NULL, length = NULL, length_head = NULL, length_fins = NULL, length_mid = NULL, resect = NULL, resect_head = NULL, resect_fins = NULL, justify = NULL, force_arrow = NULL, mid_place = NULL, lineend = NULL, linejoin = NULL, linemitre = NULL, inherit.blank = FALSE )
colour |
The colour of the arrow. |
linewidth , linewidth_head , linewidth_fins
|
The width of the arrow shaft
in millimetres. |
stroke_colour |
The colour of the arrow outline. |
stroke_width |
The width of the arrow outlien. |
arrow_head , arrow_fins , arrow_mid
|
Arrow
ornament shapes for the arrow head, arrow fins and
middle arrows respectively. Can be one of the following:
* |
length , length_head , length_fins , length_mid
|
Determines the size of
the arrow ornaments.
|
resect , resect_head , resect_fins
|
A |
justify |
A |
force_arrow |
A |
mid_place |
Sets the location of middle (interior) ornaments when
|
lineend |
A |
linejoin |
A |
linemitre |
A |
inherit.blank |
A |
An <element_arrow>
object that can replace <element_line>
objects
in theme()
.
# Setting a bunch of arrows all over the theme ggplot(whirlpool(5), aes(x, y, group = group)) + geom_path() + theme( # Proper arrow with variable width for x-axis line axis.line.x = element_arrow( arrow_head = "head_wings", linewidth_head = 2, linewidth_fins = 0 ), # Just a variable width line for the y-axis line axis.line.y = element_arrow(linewidth_head = 0, linewidth_fins = 5, lineend = "round"), # Arrows for the y-axis ticks axis.ticks.y = element_arrow(arrow_fins = arrow_head_line(angle = 45)), # Variable width lines for the x-axis ticks axis.ticks.x = element_arrow(linewidth_head = 3, linewidth_fins = 0), axis.ticks.length = unit(0.5, 'cm'), # Arrows for major panel grid panel.grid.major = element_arrow( arrow_head = "head_wings", arrow_fins = "fins_feather", length = 10 ), # Shortened lines for the minor panel grid panel.grid.minor = element_arrow(resect = 20) )
# Setting a bunch of arrows all over the theme ggplot(whirlpool(5), aes(x, y, group = group)) + geom_path() + theme( # Proper arrow with variable width for x-axis line axis.line.x = element_arrow( arrow_head = "head_wings", linewidth_head = 2, linewidth_fins = 0 ), # Just a variable width line for the y-axis line axis.line.y = element_arrow(linewidth_head = 0, linewidth_fins = 5, lineend = "round"), # Arrows for the y-axis ticks axis.ticks.y = element_arrow(arrow_fins = arrow_head_line(angle = 45)), # Variable width lines for the x-axis ticks axis.ticks.x = element_arrow(linewidth_head = 3, linewidth_fins = 0), axis.ticks.length = unit(0.5, 'cm'), # Arrows for major panel grid panel.grid.major = element_arrow( arrow_head = "head_wings", arrow_fins = "fins_feather", length = 10 ), # Shortened lines for the minor panel grid panel.grid.minor = element_arrow(resect = 20) )
This arrow geom can be used to draw lines adorned with arrow heads or fins. It is useful as an annotation layer to point to or away from other things on the plot. An arrow typically consists of three parts: the arrowhead, the shaft and fins. This geom places arrow heads at the end of a line and fins at the beginning of a line.
geom_arrow( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., arrow_head = arrow_head_wings(), arrow_fins = NULL, arrow_mid = NULL, length = 4, length_head = NULL, length_fins = NULL, length_mid = NULL, justify = 0, force_arrow = FALSE, mid_place = 0.5, resect = 0, resect_head = NULL, resect_fins = NULL, lineend = "butt", linejoin = "round", linemitre = 10, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
geom_arrow( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., arrow_head = arrow_head_wings(), arrow_fins = NULL, arrow_mid = NULL, length = 4, length_head = NULL, length_fins = NULL, length_mid = NULL, justify = 0, force_arrow = FALSE, mid_place = 0.5, resect = 0, resect_head = NULL, resect_fins = NULL, lineend = "butt", linejoin = "round", linemitre = 10, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this layer.
When using a
|
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
|
... |
Other arguments passed on to
|
arrow_head , arrow_fins , arrow_mid
|
A function call to one of the arrow ornament functions that can determine the shape of the arrow head, fins or middle (interior) arrows. |
length , length_head , length_fins , length_mid
|
Determines the size of
the arrow ornaments.
|
justify |
A |
force_arrow |
A |
mid_place |
Sets the location of middle (interior) arrows, when applicable. Can be one of the following:
|
resect , resect_head , resect_fins
|
A |
lineend |
Line end style (round, butt, square). |
linejoin |
Line join style (round, mitre, bevel). |
linemitre |
Line mitre limit (number greater than 1). |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
A <Layer>
ggproto object that can be added to a plot.
geom_arrow()
understands the following aesthetics (required aesthetics are in bold):
arrow_fins
arrow_head
arrow_mid
resect_fins
resect_head
stroke_colour
stroke_width
Learn more about setting these aesthetics in vignette("ggplot2-specs")
.
Other arrow geoms:
annotate_arrow()
,
geom_arrow_chain()
,
geom_arrow_curve()
,
geom_arrow_segment()
# Setting up a plot p <- ggplot(whirlpool(), aes(x, y, colour = group)) + coord_equal() # A standard arrow p + geom_arrow() # Arrows can have varying linewidths p + geom_arrow(aes(linewidth = arc)) # You can use `length_head` to decouple arrow-head size from linewidth p + geom_arrow(aes(linewidth = arc), length_head = unit(10, "mm")) # The arrow head shape can be controlled with the `arrow_head` argument p + geom_arrow(arrow_head = arrow_head_line(), length_head = unit(10, "mm")) # This works similarly for the arrow fins p + geom_arrow( arrow_fins = arrow_fins_feather(), length_fins = unit(7, "mm") )
# Setting up a plot p <- ggplot(whirlpool(), aes(x, y, colour = group)) + coord_equal() # A standard arrow p + geom_arrow() # Arrows can have varying linewidths p + geom_arrow(aes(linewidth = arc)) # You can use `length_head` to decouple arrow-head size from linewidth p + geom_arrow(aes(linewidth = arc), length_head = unit(10, "mm")) # The arrow head shape can be controlled with the `arrow_head` argument p + geom_arrow(arrow_head = arrow_head_line(), length_head = unit(10, "mm")) # This works similarly for the arrow fins p + geom_arrow( arrow_fins = arrow_fins_feather(), length_fins = unit(7, "mm") )
An arrow chains connects a set of coordinates with a sequence of arrows. The
geom_arrow_chain()
function can be useful to connect observations in a
directed manner.
geom_arrow_chain( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., arrow_head = arrow_head_wings(), arrow_fins = NULL, arrow_mid = NULL, length = 4, length_head = NULL, length_fins = NULL, length_mid = NULL, justify = 0, force_arrow = FALSE, mid_place = 0.5, resect = 1, resect_head = NULL, resect_fins = NULL, lineend = "butt", linejoin = "round", linemitre = 10, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
geom_arrow_chain( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., arrow_head = arrow_head_wings(), arrow_fins = NULL, arrow_mid = NULL, length = 4, length_head = NULL, length_fins = NULL, length_mid = NULL, justify = 0, force_arrow = FALSE, mid_place = 0.5, resect = 1, resect_head = NULL, resect_fins = NULL, lineend = "butt", linejoin = "round", linemitre = 10, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this layer.
When using a
|
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
|
... |
Other arguments passed on to
|
arrow_head , arrow_fins , arrow_mid
|
A function call to one of the arrow ornament functions that can determine the shape of the arrow head, fins or middle (interior) arrows. |
length , length_head , length_fins , length_mid
|
Determines the size of
the arrow ornaments.
|
justify |
A |
force_arrow |
A |
mid_place |
Sets the location of middle (interior) arrows, when applicable. Can be one of the following:
|
resect , resect_head , resect_fins
|
A |
lineend |
Line end style (round, butt, square). |
linejoin |
Line join style (round, mitre, bevel). |
linemitre |
Line mitre limit (number greater than 1). |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
A <Layer>
ggproto object that can be added to a plot.
geom_arrow_segment()
understands the following aesthetics (required aesthetics are in bold):
arrow_fins
arrow_head
arrow_mid
linewidth_fins
linewidth_head
resect_fins
resect_head
stroke_colour
stroke_width
The linewidth_fins
and linewidth_head
inherit from linewidth
. They can be used to seperately control the start- and end-width.
Learn more about setting these aesthetics in vignette("ggplot2-specs")
.
Other arrow geoms:
annotate_arrow()
,
geom_arrow()
,
geom_arrow_curve()
,
geom_arrow_segment()
# Setup dummy data t <- seq(0, 2 * pi, length.out = 11) l <- rep(c(1, 0.4), length.out = 11) df <- data.frame( x = cos(t) * l, y = sin(t) * l, size = l + 0.4 ) p <- ggplot(df, aes(x, y, size = size)) + geom_point(colour = 2) + coord_equal() # An arrow chains adapts to the `size` aesthetic to go nicely with points p + geom_arrow_chain() # Without arrowhead, it is similar to a `type = 'b'` base R plot p + geom_arrow_chain(arrow_head = NULL) # To widen the gap, one can increase the `resect` parameter p + geom_arrow_chain(resect = 5) # To ignore the points, set `resect` and `size` to 0 p + geom_arrow_chain(size = 0, resect = 0) # Linewidths will be interpolated across arrows p + geom_arrow_chain(aes(linewidth = seq_along(x))) # Alternatively, we can set them seperately for starts and ends p + geom_arrow_chain(linewidth_fins = 0, linewidth_head = 3)
# Setup dummy data t <- seq(0, 2 * pi, length.out = 11) l <- rep(c(1, 0.4), length.out = 11) df <- data.frame( x = cos(t) * l, y = sin(t) * l, size = l + 0.4 ) p <- ggplot(df, aes(x, y, size = size)) + geom_point(colour = 2) + coord_equal() # An arrow chains adapts to the `size` aesthetic to go nicely with points p + geom_arrow_chain() # Without arrowhead, it is similar to a `type = 'b'` base R plot p + geom_arrow_chain(arrow_head = NULL) # To widen the gap, one can increase the `resect` parameter p + geom_arrow_chain(resect = 5) # To ignore the points, set `resect` and `size` to 0 p + geom_arrow_chain(size = 0, resect = 0) # Linewidths will be interpolated across arrows p + geom_arrow_chain(aes(linewidth = seq_along(x))) # Alternatively, we can set them seperately for starts and ends p + geom_arrow_chain(linewidth_fins = 0, linewidth_head = 3)
This arrow geom can be used to draw curves from one point to oneanother with arrow heads or fins.
geom_arrow_curve( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., curvature = 0.5, angle = 90, ncp = 5, arrow_head = arrow_head_wings(), arrow_fins = NULL, arrow_mid = NULL, length = 4, length_head = NULL, length_fins = NULL, length_mid = NULL, justify = 0, force_arrow = FALSE, mid_place = 0.5, resect = 0, resect_head = NULL, resect_fins = NULL, lineend = "butt", linejoin = "round", linemitre = 10, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
geom_arrow_curve( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., curvature = 0.5, angle = 90, ncp = 5, arrow_head = arrow_head_wings(), arrow_fins = NULL, arrow_mid = NULL, length = 4, length_head = NULL, length_fins = NULL, length_mid = NULL, justify = 0, force_arrow = FALSE, mid_place = 0.5, resect = 0, resect_head = NULL, resect_fins = NULL, lineend = "butt", linejoin = "round", linemitre = 10, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this layer.
When using a
|
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
|
... |
Other arguments passed on to
|
curvature |
A numeric value giving the amount of curvature. Negative values produce left-hand curves, positive values produce right-hand curves, and zero produces a straight line. |
angle |
A numeric value between 0 and 180, giving an amount to skew the control points of the curve. Values less than 90 skew the curve towards the start point and values greater than 90 skew the curve towards the end point. |
ncp |
The number of control points used to draw the curve. More control points creates a smoother curve. |
arrow_head , arrow_fins , arrow_mid
|
A function call to one of the arrow ornament functions that can determine the shape of the arrow head, fins or middle (interior) arrows. |
length , length_head , length_fins , length_mid
|
Determines the size of
the arrow ornaments.
|
justify |
A |
force_arrow |
A |
mid_place |
Sets the location of middle (interior) arrows, when applicable. Can be one of the following:
|
resect , resect_head , resect_fins
|
A |
lineend |
Line end style (round, butt, square). |
linejoin |
Line join style (round, mitre, bevel). |
linemitre |
Line mitre limit (number greater than 1). |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
A <Layer>
ggproto object that can be added to a plot.
geom_arrow()
understands the following aesthetics (required aesthetics are in bold):
arrow_fins
arrow_head
arrow_mid
resect_fins
resect_head
stroke_colour
stroke_width
Learn more about setting these aesthetics in vignette("ggplot2-specs")
.
Other arrow geoms:
annotate_arrow()
,
geom_arrow()
,
geom_arrow_chain()
,
geom_arrow_segment()
curve_data <- data.frame( x1 = c(2.62, 1.835), x2 = c(3.57, 5.250), y1 = c(21.0, 33.9), y2 = c(15.0, 10.4), group = c("A", "B") ) ggplot(mtcars, aes(wt, mpg)) + geom_point() + geom_arrow_curve( aes(x = x1, y = y1, xend = x2, yend = y2, colour = group, arrow_head = group), data = curve_data, curvature = -0.2, length_head = 10 )
curve_data <- data.frame( x1 = c(2.62, 1.835), x2 = c(3.57, 5.250), y1 = c(21.0, 33.9), y2 = c(15.0, 10.4), group = c("A", "B") ) ggplot(mtcars, aes(wt, mpg)) + geom_point() + geom_arrow_curve( aes(x = x1, y = y1, xend = x2, yend = y2, colour = group, arrow_head = group), data = curve_data, curvature = -0.2, length_head = 10 )
geom_arrow_segment()
draws a straight arrow between points (x, y) and
(xend, yend). In contrast to geom_segment()
, the xend
and yend
aesthetics default to x
and y
respectively, so only one of xend
and
yend
is required.
geom_arrow_segment( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., arrow_head = arrow_head_wings(), arrow_fins = NULL, arrow_mid = NULL, length = 4, length_head = NULL, length_fins = NULL, length_mid = NULL, justify = 0, force_arrow = FALSE, mid_place = 0.5, resect = 0, resect_head = NULL, resect_fins = NULL, lineend = "butt", linejoin = "round", linemitre = 10, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
geom_arrow_segment( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., arrow_head = arrow_head_wings(), arrow_fins = NULL, arrow_mid = NULL, length = 4, length_head = NULL, length_fins = NULL, length_mid = NULL, justify = 0, force_arrow = FALSE, mid_place = 0.5, resect = 0, resect_head = NULL, resect_fins = NULL, lineend = "butt", linejoin = "round", linemitre = 10, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this layer.
When using a
|
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
|
... |
Other arguments passed on to
|
arrow_head , arrow_fins , arrow_mid
|
A function call to one of the arrow ornament functions that can determine the shape of the arrow head, fins or middle (interior) arrows. |
length , length_head , length_fins , length_mid
|
Determines the size of
the arrow ornaments.
|
justify |
A |
force_arrow |
A |
mid_place |
Sets the location of middle (interior) arrows, when applicable. Can be one of the following:
|
resect , resect_head , resect_fins
|
A |
lineend |
Line end style (round, butt, square). |
linejoin |
Line join style (round, mitre, bevel). |
linemitre |
Line mitre limit (number greater than 1). |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
A <Layer>
ggproto object that can be added to a plot.
geom_arrow_segment()
understands the following aesthetics (required aesthetics are in bold):
arrow_fins
arrow_head
arrow_mid
linewidth_fins
linewidth_head
resect_fins
resect_head
stroke_colour
stroke_width
The linewidth_fins
and linewidth_head
inherit from linewidth
. They can be used to seperately control the start- and end-width.
Learn more about setting these aesthetics in vignette("ggplot2-specs")
.
Other arrow geoms:
annotate_arrow()
,
geom_arrow()
,
geom_arrow_chain()
,
geom_arrow_curve()
# Setup dummy data set.seed(42) df <- data.frame( x = LETTERS[1:6], y = 6:1 + rnorm(6) ) # We can omit either `xend` or `yend` for this segment geom p <- ggplot(df, aes(x, y = 0, yend = y, colour = x)) p + geom_arrow_segment() # We can set the linewidth globally p + geom_arrow_segment(aes(linewidth = y)) # Or seperately for the head and fins p + geom_arrow_segment(aes(linewidth_head = y, linewidth_fins = 0)) # We can also place arrows in the middle p + geom_arrow_segment( arrow_mid = arrow_head_line(), mid_place = c(0.33, 0.66), arrow_head = NULL # Turn off head )
# Setup dummy data set.seed(42) df <- data.frame( x = LETTERS[1:6], y = 6:1 + rnorm(6) ) # We can omit either `xend` or `yend` for this segment geom p <- ggplot(df, aes(x, y = 0, yend = y, colour = x)) p + geom_arrow_segment() # We can set the linewidth globally p + geom_arrow_segment(aes(linewidth = y)) # Or seperately for the head and fins p + geom_arrow_segment(aes(linewidth_head = y, linewidth_fins = 0)) # We can also place arrows in the middle p + geom_arrow_segment( arrow_mid = arrow_head_line(), mid_place = c(0.33, 0.66), arrow_head = NULL # Turn off head )
ggarrow relieas on the extension mechanism of ggplot2 through
ggproto class objects, that allow for cross-package
inheritance of geoms. These objects can be ignored by users for the purpose
of making plots, since interacting with these objects is preferred through
various geom_*()
functions.
Creates a graphical object that draws arrows. An arrow typically consists of three parts: the arrowhead, the shaft and fins. Relative to how an arrow is drawn from coordinates, these three parts describe the end, middle and beginning of an arrow line.
grob_arrow( x = unit(c(0, 1), "npc"), y = unit(c(0, 1), "npc"), id = NULL, id.lengths = NULL, arrow_head = arrow_head_wings(), arrow_fins = NULL, arrow_mid = NULL, length_head = unit(5, "mm"), length_fins = NULL, length_mid = NULL, justify = 0, shaft_width = unit(1, "mm"), mid_place = 0.5, resect = unit(0, "mm"), resect_fins = NULL, resect_head = NULL, force_arrow = FALSE, default.units = "mm", name = NULL, gp = gpar(), vp = NULL )
grob_arrow( x = unit(c(0, 1), "npc"), y = unit(c(0, 1), "npc"), id = NULL, id.lengths = NULL, arrow_head = arrow_head_wings(), arrow_fins = NULL, arrow_mid = NULL, length_head = unit(5, "mm"), length_fins = NULL, length_mid = NULL, justify = 0, shaft_width = unit(1, "mm"), mid_place = 0.5, resect = unit(0, "mm"), resect_fins = NULL, resect_head = NULL, force_arrow = FALSE, default.units = "mm", name = NULL, gp = gpar(), vp = NULL )
x |
A numeric vector or unit object specifying x-values. |
y |
A numeric vector or unit object specifying y-values. |
id |
A numeric vector used to separate locations in |
id.lengths |
A numeric vector used to separate locations in |
arrow_head , arrow_fins , arrow_mid
|
A |
length_head , length_fins , length_mid
|
A |
justify |
A |
shaft_width |
A |
mid_place |
Sets the location of middle (interior) arrows, when applicable. Can be one of the following:
|
resect , resect_fins , resect_head
|
A |
force_arrow |
A |
default.units |
A string indicating the default units to use
if |
name |
A character identifier. |
gp |
An object of class |
vp |
A Grid viewport object (or NULL). |
A <arrow_path>
graphical object.
requireNamespace("grid") # Creating an arrow arrow <- grob_arrow( x = unit(c(0.2, 0.5, 0.8, 0.2, 0.5, 0.8), "npc"), y = unit(c(0.2, 0.8, 0.2, 0.8, 0.2, 0.8), "npc"), id.lengths = c(3, 3), arrow_head = arrow_head_wings(), arrow_fins = arrow_fins_feather(), length_fins = 8, shaft_width = 1, gp = grid::gpar(fill = c("dodgerblue", "tomato"), col = "black") ) # Drawing the arrow grid::grid.newpage(); grid::grid.draw(arrow)
requireNamespace("grid") # Creating an arrow arrow <- grob_arrow( x = unit(c(0.2, 0.5, 0.8, 0.2, 0.5, 0.8), "npc"), y = unit(c(0.2, 0.8, 0.2, 0.8, 0.2, 0.8), "npc"), id.lengths = c(3, 3), arrow_head = arrow_head_wings(), arrow_fins = arrow_fins_feather(), length_fins = 8, shaft_width = 1, gp = grid::gpar(fill = c("dodgerblue", "tomato"), col = "black") ) # Drawing the arrow grid::grid.newpage(); grid::grid.draw(arrow)
Creates a graphical object that draws curved arrows.
grob_arrow_curve( x1, y1, x2, y2, default.units = "mm", curvature = 1, angle = 90, ncp = 1, shape = 0.5, square = TRUE, squareShape = 1, inflect = FALSE, open = TRUE, name = NULL, gp = gpar(), vp = NULL, ..., width_head = unit(1, "mm"), width_fins = unit(1, "mm") )
grob_arrow_curve( x1, y1, x2, y2, default.units = "mm", curvature = 1, angle = 90, ncp = 1, shape = 0.5, square = TRUE, squareShape = 1, inflect = FALSE, open = TRUE, name = NULL, gp = gpar(), vp = NULL, ..., width_head = unit(1, "mm"), width_fins = unit(1, "mm") )
x1 |
A numeric vector or unit object specifying the x-location of the start point. |
y1 |
A numeric vector or unit object specifying the y-location of the start point. |
x2 |
A numeric vector or unit object specifying the x-location of the end point. |
y2 |
A numeric vector or unit object specifying the y-location of the end point. |
default.units |
A string indicating the default units to use
if |
curvature |
A numeric value giving the amount of curvature. Negative values produce left-hand curves, positive values produce right-hand curves, and zero produces a straight line. |
angle |
A numeric value between 0 and 180, giving an amount to skew the control points of the curve. Values less than 90 skew the curve towards the start point and values greater than 90 skew the curve towards the end point. |
ncp |
The number of control points used to draw the curve. More control points creates a smoother curve. |
shape |
A numeric vector of values between -1 and 1, which
control the shape of the curve relative to its control points.
See |
square |
A logical value that controls whether control
points for the curve are created city-block fashion or
obliquely. When |
squareShape |
A |
inflect |
A logical value specifying whether the curve should be cut in half and inverted (see Examples below). |
open |
A logical value indicating whether to close the curve (connect the start and end points). |
name |
A character identifier. |
gp |
An object of class |
vp |
A Grid viewport object (or NULL). |
... |
Arguments passed on to
|
width_fins , width_head
|
A |
A <curve_arrow>
graphical object.
requireNamespace("grid") # Creating the curved arrow grob <- grob_arrow_curve( x1 = unit(0.25, "npc"), y1 = unit(0.25, "npc"), x2 = unit(0.75, "npc"), y2 = unit(0.75, "npc"), angle = 90, curvature = 0.5, ncp = 5, arrow_head = arrow_head_line() ) # Drawing the arrow grid::grid.newpage(); grid::grid.draw(grob)
requireNamespace("grid") # Creating the curved arrow grob <- grob_arrow_curve( x1 = unit(0.25, "npc"), y1 = unit(0.25, "npc"), x2 = unit(0.75, "npc"), y2 = unit(0.75, "npc"), angle = 90, curvature = 0.5, ncp = 5, arrow_head = arrow_head_line() ) # Drawing the arrow grid::grid.newpage(); grid::grid.draw(grob)
Arrow geoms have a resect
aesthetic that controls how much an arrow should
be shortened. These scales can help to rescale the output range of resection.
scale_resect_continuous( ..., range = NULL, aesthetics = c("resect_head", "resect_fins"), guide = "none" ) scale_resect_discrete( ..., values = NULL, aesthetics = c("resect_head", "resect_fins"), range = NULL, guide = "none" )
scale_resect_continuous( ..., range = NULL, aesthetics = c("resect_head", "resect_fins"), guide = "none" ) scale_resect_discrete( ..., values = NULL, aesthetics = c("resect_head", "resect_fins"), range = NULL, guide = "none" )
... |
Arguments passed on to
|
range |
A numeric vector of length 2 indicating the minimum and maximum
size of the resection after transformation in millimetres. |
aesthetics |
The names of the aesthetics that this scale works with. |
guide |
A function used to create a guide or its name. See
|
values |
(Discrete scale only) A numeric vector to map data values to.
The values will be matched in order with the limits of the scale, or with
|
Conceptually, these scales depart slightly from ggplot2 conventions. The
scale_resect_continuous()
function returns an identity scale
when range = NULL
(default) and a typical continuous scale when the
range
argument is set.
The scale_resect_discrete()
acts as a manual scale when values
is set
and as an ordinal scale when range
is set.
A <Scale>
that can be added to a plot.
# A plot with points indicating path ends p <- ggplot(whirlpool(5), aes(x, y, colour = group)) + geom_point(data = ~ subset(.x, arc == ave(arc, group, FUN = max))) # Resect scale as an identity scale p + geom_arrow(aes(resect_head = as.integer(group))) + scale_resect_continuous() # Resect scale as typical continuous scale p + geom_arrow(aes(resect_head = as.integer(group))) + scale_resect_continuous(range = c(0, 10)) # Resect scale as manual scale p + geom_arrow(aes(resect_head = group)) + scale_resect_discrete(values = c(10, 5, 0, 5, 10)) # Resect scale as ordinal scale p + geom_arrow(aes(resect_head = group)) + scale_resect_discrete(range = c(0, 10))
# A plot with points indicating path ends p <- ggplot(whirlpool(5), aes(x, y, colour = group)) + geom_point(data = ~ subset(.x, arc == ave(arc, group, FUN = max))) # Resect scale as an identity scale p + geom_arrow(aes(resect_head = as.integer(group))) + scale_resect_continuous() # Resect scale as typical continuous scale p + geom_arrow(aes(resect_head = as.integer(group))) + scale_resect_continuous(range = c(0, 10)) # Resect scale as manual scale p + geom_arrow(aes(resect_head = group)) + scale_resect_discrete(values = c(10, 5, 0, 5, 10)) # Resect scale as ordinal scale p + geom_arrow(aes(resect_head = group)) + scale_resect_discrete(range = c(0, 10))
This function returns some made-up data to test arrow functionality with.
whirlpool(n = 5, detail = 100)
whirlpool(n = 5, detail = 100)
n |
The number of streams in the whirlpool. |
detail |
The number of points per stream. |
A data.frame
whirlpool()
whirlpool()