我想将标题放在图中而不是默认的顶部位置。
这是一个简单的代码片段
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
来做到这一点。值(value),但现在它不起作用。知道如何做到这一点吗?
最佳答案
在 the ggplot issue "vjust not working in v 2.0 for plot.title?" ,哈德利写道:
“所有文本元素现在都有一个边距,默认情况下会随字体缩放
主题中的大小。这会导致更好的间距,尤其是大字体
尺寸。这意味着黑客攻击 vjust
和 hjust
不再工作。反而,
使用 margin()
element_text()
的参数”
玩转 t
和 b
margin
中的参数调整标题,例如:
ggplot(df, aes(x, y))+
geom_line() +
ggtitle("Demo") +
theme(plot.title = element_text(margin = margin(t = 10, b = -20)))
见
?margin
为进一步论证。请注意,您应该使用
margin
axis.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 - 将标题垂直调整到绘图内部 - vjust 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34805506/