4

It seems there is some padding around the title, which I can't figure out how to change, any thoughts?

xy <- data.frame(x=1:10, y=10:1)
plot <- ggplot(data = xy)+ geom_point(aes(x = x, y =  y))
plot <- plot + opts(plot.background = theme_rect(colour = 'purple', fill = 'pink', size = 3, linetype='dashed'))
plot
plot + opts(title = 'Graph Title')
plot

If you run this, hold a piece of paper on your screen (old school, I know) in line with the top of the G and T from the title, then run the plot again, you'll see you have some grey above your paper. Which I can only presume is indicative of some padding around the title? Or likewise, if you run it without the title and hold the paper (above) in line with the end of the pink background, then run it with the title, the top of the G and T are below the paper.

Example is essentially from https://github.com/hadley/ggplot2/wiki/Graph-Panel-Attributes

This suggests there is a line height option but it appears to do nothing http://www.inside-r.org/packages/cran/ggplot2/docs/theme_text

nzcoops
  • 9,132
  • 8
  • 41
  • 52
  • I can replicate your issue, but after a few minutes of fiddling about, I couldn't find a solution. Hopefully somebody with better ggplot2-fu will see this. Someone like @kohske. – Andrie Apr 26 '12 at 08:22
  • title has a padding of a half of the line height. – kohske Apr 26 '12 at 08:34
  • I just use `pdfcrop` on all my pdf's. Saves hassle with padding. – csgillespie Apr 26 '12 at 08:38
  • @csgillespie could you please elaborate? on a side note, I'm sticking multiple plots together using grid.arrange() which is why the padding jumped out at me in the first place, so a crop solution for a single pdf wouldn't really address the problem. cheers – nzcoops Apr 26 '12 at 12:34
  • [pdfcrop](http://pdfcrop.sourceforge.net/) is a command line unix function that removes any white space from around a pdf. Since you are using `grid.arrange`, this won't help you. – csgillespie Apr 26 '12 at 13:18
  • ah nice, will note that down for the future, thanks – nzcoops Apr 26 '12 at 13:24

1 Answers1

3

So here is a hack:

p <- plot + opts(title = 'Graph Title')
p <- ggplot_gtable(ggplot_build(p))
p$heights[[2]] <- p$heights[[2]]-unit(0.5, "lines")
grid.draw(p)

This code remove the padding.

But I'd recommend to send a feature request: https://github.com/hadley/ggplot2/issues?milestone=

kohske
  • 65,572
  • 8
  • 165
  • 155
  • looking at `p$layout` it didn't seem obvious to me which item in `p$heights` corresponded to `p$layout$name == "title"`. Is it a fixed order, or otherwise? – baptiste Apr 26 '12 at 09:24
  • Fixed at the moment but it would be better to get the position from the layout: `subset(p$layout, name=="title")$t` – kohske Apr 26 '12 at 09:28
  • thanks @kohske :) would not have found that, my how deep the rabbit hole goes *shakes fist at hadley* – nzcoops Apr 26 '12 at 12:36
  • on a side note, that second link I posted suggested lineheight, but I couldn't get it to do anything using it the way it was in the post, any comments re that? – nzcoops Apr 26 '12 at 12:38
  • @nzcoops What did you do exactly? – kohske Apr 26 '12 at 12:54
  • for example: > plot + opts(title = 'Graph Title', theme_text(lineheight = 10)) > plot + opts(title = 'Graph Title', theme_text(lineheight = 0.2)) You would think (from that second link I posted) that this should do something right? And you mention (comment to question) that your hack is to do with line height, so just curious why the theme_text(lineheight) isn't linked to the same height variable you changed in the hack? – nzcoops Apr 26 '12 at 13:14
  • As far as I can tell this argument of `gpar()` is useful for grobs that would use a `height` defined in units of lines, as in `convertUnit(grobHeight(rectGrob(height=unit(1,"line"), gp=gpar(lineheight=10))), "mm", "y")`. Unless you use `unit(, "lines")` to position the text label I don't think it will have any effect. – baptiste Apr 26 '12 at 19:58