library(ggplot2)
library(grid)
# Combine grobs with each other
wrap_elements(panel = textGrob('Here are some text')) +
wrap_elements(
panel = rectGrob(gp = gpar(fill = 'steelblue')),
full = rectGrob(gp = gpar(fill = 'goldenrod'))
)
# wrapped elements can still get titles etc like ggplots
wrap_elements(panel = textGrob('Here are some text')) +
wrap_elements(
panel = rectGrob(gp = gpar(fill = 'steelblue')),
full = rectGrob(gp = gpar(fill = 'goldenrod'))
) +
ggtitle('Title for the amazing rectangles')
# You can also pass in ggplots or patchworks to e.g. have it fill out the
# panel area
p1 <- ggplot(mtcars) + geom_point(aes(mpg, disp))
p1 + wrap_elements(panel = p1 + ggtitle('Look at me shrink'))
# You can even add base graphics if you pass it as a formula (requires gridGraphics package)
if (requireNamespace("gridGraphics", quietly = TRUE)) {
p1 + wrap_elements(full = ~ plot(mtcars$mpg, mtcars$disp))
# Adding a grob or formula directly is equivalent to placing it in `full`
p1 + ~ plot(mtcars$mpg, mtcars$disp)
}
Run the code above in your browser using DataLab