ROIpad ← Back to Search
stackoverflow › answer

Answer to: Modify the gtable of a ggplot2 and end up with a ggplot2 object without loss of information

Score: 2
Answered: Oct 30, 2025
User Rep: 70,715
I have not made this into a function yet, but I wanted to outline a potential approach using patchwork that might be simpler to manage. To be a function, this would require adding some bookkeeping to keep track of when we need 2 vs. 3 heights/widths. (And it's not clear to me why we need 3 here for b but only 2 for d.) wrap_elements(plot = grid::textGrob("Hello, top of the world!")) / p + plot_layout(height = c(1, 20)) -> a # Add grob on bottom # heights here needs to be 3 elements, for top / plot / bottom a / wrap_elements(plot = grid::textGrob("Hello, bottom of the world!")) + plot_layout(heights = c(1, 20, 1)) -> b # Add grob on left wrap_elements(plot = grid::textGrob("Hello, left of the world!", rot = 90)) + b + plot_layout(widths = c(1, 20)) -> c # Add another grob on top wrap_elements(plot = grid::textGrob("Hello again, top of the world!")) / c + plot_layout(height = c(1, 20)) -> d
r ggplot2
View Question ↗
Question