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

Asked

Viewed 418 times

1

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

 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, upper, right and lower margins are respectively 3.3.2 and 2 centimeters;
  • The graphs are arranged side by side in two columns with 0.5cm distance between them, a grid of 3x2;
  • The font size of the title is 12 and the caption size is 10.
  • 1

    Jean, two details: 1. There is a parenthesis left/missing in your first line. 2. Always put the code to load the packages when asking a question or giving an answer, by code it is not possible to know which packages are needed.

  • 1

    Do you care only about the margin, or also about the space that graphics will occupy? And the alignment on the page?

  • @Molx - Thanks for the corrections. I had not thought of details beyond the margins... but I will improve the question.

  • 1

    @Jean, I’m sorry to ask, but what’s the point of this? Maybe use the sweave is a more appropriate approach, depending on your objective.

  • @Bernado, I tried to create a problem similar to what I came across, created several graphs and need to print them in an organized way. I will check the sweave. Thank you!

1 answer

2


That was a little harder than planned, because I couldn’t do the pdf work with the par(mai), to define the margins. To work, I made a very elegant strategy, but it seems to have worked.

What I did was reset all the margins of the graphics and create the pdf with plotting area of equal size to an A4 sheet. So I filled the margin 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 biased, because I didn’t try to solve the source part. I do not know if it is possible to use this definition of size 12 or 10, maybe it is better to do the tests on ggplot and then set manually.

  • which width of Blank?

  • @Jean Width is defined in widths. Are the width of the margins and the spacing between the figures.

  • everything worked perfectly! If you have any more tips on the subject I will be grateful.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.