Learn R Programming

ggpp (version 0.4.3)

geom_grob: 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 and geom_text_s. In most respects they behave as any other ggplot geometry: ther add a layer containing one or more grobs and faceting works as usual.

Usage

geom_grob(
  mapping = NULL,
  data = NULL,
  stat = "identity",
  position = "identity",
  ...,
  nudge_x = 0,
  nudge_y = 0,
  add.segments = TRUE,
  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 aes or aes_. Only needs to be set at the layer level if you are overriding the plot defaults.

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 layer. This can include aesthetics whose values you want to set, not map. See layer for more details.

nudge_x, nudge_y

Horizontal and vertical adjustments to nudge the starting position of each text label. The units for nudge_x and nudge_y are the same as for the data units on the x-axis and y-axis.

add.segments

logical Display connecting segments or arrows between original positions and displaced ones if both are available.

arrow

specification for arrow heads, as created by arrow

na.rm

If FALSE (the default), removes missing values with a warning. If TRUE silently removes missing values.

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.

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.

Value

A plot layer instance.

Details

You can modify the alignment of inset grobs 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").

You can modify the size of inset grobs 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 and geom_text_s the size of the insets remains the same relative to the size of the plotting area irrespective of how the plot is rendered. The aspect ratio of insets is preserved and size is adjusted until the whole inset fits within the viewport.

You can modify inset grob 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"`). There several two special alignments: `"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. It tagged with `_mean` or `_median` the mean or median of the data in the panel along the corresponding axis is used as center.

By default this geom uses position_nudge_center which is backwards compatible with position_nudge but provides additional control on the direction of the nudging. In contrast to position_nudge, position_nudge_center and all other position functions defined in packages 'ggpp' and 'ggrepel' keep the original coordinates thus allowing the plotting of connecting segments and arrows.

This geom works only with tibbles as data, as its expects 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.

In the case of geom_grob_npc(), npcx and npcy aesthetics determine the position of the inset grob. As for text labels, justification is interpreted as indicating the position of the grob with respect to the x and y coordinates in "npc" units, and angle is used to rotate the plot as a whole.

annotate cannot be used with geom = "grob". Use annotate (automatic unless 'ggpp' is not attached) as redefined in 'ggpp' when adding inset grobs as annotations (automatic unless 'ggpp' is not attached).

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

geom_plot, geom_table, annotate, position_nudge_keep, position_nudge_to, position_jitternudge, position_dodgenudge and position_stacknudge.

Other geometries adding layers with insets: geom_plot(), geom_table()

Examples

Run this code
# NOT RUN {
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,
            segment.colour = "red")

# 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)

# }

Run the code above in your browser using DataLab