R如何使用grid.arrange textGrobs添加页眉,脚注,标题等

时间:2019-06-18 19:42:47

标签: r pdf grid grob

我有以下代码来生成1x3的图表网格,另存为.pdf。

#make blank list
plot_list <- list()

#assign plots to list
plot_list[[1]] <- mtcars %>% ggplot(., aes(x = mpg, y = disp)) + geom_point()
plot_list[[2]] <- mtcars %>% ggplot(., aes(x = mpg, y = hp)) + geom_point()
plot_list[[3]] <- mtcars %>% ggplot(., aes(x = mpg, y = drat)) + geom_point()

#define text
title_text <- "This text appears large and centered as a title"
caption_text <- "This text is a caption at the bottom.\nWhen split into two lines, it's not straight aligned and has forces a large space between lines."

#plot and save as pdf
pdf("myplots.pdf", width = 8.5, height = 11)
grid.arrange(plot_list[[1]], 
             plot_list[[2]], 
             plot_list[[3]],
             ncol = 1,
             top = textGrob(title_text,
                            gp = gpar(fontsize = 12, 
                                      fontface = "bold")),
             vp = viewport(width=0.9, height=0.9), #give some margins around the edge
             bottom = textGrob(caption_text, just = .6, gp = gpar(fontface = 3L, fontsize = 9))
             #sub = footnote2?
            )
dev.off()

这将输出:

enter image description here 我想在左上方(主标题上方)添加标题文本。另外,我想在页面左下方添加一个脚注/标题(与左侧对齐)。但是,grid.arrange documentation在如何将文本放在这样的位置上并没有提供太多帮助。有关如何执行此操作的任何建议?

0 个答案:

没有答案