geom_grob
and geom_grob_npc
add a Grob as inset to the ggplot
using syntax similar to that of geom_label
.In most
respects they behave as any other ggplot geometry: a layer con contain
multiple tables and faceting works as usual.
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
)
A layer specific dataset - only needed if you want to override the plot defaults.
The statistical transformation to use on the data for this layer, as a string.
Position adjustment, either as a string, or the result of a call to a position adjustment function.
If FALSE
(the default), removes missing values with a
warning. If TRUE
silently removes missing values.
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.
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
.
A plot layer instance.
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").
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.
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.
The idea of implementing a geom_custom()
for grobs has
been discussed as an issue at
https://github.com/tidyverse/ggplot2/issues/1399.
Other geometries adding layers with insets:
geom_plot()
,
geom_table()
# 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