R - How to change size of a whole figure produced by grid.arrange?

1k views Asked by At

I want to display multiple ggplot objects in one figure using package gridExtra. It works, but titles are cut. I can fix it by changing plots font size, but I would rather change the width of the whole figure so that it is wide enough to fit in titles (and as wide as other outputs in my RMarkdown doc, e.g. the table right over it). I've tried to change margins by typing par(mai = 2*par('mai')) before call to grid.arrange, but it did nothing. Does anyone know how to do it properly? I am really confused about the gridExtra package.

library(gridExtra)
g <- grid.arrange(zad4_kl$pw, zad4_kl$pk, zad4_kl$pp, zad4_AC$pw, zad4_AC$pk, zad4_AC$pp, nrow = 2)
g

Screenshot of produced output

1

There are 1 answers

0
Roach On

If you are saving the image, then you can increase the width and dpi in ggsave.
In R Markdown, you can use fig.width and fig.height

{r, echo=FALSE, fig.width=8, fig.cap="A nice image."}

library(gridExtra)
g <- grid.arrange(zad4_kl$pw, zad4_kl$pk, zad4_kl$pp, zad4_AC$pw, zad4_AC$pk, zad4_AC$pp, nrow = 2)
g

If rendered image doesn't fit the Markdown output, then you will have to change font in individual plots.