Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

wrap_ggplot_grob

Make a gtable created from a ggplot object patchwork compliant


Description

This function converts a gtable, as produced by ggplot2::ggplotGrob() and makes it ready to be added to a patchwork. In contrast to passing the gtable to wrap_elements(), wrap_ggplot_grob() ensures proper alignment as expected. On the other hand major restructuring of the gtable will result in an object that doesn't work properly with wrap_ggplot_grob().

Usage

wrap_ggplot_grob(x)

Arguments

x

A gtable as produced by ggplot2::ggplotGrob()

Value

A table_patch object to be added to a patchwork

Examples

library(grid)
library(gtable)
library(ggplot2)

p1 <- ggplot(mtcars) + geom_point(aes(mpg, disp)) + ggtitle('disp and mpg seems connected')
p2 <- ggplot(mtcars) + geom_boxplot(aes(gear, disp, group = gear))

# Convert p2 so we can add new stuff to it
p2_table <- ggplotGrob(p2)
stamp <- textGrob('TOP SECRET', rot = 35,
  gp = gpar(fontsize = 72, fontface = 'bold')
)
p2_table <- gtable_add_grob(p2_table, stamp,
  t = 1, l = 1, b = nrow(p2_table), r = ncol(p2_table)
)

# Adding it directly will loose alignment
p1 + p2_table

# Use wrap_ggplot_grob to keep alignment
p1 + wrap_ggplot_grob(p2_table)

patchwork

The Composer of Plots

v1.1.1
MIT + file LICENSE
Authors
Thomas Lin Pedersen [cre, aut] (<https://orcid.org/0000-0002-5147-4711>)
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.