6

いくつかのプロットを 3 X 2 のマトリックスに配置して、PNG ファイルとして作成およびエクスポートしようとしています。各行 (2 つのプロットを含む) には、独自の X 軸があります。grid.text を介して追加の軸を追加できますが、この grid.text は PNG ファイルでエクスポートされません。PNG エクスポートのプロット マトリックスに追加のテキストまたは Grobs を追加するにはどうすればよいですか?

以下は、サンプルの 2 X 2 プロット マトリックスです。

a<-rnorm(100,56,3)
b<-rnorm(100,43,6)
c<-data.frame(cbind(a,b))
colnames(c) <- c("A","B")

library(ggplot2)
library(gridExtra)
library(grid)

plot1<-ggplot(c, aes(x=A, y=B))+  geom_point(size=3)+stat_smooth()+
ggtitle("Plot1")+ ylab("Y Axis")
plot1

plot2<-ggplot(c, aes(x=B, y=A))+   geom_point(size=3)+   stat_smooth()+
ggtitle("Plot2")+ ylab("Y Axis")
plot2

plot3<-ggplot(c, aes(x=B, y=A))+ geom_point(size=3)+stat_smooth(se=FALSE)+
ggtitle("Plot3")+ ylab("Y Axis")
plot3

plot4<-ggplot(c, aes(x=A, y=B))+ geom_point(size=3)+ stat_smooth(se=FALSE)+
ggtitle("Plot4")+ ylab("Y Axis")
plot4

grid.arrange(arrangeGrob(plot1,plot2,plot3, plot4,ncol=2,
                         sub=textGrob("A (hr)", vjust=0,gp = gpar(fontsize=20,   fontfamily="Times New Roman")),
                         left=textGrob("                 B (MPH)", rot=90,gp =      gpar(fontsize=18, fontfamily="Times New Roman"), vjust=1)))

grid.text("This is were the additional x-axis goes", x = unit(0.5, "npc"), y = unit(.51, "npc"),gp = gpar(fontsize=20, fontfamily="Times New Roman"))
4

1 に答える 1

3

printオブジェクトをグリッド化する必要があります。(よくある質問です):

library(gridExtra)
png(); print( 
    grid.arrange(arrangeGrob(plot1,plot2,plot3, plot4,ncol=2,
                     sub=textGrob("A (hr)", vjust=0,gp = gpar(fontsize=20,   fontfamily="Times New Roman")),
                     left=textGrob("                 B (MPH)", rot=90,gp =      gpar(fontsize=18, fontfamily="Times New Roman"), vjust=1)))
             )

grid.text("This is were the additional x-axis goes", x = unit(0.5, "npc"), 
          y = unit(.51, "npc"),gp = gpar(fontsize=20, fontfamily="Times New Roman"))
 dev.off()

ここに画像の説明を入力

于 2015-06-23T21:16:41.647 に答える