gg_inset | R Documentation |
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.
gg_inset(grob, xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, data)
grob |
a ggplot object. |
xmin, xmax, ymin, ymax |
coordinates at which the inset starts and ends.
Set to |
data |
(dataframe) should specify the value of the variable used to facet the original plot (see 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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.