【问题标题】:Increase space between axis.title and axis.text in ggplot2 (version >= 0.9.0)增加ggplot2中axis.title和axis.text之间的空间(版本> = 0.9.0)
【发布时间】:2012-02-03 16:24:47
【问题描述】:

我目前正在使用来自 github 的最新版本的 ggplot2。

在 0.8.9 版本中,我可以执行以下操作来增加 axis.title 和 axis.text 之间的空间:

之前:

ggplot(diamonds, aes(clarity)) + geom_bar() + opts(
    axis.title.x = theme_text(vjust=-1.1)
)

修复:

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")
)

并且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"))

(注意底部边距增加 - 我无法让 plot.background 在最新的开发版本中工作)

似乎0.8.9允许axis.title移动到plot.margin创建的额外空间上,但在最新的开发版本中是不允许的。

在最新的开发版本中是否有完成此任务的新方法(或快速修复它)?

任何帮助表示赞赏。

【问题讨论】:

  • @BrianDiggs 非常感谢添加图片,但是如果我编辑我的帖子以添加其他信息,除非我恢复使用链接,否则我不能保存它。
  • 我把数据放回去了。如果您在合理的时间内没有在这里得到回复,请尝试 ggplot2 或 ggplot2-dev 列表,它们都托管在 google 群组中。
  • 如果您可以将此作为错误提交,或者在 ggplot2-dev 邮件列表中提及它会很有用。
  • @hadley 我添加了一个快速修复的错误报告,但链接到此处了解详细信息 - 希望没关系。
  • 这在上个版本中已经改变了,你应该使用下面的代替theme(axis.title.y=element_text(margin=margin(20,0,0,0))),去here

标签: r ggplot2


【解决方案1】:

如何正确操作

element_textmargin 属性用于theme 中的axis.title。 我为 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)
    )

老黑客

ggplot 不再支持optstheme_text。因此,我快速而肮脏的解决方案是在标题的开头或结尾添加换行符。它可能很丑陋,但可以用最少的努力完成工作。

ggplot(diamonds, aes(clarity)) +
    geom_bar() +
    xlab("\nClarity") + ylab("Count\n")

【讨论】:

  • @branchwarren 在两个答案中都缺少另一种更合适的方法。现在添加。
【解决方案2】:

我正在结束这个问题,因为我所做的修复似乎暂时有效(见下文)。

我在 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)

我改成了:

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

这允许使用 hjust/vjust 与 plot.margin 结合来移动axis.titles而不进行剪辑。

应要求,我在github 上填写了此错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多