First page is blank when printing graphs in R
I am trying to generate a pdf with multiple graphs. Specifically, I want to keep my plots, 4 on each page. So I have the following code in r (which works, but leaves the page blank - the first one):
pdf("Plots/plots_numeric_four_in_page.pdf",paper="a4r",width = 14)
graphlist <- lapply(3:NCOL(agg_num), function(i) {
force(i)
tempColName=dataName_num[i]
print (tempColName)
p<-qplot(Group.1,agg_num[[tempColName]],data = agg_num,color=Group.2,geom = "line",main=tempColName) + xlab("Date") + ylab(paste("Count of ", tempColName)) + geom_line(size=1.5)+ scale_x_date(labels = date_format("%m/%Y"))+
theme(legend.position="bottom",legend.direction="horizontal")+ guides(col=guide_legend(ncol=3))
})
do.call("marrangeGrob",c(graphlist,ncol=2,nrow=2))
dev.off()
It displays about 50 graphs correctly, 4 on each page in PDF. However, it leaves the first page blank and starts from the second. I looked at the options for marrangeGrob but I couldn't find anything to solve the problem. Do you know a workaround or any way to fix this problem?
source to share
There is a known bug between ggplot2 gridExtra
which causes this for some marrangeGrob
containing ggplots. Manually overriding the function grid.draw.arrangelist
( src ) ( marrangeGrob
returns an object arrangelist
) could potentially fix it (suggested here ).
grid.draw.arrangelist <- function(x, ...) {
for(ii in seq_along(x)){
if(ii>1) grid.newpage() # skips grid.newpage() call the first time around
grid.draw(x[[ii]])
}
}
It may be safer to define a new class for the object in question arrangelist
and apply a fix to it than to override grid.draw
for each call marrageGrob
in scope.
source to share