3

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.

8
  • 7
    in RMD, you can change the height and width of each of the components i.e. ```{r your plot, fig.height = 14, fig.width = 16, fig.align = "center"}
    – akrun
    Apr 21, 2021 at 17:12
  • yeah, i tried that, but it seems to mess with the resolution of my plots if I change the fig.width and height Apr 21, 2021 at 20:32
  • 2
    maybe try with patchwork
    – akrun
    Apr 21, 2021 at 20:32
  • 2
    Could you please share your code so we can reproduce your problem?
    – Quinten
    May 8, 2022 at 18:06
  • 3
    I'm not reproducing the problem. What type of plots are you using? What styles and output options are you using? BTW, the figure height and width are not 'per plot' with grid.arrange. It's the size of the grid.arrange object. html_document defaults to some wicked margins. (You said html but there are many, many HTML options in RMD.) For the margin issue: <style>.main-container{max-width: unset;}</style>. If you are using flex_dashboard, try using vertical_layout: scroll. (Default is fill.)
    – Kat
    May 9, 2022 at 2:40

2 Answers 2

2
+150

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.

1
  • If you want to preserve the aspect ratios of the graphs, set the 2x2 plot to be twice the height of the 1x1. To find the dimensions, right click on the html and save the figure to png, open properties > details > dimensions > divide by 300 dpi to get in inches. For my single plot it was 4.48 x 3.2 (width x height inches), so setting {r plot4, fig.height = 3.2 * 2} for the 2x2 plot and fig.height = 3.2 for the 2x1 retains the aspect ratio of a single plot in both; or if you are basing off the 2x1 plot in the question, then you would do 3.2 * 4 for the height May 14, 2022 at 19:27
0

You could try another function ggarrange from the ggpubr package.

library(ggpubr)

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

*Edit: misspelled function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.