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:

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!

2

1 Answer

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:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest