2016-11-28 37 views
1

以下是我的問題的一個簡單的例子(請原諒的重複地塊 - 不能用我的實際數據)多個「頂」 textGrob標題

例子:

#packages 
library(grid) 
library(gridExtra) 
library(ggplot2) 

#simple plot 
p <- ggplot(mtcars, aes(wt,mpg)) 


# setting-up grid of plots...2 columns by 4 rows 
sample <- grid.arrange(p + geom_point()+labs(title="Sample \nTitle One"), 
       p + geom_point()+labs(title="Sample \nTitle Two"), 
       p + geom_point(), 
       p + geom_point(), 
       p + geom_point(), 
       p + geom_point(), 
       p + geom_point(), 
       p + geom_point(), 
       ncol = 2) 

輸出:

enter image description here

問題:排名前兩位的情節已被壓縮。我試圖使用textGrob,如下所示:

top = textGrob("Sample Title One",hjust = 1,gp = gpar(fontfamily = "CM Roman", size = 12)) 

但是,我沒有看到合併兩個單獨標題的方法。我還沒有嘗試使用cowplot,這可能是一個更合理的方法,但很好奇,如果有一種方法可以使用textGrob來做到這一點。

謝謝你的時間!

+1

完美的作品 - 你可以添加一個答案,如果你要我「選擇」爲回答。對此還是新的,所以也許這不是必需的。 – EntryLevelR

+1

聽起來不錯 - 我會這樣做的。謝謝! – EntryLevelR

回答

1

正如user20650說,你可以做到以下幾點:

grid.arrange(arrangeGrob(p,p,p,p,top=textGrob("Sample Title One"), 
ncol=1), arrangeGrob(p,p,p,p,top=textGrob("Sample Title Two"), ncol=1), 
ncol = 2) 

得到以下: enter image description here