垂直调整标题到剧情内 - vjust不工作 [英] Adjust title vertically to inside the plot - vjust not working
问题描述
我想将标题放置在绘图中而不是默认的顶部位置。
这里是一个简单的代码片段
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
的值来实现这一点,但现在它无法工作。任何想法如何做到这一点?
在
请参阅?margin
获取更多参数。
请注意,您应该为 axis.title.x
和 axis.title.y使用
以及: margin
参数
ggplot()+ ggtitle(this is title)+ xlab(这是x)+ ylab(this is y)+
theme(plot.title = element_text(margin = margin(b = -10)),
axis.title.x = element_text(保证金=保证金(t = -10)),
axis.title.y = element_text(保证金=保证金(r = -10)))
I want to place the title inside the plot instead at the default top position. Here is a simple code snippet
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))
In the past I was able to do this by varying vjust
value, but now it is not working. Any idea how to do this?
In the ggplot issue "vjust not working in v 2.0 for plot.title?", Hadley writes:
"All text elements now have a margin, which by default scale with the font
size in the theme. This leads to nicer spacing, particularly at large font
sizes. This means hacks with vjust
and hjust
no longer work. Instead,
use the margin()
parameter of element_text()
"
Play around with the t
and b
arguments in margin
to adjust the title, e.g.:
ggplot(df, aes(x, y))+
geom_line() +
ggtitle("Demo") +
theme(plot.title = element_text(margin = margin(t = 10, b = -20)))
See ?margin
for further arguments.
Note that you should use the margin
argument for axis.title.x
and axis.title.y
as well:
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)))
这篇关于垂直调整标题到剧情内 - vjust不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!