How to plot multiple graphs on an A4 sheet with defined margins?

1

To illustrate the question I considered the hypothetical situation below using the ggplot2 and gridExtra libraries.

 library(ggplot2)
 library(gridExtra)

 df<-data.frame(x=1:10,y=1:10)
 a<-ggplot(data = df, aes(x = x, y=y^2)) + xlab("Posição") + ylab("Função") + ggtitle("Posição x Função")+geom_point()
 b<-ggplot(data = df, aes(x = x, y=y^3)) + xlab("Posição") + ylab("Função") + ggtitle("Posição x Função")+ geom_point()
 c<-ggplot(data = df, aes(x = x, y=y^4)) + xlab("Posição") + ylab("Função") + ggtitle("Posição x Função") + geom_point()
 d<-ggplot(data = df, aes(x = x, y=y^5)) + xlab("Posição") + ylab("Função") + ggtitle("Posição x Função") + geom_point()
 e<-ggplot(data = df, aes(x = x, y=y^6)) + xlab("Posição") + ylab("Função") + ggtitle("Posição x Função") + geom_point()
 f<-ggplot(data = df, aes(x = x, y=y^7)) + xlab("Posição") + ylab("Função") + ggtitle("Posição x Função") + geom_point()
 grid.arrange(a,b,c,d,e,f)

How can I plot the graphics generated by this routine on an A4 sheet taking into account that:

  • The orientation of the sheet is Portrait, and its left, top, right and bottom margins are respectively 3,3,2 and 2 centimeters;
  • The graphs are arranged side by side in two columns with 0.5cm of distance between them, a grid of 3x2;
  • The font size of the title is 12 and the captions are 10.
asked by anonymous 10.12.2015 / 05:55

1 answer

2

This was a bit harder than planned because I could not make pdf work with par(mai) to set the margins. To work, I made a pretty non-elegant strategy, but it seems to have worked.

What I did was to zero out all the margins of the graphics and create the pdf with plot area of the same size as an A4 sheet. So I filled the margins / spaces between charts with empty grobs.

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

df<-data.frame(x=1:10,y=1:10)
a<-ggplot(data = df, aes(x = x, y=y^2)) + xlab("Posição") + ylab("Função") +
  ggtitle("Posição x Função") + geom_point() + theme(plot.margin=unit(rep(0, 4), "in"))
b<-ggplot(data = df, aes(x = x, y=y^3)) + xlab("Posição") + ylab("Função") +
  ggtitle("Posição x Função") + geom_point() + theme(plot.margin=unit(rep(0, 4), "in"))
c<-ggplot(data = df, aes(x = x, y=y^4)) + xlab("Posição") + ylab("Função") +
  ggtitle("Posição x Função") + geom_point() + theme(plot.margin=unit(rep(0, 4), "in"))
d<-ggplot(data = df, aes(x = x, y=y^5)) + xlab("Posição") + ylab("Função") +
  ggtitle("Posição x Função") + geom_point() + theme(plot.margin=unit(rep(0, 4), "in"))
e<-ggplot(data = df, aes(x = x, y=y^6)) + xlab("Posição") + ylab("Função") +
  ggtitle("Posição x Função") + geom_point() + theme(plot.margin=unit(rep(0, 4), "in"))
f<-ggplot(data = df, aes(x = x, y=y^7)) + xlab("Posição") + ylab("Função") +
  ggtitle("Posição x Função") + geom_point() + theme(plot.margin=unit(rep(0, 4), "in"))

pdf("gg.pdf", width=21/2.54, height=29.7/2.54) #Tamanho da A4, de cm para in
blank <- rectGrob(gp=gpar(col="white"))
grid.arrange(blank, blank, blank, blank, blank,
             blank, a, blank, b, blank,
             blank, blank, blank, blank, blank,
             blank, c, blank, d, blank,
             blank, blank, blank, blank, blank,
             blank, e, blank, f, blank,
             ncol = 5,
             widths = c(3, 7.75, 0.5, 7.75, 2), #Calculado a partir da largura da A4 e das margens
             heights = c(3, 7.9, 0.5, 7.9, 0.5, 7.9,2)) #Calculado a partir da altura da A4 e das margens
dev.off()

The answer is still partial because I have not tried to solve the source part. I do not know if it's possible to use this definition of size 12 or 10, maybe it's best to run the tests in ggplot and then set manually.

    
13.12.2015 / 06:28