4

I have two plots arranged side by side with gridExtra::grid.arrange. I can put a title on top of them with the top argument. Problem is, I was requested to locate the title on the top left of the plot.

A reproducible example:

library(ggplot2)
library(gridExtra)

p1 <- qplot(1:20)
p2 <- qplot(30, 35)
grid.arrange(p1, p2, nrow = 1, top = "Title")

which produces

enter image description here

But what I need is:

enter image description here

I read several times the ?arrangeGrob file (I think there lies my answer), but haven't figured out how to achieve it.

zx8754
  • 52,746
  • 12
  • 114
  • 209
PavoDive
  • 6,322
  • 2
  • 29
  • 55

2 Answers2

12

Using top argument:

grid.arrange(p1, p2, nrow = 1, top = grid::textGrob("Title", x = 0, hjust = 0))

enter image description here

zx8754
  • 52,746
  • 12
  • 114
  • 209
user9606341
  • 121
  • 2
3

patchwork is a gridExtra alternative that behaves more like ggplot than grid, including how its plot_annotate handles titles:

library(ggplot2)
library(patchwork)

qplot(1:20) + qplot(30, 35) + plot_annotation(title = 'Title')

If you want to adjust it further, its theme parameter accepts a ggplot theme call.

alistaire
  • 42,459
  • 4
  • 77
  • 117