Add grid.text to arrange.grob for export as PNG
Asked Answered
R

1

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"))
Roberto answered 23/6, 2015 at 21:3 Comment(1)
I don't see any png() call.Achievement
A
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

Achievement answered 23/6, 2015 at 21:16 Comment(5)
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().Achievement
actually, print is not the way to draw grid objects (use grid.draw), it's only lattice and ggplot2 that have set this trend.Sherwood
Thanks, @baptiste. I will try that first next time.Achievement
With later versions of gridExtra (> 2.0.0?), sub has become bottom.Raspberry
I wonder if the package authors are aware of the BDSM uses of those terms?Achievement

© 2022 - 2024 — McMap. All rights reserved.