Locate title in top left of grid.arrange

房东的猫 提交于 2020-05-15 04:18:33

问题


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

But what I need is:

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


回答1:


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.




回答2:


Using top argument:

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



来源:https://stackoverflow.com/questions/49685458/locate-title-in-top-left-of-grid-arrange

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!