DiamondJoe12
DiamondJoe12

Reputation: 1809

grid.arrange - side-by-side plots being squished to small size in r markdown

I'm having an issue trying to use grid.arrange to arrange multiple plots in an RMarkdown document (output to html).

Without being able to post a replicable example of the plots themselves, here's the basic issue:

I can arrange two plots, side-by-side, reasonably well:

grid.arrange(
  plot1,
  plot2,
  ncol=2,
  top = "Title of the page"
)

Which looks like this:

enter image description here

But as soon as I try to add two more plots:

grid.arrange(
  plot1,
  plot2,
  plot3,
  ncol=2,
  top = "Title of the page"
)

grid.arrange starts to squish the plots:

enter image description here

I've tried adding a heightsparameter to grid.arrange, but no luck. Basically, I just want a reasonable looking arrangement of graphs that doesn't squish or distort them in the RMarkdown doc.

Upvotes: 4

Views: 4971

Answers (2)

Andrew A
Andrew A

Reputation: 195

It turns out the the problem is not in the functions themselves, but in the markdown used to establish the chunk. By default, the discrete output of images and plots from a chunk are displayed in a window of a specific size, and are scaled to fit it, so setting the height variable within the function itself, as you noted, does not work, and changing to a slightly different function, as has been suggested, also is unlikely to do much. The solution is to change the figure height of the output for the chunk itself, using the fig.height and related chunk options. Here is specifically how I did it:

```{r my-attempt, fig.height=9}
grid.arrange(
  plot1,
  plot2,
  plot3,
  ncol = 2,
  top = "Title of the page"
) 
```

This information can be found on this page of the R Markdown Cookbook

Edit: I seem to have overlooked that this solution was mentioned in the comments, and the asker already expressed reservations about it, specifically around effects on resolution. I have not experienced problems like that in my replication attempt, but of course I don't have the original plots. I still think that the solution offered here is probably the best simple way to resolve the issue, so I will be leaving this up.

Upvotes: 3

catrionelee
catrionelee

Reputation: 1

You could try another function ggarrange from the ggpubr package.

library(ggpubr)

ggarrange(
    plot1,
    plot2,
    plot3,
    common.legend = TRUE)

*Edit: misspelled function

Upvotes: 0

Related Questions