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

标签 r plot ggplot2

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

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?" ,哈德利写道:

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

玩转 tb margin 中的参数调整标题,例如:

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

enter image description here

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

enter image description here

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

相关文章:

r - Googlesheets4 不读取我的电子表格

r - 如何在igraph中缩放边缘颜色?

r - 在 stat_密度图中使用facet_zoom

r - 如何根据第二个字符串删除字符串中的字符?

r - 根据公共(public)值组合 R 中的数据框行

plot - 如何在终端中使用 xmgrace 绘制某些列?

python - Matplotlib:使用 twinx() 和 cla() 清除第二个轴后无法重新绘制第一个轴

r - 向 ggplot2 图中添加辅助 y 轴 - 使其完美

r - 更改 ggplot2 中 strip 文本背景的高度无法按预期工作

r - 在 Tab delim 文件中删除额外的列