Search code examples
rgridextrar-grid

Multiple "Top" textGrob Titles


The following is a simple example of my issue (please forgive the repetitive plots - can't use my actual data)

Example:

#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)

Output:

enter image description here

Issue: The top two plots have been compressed. I attempted to use the textGrob, like follows:

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

But, I am not seeing a way to incorporate two separate titles. I have yet to try using cowplot, which might be a more reasonable way to go, but was curious if there was a way to do this using textGrob.

Thanks for your time!


Solution

  • As stated by user20650, you can do the following:

     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)
    

    To get the following: enter image description here