geom_grob {ggpp} | R Documentation |
Inset graphical objects
Description
geom_grob
and geom_grob_npc
add Grobs as insets to the ggplot
using syntax similar to that of geom_text
,
geom_text_s
and geom_text_npc
.
In most respects they behave as any other ggplot geometry: they add a layer
containing one or more grobs and grouping and faceting works as usual. The
most common use of geom_grob
is to add data labels that are graphical
objects rather than text. geom_grob_npc
is used to add grobs
as annotations to plots, but contrary to layer function annotate()
,
geom_grob_npc
is data driven and respects grouping and facets,
thus plot insets can differ among panels. Of these two geoms only
geom_grob
supports the plotting of segments, as
geom_grob_npc
uses a coordinate system that is unrelated
to data units and data.
Usage
geom_grob(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
nudge_x = 0,
nudge_y = 0,
default.colour = "black",
default.color = default.colour,
colour.target = "segment",
color.target = colour.target,
default.alpha = 1,
alpha.target = "segment",
add.segments = TRUE,
box.padding = 0.25,
point.padding = 1e-06,
segment.linewidth = 0.5,
min.segment.length = 0,
arrow = NULL,
na.rm = FALSE,
show.legend = FALSE,
inherit.aes = FALSE
)
geom_grob_npc(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
na.rm = FALSE,
show.legend = FALSE,
inherit.aes = FALSE
)
Arguments
mapping |
The aesthetic mapping, usually constructed with
|
data |
A layer specific dataset - only needed if you want to override the plot defaults. |
stat |
The statistical transformation to use on the data for this layer, as a string. |
position |
Position adjustment, either as a string, or the result of a call to a position adjustment function. |
... |
other arguments passed on to |
nudge_x , nudge_y |
Horizontal and vertical adjustments to nudge the
starting position of each text label. The units for |
default.colour , default.color |
A colour definition to use for elements not targeted by the colour aesthetic. |
colour.target , color.target |
A vector of character strings; |
default.alpha |
numeric in [0..1] A transparency value to use for elements not targeted by the alpha aesthetic. |
alpha.target |
A vector of character strings; |
add.segments |
logical Display connecting segments or arrows between original positions and displaced ones if both are available. |
box.padding , point.padding |
numeric By how much each end of the segments should shortened in mm. |
segment.linewidth |
numeric Width of the segments or arrows in mm. |
min.segment.length |
numeric Segments shorter that the minimum length are not rendered, in mm. |
arrow |
specification for arrow heads, as created by
|
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
Details
You can modify the size of insets with the vp.width
and
vp.height
aesthetics. These can take a number between 0 (smallest
possible inset) and 1 (whole plotting area width or height). The default
value for for both of these aesthetics is 1/5. Thus, in contrast to
geom_text
, geom_label
,
geom_text_s
and geom_label_s
the size of the
insets remains the same relative to the size of the plotting area
irrespective of the size the plot is rendered at. The aspect ratio of
insets is preserved and size is adjusted until the whole inset fits within
the viewport.
By default geom_grob
uses position_nudge_center
and
justification "position"
, while geom_grob_npc
uses
position_nudge
and justification "inward"
. In
contrast to position_nudge
,
position_nudge_center
and all other position functions
defined in packages 'ggpp' keep the original coordinates thus allowing the
plotting of connecting segments and arrows.
This geom_grob
and geom_grob_npc
require the use tibbles as
argument for data
, as the grobs should be stored as a list of
graphics objects ("grob") to be mapped to the label
aesthetic.
The x
and y
aesthetics determine the position of the whole
inset grob, similarly to that of a text label, justification is interpreted
as indicating the position of the grob with respect to its x and
y coordinates in the data, and angle
is used to rotate the
grob as a whole.
Value
A plot layer instance.
Plot boundaries and clipping
The "width" and "height" of an inset as for a text element are 0, so stacking and dodging inset plots will not work by default, and axis limits are not automatically expanded to include all inset plots. Obviously, insets do have height and width, but they are physical units, not data units. The amount of space they occupy on the main plot is not constant in data units of the base plot: when you modify scale limits, inset plots stay the same size relative to the physical size of the base plot.
Alignment
You can modify text alignment with the vjust
and
hjust
aesthetics. These can either be a number between 0
(right/bottom) and 1 (top/left) or a character ("left"
,
"middle"
, "right"
, "bottom"
, "center"
,
"top"
). In addition, you can use special alignments for
justification including "position"
, "inward"
and
"outward"
. Inward always aligns text towards the center of the
plotting area, and outward aligns it away from the center of the plotting
area. If tagged with _mean
or _median
(e.g.,
"outward_mean"
) the mean or median of the data in the panel along
the corresponding axis is used as center. If the characters following the
underscore represent a number (e.g., "outward_10.5"
) the reference
point will be this value in data units. Position justification is computed
based on the direction of the displacement of the position of the label so
that each individual text or label is justified outwards from its original
position. The default justification is "position"
.
If no position displacement is applied, or a position function defined in
'ggplot2' is used, these geometries behave similarly to the corresponding
ones from package 'ggplot2' with a default justification of 0.5
and
no segment drawn.
Position functions
Many layer functions from package 'ggpp' are
designed to work seamlessly with position functions that keep, rather than
discard, the original x
and y
positions in data
when
computing a new displaced position. See position_nudge_keep
,
position_dodge_keep
, position_jitter_keep
,
position_nudge_center
, position_nudge_line
,
position_nudge_to
, position_dodgenudge
,
position_jitternudge
, and position_stacknudge
for examples and details of their use.
Note
The insets are stored nested within the main ggplot object and contain their own copy of the data, and are rendered as grid grobs as normal ggplots at the time the main ggplot is rendered. They can have different themes.
Use annotate
as redefined in 'ggpp' when adding insets
as annotations (automatically available unless 'ggpp' is not attached).
annotate
cannot be used with the npcx
and
npcy
pseudo-aesthetics.
References
The idea of implementing a geom_custom()
for grobs has
been discussed as an issue at
https://github.com/tidyverse/ggplot2/issues/1399.
See Also
grid-package
, geom_text
,
and other documentation of package 'ggplot2'.
Examples
library(tibble)
df <- tibble(x = 2, y = 15, grob = list(grid::circleGrob(r = 0.2)))
# without nudging no segments are drawn
ggplot(data = mtcars,
aes(wt, mpg)) +
geom_point(aes(colour = factor(cyl))) +
geom_grob(data = df,
aes(x, y, label = grob))
# with nudging segments are drawn
ggplot(data = mtcars,
aes(wt, mpg)) +
geom_point(aes(colour = factor(cyl))) +
geom_grob(data = df,
aes(x, y, label = grob),
nudge_x = 0.5,
colour = "red",
hjust = 0.5,
vjust = 0.5)
ggplot(data = mtcars,
aes(wt, mpg)) +
geom_point(aes(colour = factor(cyl))) +
geom_grob(data = df,
aes(x, y, label = grob),
nudge_x = 0.5,
colour = "red",
colour.target = "none",
hjust = 0.5,
vjust = 0.5)
# with nudging plotting of segments can be disabled
ggplot(data = mtcars,
aes(wt, mpg)) +
geom_point(aes(colour = factor(cyl))) +
geom_grob(data = df,
aes(x, y, label = grob),
add.segments = FALSE,
nudge_x = 0.5,
hjust = 0.5,
vjust = 0.5)