使用grid.arrange在multiplot ggplot2中更改多行标题

时间:2015-07-25 18:54:42

标签: r ggplot2

我遵循了讨论的建议

changing title in multiplot ggplot2 using grid.arrange

但我的2行标题不会改变字体大小。

1-我应该在grid.arrange中声明main还是使用grid.arrange而不使用main然后添加以下脚本

main=textGrob(paste("titleLine1", "titleLine2", sep = "\n"),gp=gpar(fontsize=20))

由于

卡罗尔

3 个答案:

答案 0 :(得分:6)

这是一种可能性,

enter image description here

library(grid); library(gridExtra)
tg <- textGrob("Title Goes Here", gp=gpar(fontsize=30))
sg <- textGrob("more subtle subtitle ", gp=gpar(fontsize=15, fontface=3L))
margin <- unit(0.5, "line")
grid.newpage()
grid.arrange(tg, sg, rectGrob(), 
             heights = unit.c(grobHeight(tg) + 1.2*margin, 
                              grobHeight(sg) + margin, 
                              unit(1,"null")))

答案 1 :(得分:2)

您可以采用不同的方向,并使用github ggplot2 最新版本:

library(ggplot2) # (github version) devtools::install_github("hadley/ggplot2")

p <-  ggplot(cars, aes(x=speed, y=dist))
p <- p + theme_bw()
p <- p + geom_bar(fill="blue", stat="identity")
p <- p + labs(title="Fast Cars",
              subtitle="Are more Fun")
p <- p + theme(axis.text.x=element_text(angle=90, hjust=0, vjust=1)) 
p <- p + theme(plot.title=element_text(size=30, hjust=0.5, face="bold", colour="darkgreen", vjust=-1))
p <- p + theme(plot.subtitle=element_text(size=20, hjust=0.5, face="italic", color="darkred"))
p

最近这里添加了: https://github.com/hadley/ggplot2/pull/1582

让我知道它是否有效!

答案 2 :(得分:0)

paste命令提供了当前(2015年12月)由gridExtra正确解释的换行符分隔:

heres_the_tricky_part <- paste("mainTitle", "subTitle", sep="\n")

grid.arrange(plot1, plot2, nrow=1, ncol=2, top=heres_the_tricky_part )