我想将标题放在图的内部,而不是默认的顶部位置。
这是一个简单的代码片段

library(ggplot2)
df <- data.frame(x = c(1:10), y = rnorm(10, 1, 2))
ggplot(df, aes(x, y))+
   geom_line() +
   ggtitle("Demo") +
   theme(plot.title = element_text(vjust = -3))

过去,我可以通过更改vjust值来做到这一点,但现在它不起作用。任何想法如何做到这一点?

最佳答案

Hadley在the ggplot issue "vjust not working in v 2.0 for plot.title?"中写道:

“所有文本元素现在都有一个边距,默认情况下,该边距会随着字体缩放
主题的大小。这导致更好的间距,尤其是在大字体时
大小。这意味着vjusthjust的黑客不再起作用。反而,
使用margin()element_text()参数“

t中使用bmargin参数来调整标题,例如:

ggplot(df, aes(x, y))+
  geom_line() +
  ggtitle("Demo") +
    theme(plot.title = element_text(margin = margin(t = 10, b = -20)))

r - 将标题垂直调整到绘图内-调整不起作用-LMLPHP

有关更多参数,请参见?margin

请注意,还应该对marginaxis.title.x使用axis.title.y参数:
ggplot() + ggtitle("this is title") + xlab("this is x") + ylab("this is y") +
  theme(plot.title = element_text(margin = margin(b = -10)),
        axis.title.x = element_text(margin = margin(t = -10)),
        axis.title.y = element_text(margin = margin(r = -10)))

r - 将标题垂直调整到绘图内-调整不起作用-LMLPHP

关于r - 将标题垂直调整到绘图内-调整不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34805506/

10-12 17:34
查看更多