【问题标题】:how to change vertical position of ggplot title without altering axis label justification如何在不改变轴标签对齐方式的情况下更改ggplot标题的垂直位置
【发布时间】:2016-01-18 17:26:36
【问题描述】:

编辑:问题是由于我在需要更改theme(plot.title(element_text()) 时错误地尝试更改theme(title = element_text())。如果我更仔细地检查theme() documentation.

,我会注意到这一点

原帖:

更改标题垂直对齐也会改变 x 和 y 轴标签的位置。这是一个错误吗?还是我误解了theme() 的功能?我正在运行 ggplot2 版本 0.9.3.1

最小的可重现示例。

require(ggplot2)
set.seed(12345)
x <- rnorm(100,10,0.5)
y <- x * 3 + rnorm(100)
df <- data.frame(y,y)

默认标题离我的口味太近了....

ggplot(df,aes(x,y)) + 
geom_point() + 
labs(title="My Nice Graph")

当我尝试移动标题时,轴标签也会移动,并且在图表上难以辨认。

ggplot(df,aes(x,y)) + 
geom_point() + 
labs(title="My Nice Graph") + 
theme(title = element_text(vjust=2))

【问题讨论】:

  • 使用theme(plot.title = element_text(vjust=2))。看看here:“标题:所有标题元素:情节、轴、图例”。

标签: r ggplot2


【解决方案1】:

你想要plot.title而不是title

labs(title="My Nice Graph") + theme(plot.title = element_text(vjust=2))

替代快速修复是添加换行符:

  labs(title="My Nice Graph\n")

【讨论】:

  • 啊。非常感谢里卡多。所以我想我无意中在全球范围内设置了 vjust...
  • 换行符有效,但@george-dontas 的margin 方法似乎效果更好(至少对我而言)
【解决方案2】:

vjust 对我不起作用(我也认为值应该在 [0, 1] 中)。我用

... +
theme(
  plot.title = element_text(margin=margin(b = 10, unit = "pt"))
)

【讨论】:

  • 由于未知原因 vjust 对我也不起作用,margin 可以。
  • 我想我在某处看到 vjust 功能在较新版本的 ggplot2 中被删除...
猜你喜欢
  • 2021-11-14
  • 2018-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多