[英]Increase space between axis.title and axis.text in ggplot2 (version >= 0.9.0)
I am currently using the latest version of ggplot2 from github. 我目前正在使用github的最新版ggplot2。
In version 0.8.9 I could do the following to increase space between axis.title and axis.text: 在0.8.9版本中,我可以执行以下操作来增加axis.title和axis.text之间的空间:
Before: 之前:
ggplot(diamonds, aes(clarity)) + geom_bar() + opts(
axis.title.x = theme_text(vjust=-1.1)
)
Fix: 固定:
ggplot(diamonds, aes(clarity)) + geom_bar() + opts(
axis.title.x = theme_text(vjust=-1.1),
plot.margin = unit(c(1, 1, 0.8, 0.5), "lines")
)
and the axis.title becomes fully visible. 并且axis.title变得完全可见。
In the latest github version of ggplot2 plot.margin has no effect on axis.title: 在最新的github版本的ggplot2中,plot.margin对axis.title没有影响:
ggplot(diamonds, aes(clarity)) + geom_bar() + opts(
axis.title.x = theme_text(vjust=-0.2),
plot.margin = unit(c(1, 1, 2, 0.5), "lines"))
(notice the increased bottom margin - I can't get plot.background to work in the latest development version) (注意增加的底部边缘 - 我不能让plot.background在最新的开发版本中工作)
It seems that 0.8.9 allows axis.title to move over the extra space created by plot.margin, but this is not allowed in the latest development version. 似乎0.8.9允许axis.title在plot.margin创建的额外空间上移动,但在最新的开发版本中不允许这样做。
Is there a new way to accomplish this task (or a quick fix for it) in the latest development version? 在最新的开发版本中是否有新方法可以完成此任务(或快速修复)?
Any help appreciated. 任何帮助赞赏。
Use the margin
attribute of element_text
for axis.title
in theme
. 在
theme
使用element_text
的margin
属性作为axis.title
。 I use different margins for x and y since I rotate the y title to horizontal (making it easier to read). 我对x和y使用不同的边距,因为我将y标题旋转到水平(使其更容易阅读)。
library(ggplot2)
library(gridExtra)
ggplot(diamonds, aes(clarity)) +
geom_bar() +
theme(
axis.title.x = element_text(margin = unit(c(3, 0, 0, 0), "mm")),
axis.title.y = element_text(margin = unit(c(0, 3, 0, 0), "mm"), angle = 0)
)
opts
and theme_text
are no longer supported by ggplot. theme_text
不再支持opts
和theme_text。 My quick and dirty solution is therefore to just add line breaks at the start or end of the title. 因此,我的快速而肮脏的解决方案是在标题的开头或结尾添加换行符。 It might be ugly but gets the job done with a minimal effort.
它可能很丑,但只需很少的努力即可完成工作。
ggplot(diamonds, aes(clarity)) +
geom_bar() +
xlab("\nClarity") + ylab("Count\n")
I am closing the question as the fix I made seems to hold for now (see below). 我正在结束这个问题,因为我现在提出的解决办法似乎暂时搁置(见下文)。
I found the following code in plot-render.r:64: 我在plot-render.r:64中找到了以下代码:
xlab_height <- grobHeight(xlabel) +
if (is.null(labels$x)) unit(0, "lines") else unit(0.5, "lines")
plot_table <- gtable_add_rows(plot_table, xlab_height)
plot_table <- gtable_add_grob(plot_table, xlabel, name = "xlab",
l = panel_dim$l, r = panel_dim$r, t = -1)
ylab_width <- grobWidth(ylabel) +
if (is.null(labels$y)) unit(0, "lines") else unit(0.5, "lines")
plot_table <- gtable_add_cols(plot_table, ylab_width, pos = 0)
plot_table <- gtable_add_grob(plot_table, ylabel, name = "ylab",
l = 1, b = panel_dim$b, t = panel_dim$t)
which I changed to: 我改为:
xlab_height <- grobHeight(xlabel) +
if (is.null(labels$x)) unit(0, "lines") else unit(0.5, "lines")
plot_table <- gtable_add_rows(plot_table, xlab_height)
plot_table <- gtable_add_grob(plot_table, xlabel, name = "xlab",
l = panel_dim$l, r = panel_dim$r, t = -1, clip = "off") <---- here
ylab_width <- grobWidth(ylabel) +
if (is.null(labels$y)) unit(0, "lines") else unit(0.5, "lines")
plot_table <- gtable_add_cols(plot_table, ylab_width, pos = 0)
plot_table <- gtable_add_grob(plot_table, ylabel, name = "ylab",
l = 1, b = panel_dim$b, t = panel_dim$t, clip = "off") <---- here
This allows one to use the hjust/vjust combined with plot.margin to move the axis.titles without clipping. 这允许人们使用hjust / vjust结合plot.margin移动axis.titles而不剪切。
On request I filled this a bug on github . 根据要求,我在github上填写了这个bug。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.