3

I have followed the advice on the discussion

changing title in multiplot ggplot2 using grid.arrange

but my 2-line title doesn't change the font size.

1- should I state main in grid.arrange or do I use grid.arrange without main and then, add the following script

main=textGrob(paste("titleLine1", "titleLine2", sep = "\n"),gp=gpar(fontsize=20))

Thanks

Carol

Community
  • 1
  • 1
carol
  • 153
  • 1
  • 4
  • 17
  • yes, it works. I had used them separately grid.arrange in one line and textGrob on the 2nd line. – carol Jul 25 '15 at 20:07
  • 2
    What should be done if I want to display title1 one the 1st row with for ex fontsize = 18 and title2 on the 2nd row with fontsize = 16? – carol Jul 25 '15 at 20:34
  • note that with v>=2.0.0 of gridExtra the `main` argument has been renamed `top` – baptiste Jul 25 '15 at 21:44

3 Answers3

8

Here's one possibility,

enter image description here

library(grid); library(gridExtra)
tg <- textGrob("Title Goes Here", gp=gpar(fontsize=30))
sg <- textGrob("more subtle subtitle ", gp=gpar(fontsize=15, fontface=3L))
margin <- unit(0.5, "line")
grid.newpage()
grid.arrange(tg, sg, rectGrob(), 
             heights = unit.c(grobHeight(tg) + 1.2*margin, 
                              grobHeight(sg) + margin, 
                              unit(1,"null")))
baptiste
  • 75,767
  • 19
  • 198
  • 294
3

You could go in a different direction and use ggplot2 latest build from github:

library(ggplot2) # (github version) devtools::install_github("hadley/ggplot2")

p <-  ggplot(cars, aes(x=speed, y=dist))
p <- p + theme_bw()
p <- p + geom_bar(fill="blue", stat="identity")
p <- p + labs(title="Fast Cars",
              subtitle="Are more Fun")
p <- p + theme(axis.text.x=element_text(angle=90, hjust=0, vjust=1)) 
p <- p + theme(plot.title=element_text(size=30, hjust=0.5, face="bold", colour="darkgreen", vjust=-1))
p <- p + theme(plot.subtitle=element_text(size=20, hjust=0.5, face="italic", color="darkred"))
p

This was added pretty recently here: https://github.com/hadley/ggplot2/pull/1582

let me know if it works!

spsaaibi
  • 452
  • 4
  • 13
0

The paste command provides newline separation that currently (December 2015) is correctly interpreted by gridExtra:

heres_the_tricky_part <- paste("mainTitle", "subTitle", sep="\n")

grid.arrange(plot1, plot2, nrow=1, ncol=2, top=heres_the_tricky_part )
ecoe
  • 4,994
  • 7
  • 54
  • 72