R使用cookbook-R中的multiplot函数绘图时,如何添加文本(使用多行文字?)

R使用cookbook-R中的multiplot函数绘图时,如何添加文本(使用多行文字?),r,plot,ggplot2,R,Plot,Ggplot2,我使用multiplot函数from来堆叠形式为graph的大量图形,而不是multiplot。您可以使用grid.arrange: library(gridExtra) grid.arrange(arrangeGrob(p1,p2,ncol=2, nrow=1), main = "Here your title should be inserted",nrow=1) 如果要调整某些文本参数,以下内容可能会有所帮助: grid.arrange(arrangeGrob(p1,p2,ncol=2

我使用
multiplot
函数from来堆叠形式为
graph的大量图形,而不是multiplot。您可以使用grid.arrange:

library(gridExtra)

grid.arrange(arrangeGrob(p1,p2,ncol=2, nrow=1), main = "Here your title should be inserted",nrow=1)
如果要调整某些文本参数,以下内容可能会有所帮助:

grid.arrange(arrangeGrob(p1,p2,ncol=2, nrow=1), main = textGrob("Here your title should be inserted",gp=gpar(fontsize=16, font=2)))
要保存文件,请执行以下操作:

png("example-plot.png")
grid.arrange(arrangeGrob(p1,p2,ncol=2, nrow=1), main = textGrob("Here your title should be inserted",gp=gpar(fontsize=16, font=2)))
dev.off()

这确实有效,但我很难将堆叠的绘图保存在png文件中,因为显然,我必须做
g不确定文本消失的原因,我在答案中做了一些更新,您应该在哪里获得包含标题的图像。
p1 <- ggplot((subset(mtcars, gear==4)), aes(x=mpg, y=hp)) + 
      geom_point() +
      ggtitle("4 gears")
p2<-ggplot((subset(mtcars, gear==3)), aes(x=mpg, y=hp)) +
    geom_point() +
    ggtitle("3 gears")
multiplot(p1, p2, cols=2)
library(gridExtra)

grid.arrange(arrangeGrob(p1,p2,ncol=2, nrow=1), main = "Here your title should be inserted",nrow=1)
grid.arrange(arrangeGrob(p1,p2,ncol=2, nrow=1), main = textGrob("Here your title should be inserted",gp=gpar(fontsize=16, font=2)))
png("example-plot.png")
grid.arrange(arrangeGrob(p1,p2,ncol=2, nrow=1), main = textGrob("Here your title should be inserted",gp=gpar(fontsize=16, font=2)))
dev.off()