如何在ggplot2中调整标题位置 [英] How to adjust title position in ggplot2
本文介绍了如何在ggplot2中调整标题位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是代码:
require(ggplot2)
require(grid)
#pdf (a.pdf)
png('a.png')
a < - qplot(date,unemploy,data = economics,geom =line)+ opts(title ='A' )
b <-qplot(uempmed,unemploy,data = economics)+ geom_smooth(se = F)+ opts(title ='B')
c < - qplot(uempmed,unemploy,data = economics) ,geom =path)+ opts(title ='C')
grid.newpage()
pushViewport(viewport(layout = grid.layout(2,2)))
vplayout < - function(x,y)viewport(layout.pos.row = x,layout.pos.col = y)
print(a,vp = vplayout(1,1:2))
print(b,vp = vplayout(2,1))
print(c,vp = vplayout(2,2))
dev.off()
结果:
虽然这是我想要的,即将标题放置在y的顶部附近轴:
解决方案您正在寻找的是
theme(plot.title = element_text(hjust = 0))
。例如,使用最新版本的ggplot2和主题
而不是opts
,我们有a <-qplot(date,unemploy,data = economics,geom =line)+ ggtitle(A)+
theme或者,继续使用,或者使用
opts
a <-qplot(date,unemploy,data = economics,geom =line )+
opts(title =A,plot.title = element_text(hjust = 0))
Here is the code:
require(ggplot2) require(grid) # pdf("a.pdf") png('a.png') a <- qplot(date, unemploy, data = economics, geom = "line") + opts(title='A') b <- qplot(uempmed, unemploy, data = economics) + geom_smooth(se = F) + opts(title='B') c <- qplot(uempmed, unemploy, data = economics, geom="path") + opts(title='C') grid.newpage() pushViewport(viewport(layout = grid.layout(2, 2))) vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y) print(a, vp = vplayout(1, 1:2)) print(b, vp = vplayout(2, 1)) print(c, vp = vplayout(2, 2)) dev.off()
And result:
While here is what I would like to have, i.e. to position titles near the top of y-axis:
解决方案What you are looking for is
theme(plot.title = element_text(hjust = 0))
. For example, using the latest version of ggplot2 andtheme
instead ofopts
we havea <- qplot(date, unemploy, data = economics, geom = "line") + ggtitle("A") + theme(plot.title = element_text(hjust = 0))
Alternatively, staying with
opts
a <- qplot(date, unemploy, data = economics, geom = "line") + opts(title = "A", plot.title = element_text(hjust = 0))
这篇关于如何在ggplot2中调整标题位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文