ggpmisc (version 0.3.2)

geom_grob: Inset graphical objects

Description

geom_grob adds a Grob as inset to the ggplot using syntax similar to that of geom_label.

Usage

geom_grob(mapping = NULL, data = NULL, stat = "identity",
  position = "identity", ..., 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.

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.

Alignment

You can modify table 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").

Inset size

You can modify inset plot size with the vp.width and vp.height aesthetics. These can be a number between 0 (smallest posisble inset) and 1 (whole plotting area width or height). The default value for for both of these aesthetics is 1/3.

Details

Note the "width" and "height" like of a text element are 0, so stacking and dodging Grobs will not work by default, and axis limits are not automatically expanded to include all inset Grobs. Obviously, Grobs do have height and width, but they are in physical units, not data units. The amount of space they occupy on the main plot is constant in data units.

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

Other Statistics for adding insets to ggplots: 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)))
ggplot(data = mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl))) +
  geom_grob(data = df, aes(x, y, label = grob))

# }

Run the code above in your browser using DataLab