gg_inset: Add inset plot to faceted ggplot

gg_insetR Documentation

Add inset plot to faceted ggplot

Description

Ordinarily ggplot is not setup to add inset plots (annotations) to faceted plots because it wants to add the same inset to every facet. This custom annotation function makes it possible to add an inset to a single facet. Credit goes to baptiste for this solution.

Usage

gg_inset(grob, xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, data)

Arguments

grob

a ggplot object.

xmin, xmax, ymin, ymax

coordinates at which the inset starts and ends. Set to -Inf to always start from far edge.

data

(dataframe) should specify the value of the variable used to facet the original plot (see examples).

Examples

# produce main plot and inset plot
plot_df <- data.frame(x = rnorm(1e3), y = rnorm(1e3), group = 1:2)
plot_main <- ggplot2::ggplot() +
             ggplot2::geom_point(ggplot2::aes(x = x, y = y), data = plot_df) +
             ggplot2::facet_wrap(~group)
plot_inset <- ggplot2::qplot(rnorm(1e3))

# use gg_inset to annotate first panel only
plot_combined <- plot_main + gg_inset(ggplot2::ggplotGrob(plot_inset), data = data.frame(group = 1),
                                      xmin = -Inf, xmax = 1.5, ymin = 0.6, ymax = Inf)

plot_combined

bobverity/bobfunctions2 documentation built on July 4, 2023, 8:55 p.m.