7

I am attempting to create and export, as a PNG file, with several plots arranged in a 3 X 2 matrix. Each row (containing two plots) has its own X axis. I can add the additional axes via a grid.text but this grid.text is not exported with the PNG file. How does one add additional text or Grobs to the plot matrix for PNG export?

Below is a sample 2 X 2 plot matrix

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"))
user20650
  • 24,654
  • 5
  • 56
  • 91
tcacek
  • 71
  • 1
  • 2

1 Answers1

4

You need to print grid objects. (It's a FAQ):

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()

enter image description here

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • I cannot see how a call to png() would create a pdf. The printing that occurs is "bounded" by the opening call to the graphics device and the dev.off. I will post what gets printed to the png file on my system. Sounds like you are saving from the interactive device. That will give you separate items .... which was why I explicitly called `png();print(...);grid.text(.);dev.off()`. – IRTFM Jun 23 '15 at 21:38
  • actually, print is not the way to draw grid objects (use `grid.draw`), it's only lattice and ggplot2 that have set this trend. – baptiste Aug 24 '15 at 06:30
  • Thanks, @baptiste. I will try that first next time. – IRTFM Aug 24 '15 at 06:38
  • With later versions of `gridExtra` (> 2.0.0?), `sub` has become `bottom`. – JWilliman Jan 29 '19 at 22:38
  • I wonder if the package authors are aware of the BDSM uses of those terms? – IRTFM Jan 30 '19 at 00:24