我正在使用ggplot2制作水平条形图。我想像往常一样将绘图的主标题放在实际绘图区域之上,但是将其水平居中到绘图窗口的整个宽度。默认主标题水平居中于实际绘图区域的宽度(使用条形和网格)。
通过调整
中的hjust值,可以水平移动标题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))
答案 0 :(得分:0)
这个怎么样(使用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)