nzcoops
nzcoops

Reputation: 9380

ggplot2 title padding? how to tweak?

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

Upvotes: 4

Views: 1045

Answers (1)

kohske
kohske

Reputation: 66842

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=

Upvotes: 3

Related Questions