我正在用ggplot2制作水平条形图。我想像往常一样将绘图的标题放在实际绘图区域上方,但将其水平居中放置在绘图窗口的整个宽度上。默认主标题水平居中于实际绘图区域的宽度(带有条形图和网格图)。
可以通过在以下位置调整水平值来水平移动标题
theme(plot.title = element_text(hjust=0.5))
但它似乎不适用于多行标题文本。
有什么办法可以将主标题移动到整个绘图窗口的水平中心?
例:
factor<-c("short label 1", "short label 2", "longer label requires quite a lot of space", "short label 3", "short label 4")
freq<-c(16,15,25,28,17)
data<-data.frame(factor,freq)
ggplot(data, aes(x=factor, y = freq))
+ geom_bar(stat="identity", width=0.5)
+ coord_flip()
+ labs(title = "Rather long title for the plot, requires quite a lot of space")
+ theme_grey(base_size = 15)
+ theme(text = element_text(family = "serif"), axis.text.x = element_text(size = 15, colour = "black", vjust=1), axis.text.y = element_text(size = 16, colour = "black", hjust=1), plot.title = element_text(vjust=1))
怎么样(使用gridextra
):
require(gridExtra)
factor<-c("short label 1", "short label 2", "longer label requires quite a lot of space", "short label 3", "short label 4")
freq<-c(16,15,25,28,17)
data<-data.frame(factor,freq)
g.plot<-ggplot(data, aes(x=factor, y = freq)) +
geom_bar(stat="identity", width=0.5) +
coord_flip() +
theme_grey(base_size = 15) +
theme(text = element_text(family = "serif"), axis.text.x = element_text(size = 15, colour = "black", vjust=1), axis.text.y = element_text(size = 16, colour = "black", hjust=1), plot.title = element_text(vjust=1))
g.t<-textGrob(label="Rather long title for the plot, requires quite a lot of space",just=c("center","center"))
grid.arrange(g.t,g.plot,ncol=1,heights=c(1,15))
编辑-根据@Baptise的评论,您还可以使用:
grid.arrange(g.plot,main="\nRather long title for the plot, requires quite a lot of space")
要么:
grid.arrange(g.plot,main=g.t)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句