ggplot2标题填充?如何调整? [英] ggplot2 title padding? how to tweak?

查看:165
本文介绍了ggplot2标题填充?如何调整?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



 看起来标题周围有一些填充,我无法弄清楚如何改变,有什么想法? > 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(color ='purple',fill ='pink',size = 3,linetype ='dashed'))
plot
plot + opts(title ='Graph Title')
plot

如果你运行这个,hold在你屏幕上的一张纸(我知道的老书)与标题中G和T的顶部一致,然后再次运行情节,你会发现你的纸上有一些灰色。我只能假设这是标题周围的一些填充指示?或者同样,如果你在没有标题的情况下运行它,并且将纸张(上面)与粉色背景的末尾对齐,那么使用标题运行它,G和T的顶部在纸张下面。



示例实质上来自 https:// github.com/hadley/ggplot2/wiki/Graph-Panel-Attributes



这表明有一个行高选项,但它似乎什么都不做 http://www.inside-r.org/packages/cran/ggplot2/docs / theme_text

解决方案

所以这里是一个破解:



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

此代码删除填充。



但我建议发送功能请求: https://github.com/hadley/ggplot2/issues?milestone=


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

解决方案

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=

这篇关于ggplot2标题填充?如何调整?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆