Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change interlinear space in ggplot title?

Tags:

r

ggplot2

If I use \n with labs(title="whatever \n comes after this") I end up with quite a big space between the lines. Is there a way to influence it? (I mean except font-size of the title itself).

like image 546
Matt Bannert Avatar asked Nov 22 '12 10:11

Matt Bannert


2 Answers

you can do something like this ( option lineheight to modify line spacing)

  p <- qplot(mpg, wt, data = mtcars)
  p <- p + ggtitle("whatever \n comes after this") + 
  theme(plot.title = element_text(lineheight=.1))
  p
like image 193
agstudy Avatar answered Oct 15 '22 15:10

agstudy


To perfectly center everything (which \n will not do in several cases), keep every piece of text the same size or being able to relatively adjust it if there are more than 2 lines (which is useful in some cases) and at the same time being able to adjust the interlinear space, use this instead:

labs(title=expression(atop(textstyle("whateverline1"),atop(textstyle("whateverline2"),atop(scriptscriptstyle(""),textstyle("whateverline3"))))))

Then use labeller=label_parsed

This also works for facet_grid, xlab and ylab

Note the atop and textstyle to position the text whilst keeping it all the same size and the scriptscriptstyle("") to control spacing between lines. You can also use varied relative sizes of text using scriptstyle or scriptscriptstyle depending on your needs

like image 36
O. Bertrand Avatar answered Oct 15 '22 16:10

O. Bertrand